- #include <stdio.h>: This line is needed to run the line of code that starts with printf.
- int main(){ }: This is the starting point of the code. All the code inside the curly braces {} runs first.
- // output a line: This is a comment. It is not a line of code but a message we can add to code to tell ourselves or others what the code does. When the code is run this line will be ignored.
- printf("Hello World!");: This line of code prints, or outputs, the text “Hello World!” to the console. Printing text to the console is one way for a program to communicate with the user. The text inside is called string.
- gcc is how we run the compiler application.
- helloWorld.c is the filename of our code to be compiled.
- -o helloWorld is an optional but common addition to the command. It tells gcc to output the program executable under the name helloWorld. If this is left out, the executable file will be called a.out.
Type Description Values int a whole number -2,147,483,648 to 2,147,483,647 float a number with possible decimals 6 decimal places double a number with possible decimals 15 decimal places char stores one character (letter or number) a single character
printf("string to display", [list of optional parameters]).
symbol type %d or %i int %f double or float %c char
symbol effect \n newline \r carriage return \t tab
If you use const, it means you can't change your variable type.
Assigning values to variables: =, +=, -=, *=, /=, %=
Performing basic comparisons between values and variables: ==, !=, <, <=, >, >=
Using logical operators in C: &&, ||, !.
if (condition) {
// Statement(s)
}
if (condition) {
// Statement1 — do something
} else {
// Statement2 — do something else
}
if (condition) {
// Statement1 — do something
} else {
// Statement2 — do something else
}
if (condition) {
// Some code
} else if (condition) {
// Some code
} else {
// Some code
}
switch (grade) {
case 9:
printf("Freshman\n");
break;
case 10:
printf("Sophomore\n");
break;
case 11:
printf("Junior\n");
break;
case 12:
printf("Senior\n");
break;
default:
printf("Invalid\n");
break;
}
condition ? do something : do something else;
same as
if (condition) {
// Do something
} else {
// Do something else
}