Programming in c
Programming
Source Code
We as programmers write code in what is known as "Source Code", however your computer does not know how to read this, What a computer reads is binary, also known as "Machine Code".
There are many translation layers also known as "Compilers" between Source Code and Machine Code.
Below is an example of Source Code as a Hello World the c language
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
}
On line 1, we are importing a library called stdio.h this stands for Standard Input Output.
The stdio.h library will allow us to print text to the console using the printf();
function.
On line 3, int main(void)
Running C Code in VSCode
To run C code, you first need to compile the code, this can be done by typing Make [file]
in the terminal window.
Once your code has compiled without errors, type ./[file]
to run it.
Code Formatting
Single VS Double Quotes
Single quotes must be used when representing a single character, whereas double quotes must be used when representing more than one character, such as a string.
How to Represent OR in C
To represent OR, you would think, like some other languages, you would type or
, but c does not accept this. Instead, to represent or, simply use two vertical lines "||"
Constants
A Constant, or cosnt
means something, once created cannot be modified elsewhere in the code.
In this example, n--
will not work, because we are trying to modify a constant.
const n = 4
n--
Integer Overflow
Integer overflow is when an integer attempts to use more bits than what's available, causing a positive number to turn negative or visa versa.