C basics
Hello world
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World !\n");
return 0;
}
Variables
#include <stdio.h>
#include <stdlib.h>
int main()
{
char Name[] = "Protyro";
int Age = 24;
printf("My name is %s\n", Name);
printf("I'm %d years old", Age);
Age = 29;
printf("In five years, my age will be %d years old", Age);
return 0;
}
Data types
int age = 40;
double gpa = 3.6;
char grade = 'A';
char phrase[] = "Hello guys.";
Printf
#include <stdio.h>
#include <stdlib.h>
int main()
{
int anotherFavNum = 499;
char favLetter = 'z';
printf("%s guys, my favorite number is %d but I also like the number %d.\n","Hello", 500, anotherFavNum);
printf("And I'm %f years old now\n", 24.5);
printf("My favorite letter of the alphabet is %c", favLetter);
return 0;
}
Math
#include <stdio.h>
#include <math.h>
int main()
{
float result,value;
printf("Input a float value: ");
scanf("%f",&value);
result = sqrt(value);
printf("The square root of %.2f is %.2fn",value,result);
result = pow(value,3);
printf("%.2f to the 3rd power is %.2fn",value,result);
result = floor(value);
printf("The floor of %.2f is %.2fn",value,result);
result = ceil(value);
printf("And the ceiling of %.2f is %.2fn",value,result);
return(0);
}
Comments
/* This is a comment
on several lines */
// This is a comment on one line
Constants
#include <stdio.h>
#include <stdlib.h>
int main()
{
// const can't be modify, and use uppercases please :)
const int NUM = 5;
printf("%d \n", NUM);
// also considered as a constance
printf("Hello\n");
printf("%d", 70);
return 0;
}
User input
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old\n", age);
double gpa;
printf("Enter your gpa: ");
scanf("%lf", &gpa); // use %lf for double
printf("You gpa is %f\n", gpa);
char grade;
printf("Enter your grade: ");
scanf(" %c", &grade); // leave a blank before %c
printf("Your grade is %c\n", grade);
char name[20]; // define the number of characters expected
printf("Enter your name: ");
scanf("%s", name); // scanf take only characters (the first word) before a space
printf("Your name is %s\n", name);
char name[20]; // define the number of characters expected
printf("Enter your name: ");
fgets(name, 20, stdin); // use for string of characters
printf("Your name is %s\n", name);
return 0;
}
Arrays
#include <stdio.h>
#include <stdlib.h>
int main()
{
double unluckyNumbers[10]; // can store 10 double
unluckyNumbers[0] = 5.32; // assign 5.32 to the first double
printf("%f\n", unluckyNumbers[0]); // print 5.32
int luckyNumbers[] = {4, 8, 15, 16, 23, 42};
printf("%d\n", luckyNumbers[0]); //print 4
luckyNumbers[2] = 50; // replace 15 by 50
printf("%d\n", luckyNumbers[2]); //print 50
return 0;
}
Functions
#include <stdio.h>
#include <stdlib.h>
int main()
{
sayHi("Protyro", 24); // call function sayHi
sayHi("Toto", 44);
return 0;
}
void sayHi(char name[], int age) { // return anything, take char name[] and int age as parameter
printf("Hello %s, you are %d years old\n", name, age);
}
Return statements
#include <stdio.h>
#include <stdlib.h>
double cube(double num) // take num as parameter
{
double result = num * num * num; // cube the number
return result; // return the result with double format, exit the function
}
int main()
{
printf("Answer: %f\n", cube(3.0)); // call the function cube
return 0;
}
If statements
#include <stdio.h>
#include <stdlib.h>
int max(int num1, int num2, int num3)
{
int result;
if(num1 >= num2 && num1 >= num3){
result = num1;
} else if(num2 >= num1 && num2 >= num3) {
result = num2;
} else {
result = num3;
}
return result;
}
int main()
{
printf("The biggest number is: %d\n", max(13,9,15));
return 0;
}
Switch statements
#include <stdio.h>
#include <stdlib.h>
int main()
{
char grade = 'A';
switch(grade){
case 'A' :
printf("Well done !\n");
break;
case 'B' :
printf("You did alright\n");
break;
case 'C' :
printf("You did poorly\n");
break;
case 'D' :
printf("You did very bad\n");
break;
case 'F' :
printf("You failed\n");
break;
default :
printf("Invalid grade :(\n");
}
return 0;
}
Structs
#include <stdio.h>
#include <stdlib.h>
struct Student{ //struct definiton
char name[50];
char major[50];
int age;
double gpa;
};
int main()
{
struct Student student1; //create a container with student struct data
student1.age = 22;
student1.gpa = 3.2;
strcpy(student1.name, "Protyro"); // to set a string use strcpy
strcpy(student1.name, "Business");
printf("%f\n", student1.gpa);
struct Student student2; //create a container with student struct data
student2.age = 32;
student2.gpa = 2.5;
strcpy(student2.name, "James"); // to set a string use strcpy
strcpy(student2.name, "Art");
printf("%f\n", student2.name);
return 0;
}
While loops
#include <stdio.h>
#include <stdlib.h>
int main()
{
int index = 1;
while(index <= 5){
printf("%d\n", index);
index++;
}
do { // do while loop execute do instructions before check the while condition
printf("%d\n", index);
}while(index <= 5);
return 0;
}
For loops
#include <stdio.h>
#include <stdlib.h>
int main()
{
int luckyNumbers[] = {4,8,15,16,23,42};
int i;
for(i = 0; i < 6; i++){
printf("%d\n", luckyNumbers[i]);
}
return 0;
}
2D Arrays & Nested Loops
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
int nums[3][2] = { // 3 array elements and each one have 2 array elements inside
{1,2},
{3,4},
{5,6}
}; // 2 dimensions arrays
printf("%d\n", nums[0][0]); // print 1
printf("%d\n", nums[2][1]); // print 6
for (i = 0; i < 3; i++){ // 3 arrays elements --> i < 3
for (j = 0; j < 2; j++){ // each one have 2 values inside --> i < 2
printf("%d,", nums[i][j]);
}
printf("\n");
}
return 0;
}
Memory Addresses
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age = 30;
double gpa = 3.4;
char grade = 'A';
printf("age: %p\ngpa: %p\ngrade: %p\n", &age, &gpa, &grade); // %p for pointers, displays the memory address of the variables
return 0;
}
Pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age = 30;
int * pAge = &age; // this pointer varibale is storing the memory address of the age variable
double gpa = 3.4;
double * pGpa = &gpa; // this pointer varibale is storing the memory address of the gpa variable
char grade = 'A';
char * aGrade = &grade;
printf("age's memory address : %p\n", &age); // &age means we access to the memory address of the age variable
printf("age's memory address : %p\n", pAge); // here we use the pointer variable directly
return 0;
}
Dereferencing Pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age = 30;
int * pAge = &age;
printf("age's memory address : %p\n", &age); // print the memory address of age
printf("age's memory address : %p\n", pAge); // print the memory address of age thanks to the pointer variable
printf("age's memory value : %d\n", *pAge); // dereference of pointer, print the value of age
printf("age's memory value : %d\n", *&age); // derenference of the &age = address memory value / print the value of age
return 0;
}
Writing Files
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fpointer = fopen("/tmp/test.txt","w"); // if the path is not specified, it will use the current directory
fprintf(fpointer, "This is a test !\n");
fclose(fpointer);
fpointer = fopen("/tmp/test.txt","a"); // use 'a' to append and don't rewrite the file
fprintf(fpointer, "This is my second test ! :)\n");
fclose(fpointer);
return 0;
}
Reading Files
#include <stdio.h>
#include <stdlib.h>
int main()
{
char lines[250]; // we can read up to 250 chars
FILE * fpointer = fopen("/tmp/test.txt","r");
fgets(lines, 250, fpointer); // it will read fpointer and store the first line contain into the lines variable
printf("%s", lines);
fgets(lines, 250, fpointer); //read second line
printf("%s", lines);
fclose(fpointer);
return 0;
}
Last updated
Was this helpful?