C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
To start using C, you need two things:
- A text editor, like Notepad, to write C code
- A compiler, like GCC, to translate the C code into a language that the computer will understand
- str
- int
- float
- decimal
- Char
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C code.
Data type
#include
int main()
{
printf("Hello World!");
return 0;
}
#include
int main()
{
printf("Hello World!");
return 0;
}
Most C programs contain many statements.
The statements are executed, one by one, in the same order as they are written:
#include
int main()
{
printf("Hello World!");
printf("Have a good day!");
return 0;
}
Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
While Loop
The while loop loops through a block of code as long as a specified condition is true:
#include
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Statements
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
The following statement "instructs" the compiler to print the text "Hello World" to the screen:
#include
int main() {
printf("Hello World!");
return 0;
}
Comments in C
Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined. Single-line Comments Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by the compiler (will not be executed). This example uses a single-line comment before a line of code:
#include
int main() {
printf("Hello World!"); // This is a comment
return 0;
}