#hello.c
#include <stdio.h>int main(void) { printf("Hello World!\n"); return 0;}
Compile hello.c
file with gcc
$ gcc hello.c -o hello
Run the compiled binary hello
$ ./hello
Output => Hello World!
#Variables
int myNum = 15;int myNum2; // do not assign, then assignmyNum2 = 15;int myNum3 = 15; // myNum3 is 15myNum3 = 10; // myNum3 is now 10float myFloat = 5.99; // floating point numberchar myLetter = 'D'; // characterint x = 5;int y = 6;int sum = x + y; // add variables to sum// declare multiple variablesint x = 5, y = 6, z = 50;
#Constants
const int minutesPerHour = 60;const float PI = 3.14;
Best Practices
const int BIRTHYEAR = 1980;
#Comment
// this is a commentprintf("Hello World!"); // Can comment anywhere in file/*Multi-line comment, print Hello World!to the screen, it's awesome */
#Print text
printf("I am learning C.");int testInteger = 5;printf("Number = %d", testInteger);float f = 5.99; // floating point numberprintf("Value = %f", f);short a = 0b1010110; // binary numberint b = 02713; // octal numberlong c = 0X1DAB83; // hexadecimal number// output in octal formprintf("a=%ho, b=%o, c=%lo\n", a, b, c);// output => a=126, b=2713, c=7325603// Output in decimal formprintf("a=%hd, b=%d, c=%ld\n", a, b, c);// output => a=86, b=1483, c=1944451// output in hexadecimal form (letter lowercase)printf("a=%hx, b=%x, c=%lx\n", a, b, c);// output => a=56, b=5cb, c=1dab83// Output in hexadecimal (capital letters)printf("a=%hX, b=%X, c=%lX\n", a, b, c);// output => a=56, b=5CB, c=1DAB83
#Control the number of spaces
int a1 = 20, a2 = 345, a3 = 700;int b1 = 56720, b2 = 9999, b3 = 20098;int c1 = 233, c2 = 205, c3 = 1;int d1 = 34, d2 = 0, d3 = 23;printf("%-9d %-9d %-9d\n", a1, a2, a3);printf("%-9d %-9d %-9d\n", b1, b2, b3);printf("%-9d %-9d %-9d\n", c1, c2, c3);printf("%-9d %-9d %-9d\n", d1, d2, d3);
output result
20 345 700 56720 9999 20098 233 205 1 34 0 23
In %-9d
, d
means to output in 10
base, 9
means to occupy at least 9
characters width, and the width is not enough to fill with spaces, -
means left alignment
#Strings
char greetings[] = "Hello World!";printf("%s", greetings);
access string
char greetings[] = "Hello World!";printf("%c", greetings[0]);
modify string
char greetings[] = "Hello World!";greetings[0] = 'J';printf("%s", greetings);// prints "Jello World!"
Another way to create a string
char greetings[] = {'H','e','l','l','\0'};printf("%s", greetings);// print "Hell!"
Creating String using character pointer (String Literals)
char *greetings = "Hello";printf("%s", greetings);// print "Hello!"
NOTE: String literals might be stored in read-only section of memory. Modifying a string literal invokes undefined behavior. You can't modify it.!
C
does not have a String type, use char
type and create an array
of characters
#Condition
int time = 20;if (time < 18) { printf("Goodbye!");} else { printf("Good evening!");}// Output -> "Good evening!"int time = 22;if (time < 10) { printf("Good morning!");} else if (time < 20) { printf("Goodbye!");} else { printf("Good evening!");}// Output -> "Good evening!"
#Ternary operator
int age = 20;(age > 19) ? printf("Adult") : printf("Teenager");
#Switch
int day = 4;switch (day) { case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; default: printf("Weekend!");}// output -> "Thursday" (day 4)
#While Loop
int i = 0;while (i < 5) { printf("%d\n", i); i++;}
NOTE: Don't forget to increment the variable used in the condition, otherwise the loop will never end and become an "infinite loop"!
#For Loop
for (int i = 0; i < 5; i++) { printf("%d\n", i);}
#Break out of the loop Break/Continue
for (int i = 0; i < 10; i++) { if (i == 4) { break; } printf("%d\n", i);}
break out of the loop when i
is equal to 4
for (int i = 0; i < 10; i++) { if (i == 4) { continue; } printf("%d\n", i);}
Example to skip the value of 4
#While Break Example
int i = 0;while (i < 10) { if (i == 4) { break; } printf("%d\n", i); i++;}
#While continue example
int i = 0;while (i < 10) { i++; if (i == 4) { continue; } printf("%d\n", i);}
#Arrays
int myNumbers[] = {25, 50, 75, 100};printf("%d", myNumbers[0]);// output 25
change array elements
int myNumbers[] = {25, 50, 75, 100};myNumbers[0] = 33;printf("%d", myNumbers[0]);
Loop through the array
int myNumbers[] = {25, 50, 75, 100};int i;for (i = 0; i < 4; i++) { printf("%d\n", myNumbers[i]);}
set array size
// Declare an array of four integers:int myNumbers[4];// add elementmyNumbers[0] = 25;myNumbers[1] = 50;myNumbers[2] = 75;myNumbers[3] = 100;
#Enumeration Enum
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };
define enum variable
enum week a, b, c;enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a, b, c;
With an enumeration variable, you can assign the value in the list to it
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };enum week a = Mon, b = Wed, c = Sat;// orenum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a = Mon, b = Wed, c = Sat;
#Enumerate sample applications
enum week {Mon = 1, Tues, Wed, Thurs} day;scanf("%d", &day);switch(day) { case Mon: puts("Monday"); break; case Tues: puts("Tuesday"); break; case Wed: puts("Wednesday"); break; case Thursday: puts("Thursday"); break; default: puts("Error!");}
#User input
// Create an integer variable to store the number we got from the userint myNum;// Ask the user to enter a numberprintf("Please enter a number: \n");// Get and save the number entered by the userscanf("%d", &myNum);// Output the number entered by the userprintf("The number you entered: %d", myNum);
#User input string
// create a stringchar firstName[30];// Ask the user to enter some textprintf("Enter your name: \n");// get and save the textscanf("%s", &firstName);// output textprintf("Hello %s.", firstName);
#memory address
When a variable is created, it is assigned a memory address
int myAge = 43;printf("%p", &myAge);// Output: 0x7ffe5367e044
To access it, use the reference operator (&
)
#create pointer
int myAge = 43; // an int variableprintf("%d", myAge); // output the value of myAge(43)// Output the memory address of myAge (0x7ffe5367e044)printf("%p", &myAge);
#pointer variable
int myAge = 43; // an int variableint*ptr = &myAge; // pointer variable named ptr, used to store the address of myAgeprintf("%d\n", myAge); // print the value of myAge (43)printf("%p\n", \&myAge); // output the memory address of myAge (0x7ffe5367e044)printf("%p\n", ptr); // use the pointer (0x7ffe5367e044) to output the memory address of myAge
#Dereference
int myAge = 43; // variable declarationint*ptr = &myAge; // pointer declaration// Reference: output myAge with a pointer// memory address (0x7ffe5367e044)printf("%p\n", ptr);// dereference: output the value of myAge with a pointer (43)printf("%d\n", *ptr);