Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

C programming

Basic template
#include <stdio.h>
#include <stdlib.h>

int main()
{
printf(“Hello World\n”);
return 0;
}
Variables
int num = 30;
const int NUM = 40; //constant number
char character = ‘A’;
char string [] = “DuoDuoSia”;
double d = 35.6;
char name[20]; //declare a size 20 array to store a string
printf();
printf(“%s”,string);
printf(“%c”,’A’);
printf(“%d”,10+20);
printf(“%f”,35.6);
printf(“%d %d”,num1, num2);

printf(“Hello World\n %s”, “C Programming”); //”\n” means next line


scanf();
scanf(“%s”,string); //read the first word (before space)
//string no need to put ‘&’
scanf(“%d”,&age);
scanf(“%lf”,&decimal); //to scan a decimal num, must put %lf
fgets(“%s”,20,stdin);
scanf(“%s%s”, firstName, lastName);

char name[20];
fgets(name,20,stdin); //read a whole line
Comment
/* */
OR
//
Array
int luckyNumbers[] = {4,8,15,6,23,42};
luckyNumbers[1] = 200; //change the element in luckyNumbers[1]
printf(“%d”,luckyNumbers[0]); //print 4
int luckyNumbers[10]; //an empty array that can hold 10 numbers
Function == Method
ReturnType FunctionName(parameters){}
int main(){ //main function
sayHi(“Mike”, “12”);
}
void sayHi(char name[],int age){
printf(“Hello %s, you are %d\n”,name,age);
}

//must created before the main method or else the main method don’t know its
existence
double cube(double num){
double result = num * num * num;
return result;
}

int main()
{
printf("Answer: %f", cube(4));
return 0;
}
If-Else
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;
}
Operator Meaning
int main() && AND
{ || OR
printf("Answer: %d", max(40,10,20));
!= Negate
if(3 > 2 || 2 > 5){
printf(“true”); == Equals to
}
return 0;
}
Switch Statement
char grade = ‘A’;
switch(grade){
case ‘A’:
prinf(“You did great!”);
break;
case ‘B’:
prinf(“You did alright!”);
break;
case ‘C’:
prinf(“You did poorly!”);
break;
case ‘F’:
prinf(“You failed!”);
break;
default:
printf(“Invalid Grade”);

}
Structs
struct Student{ //create an object
char name[50];
char major[50];
int age;
double gpa;
};

int main(){
struct Student student1;
student1.age = 22;
student.gpa = 3.2;
strcpy(student1.name, “Jim”); //strcpy = stringcopy
strcpy(student1.major, “Business”); //to parse the string into object

printf(“%s” student1.name);

return 0;
}
While loop
int index = 1;
while(index <= 5){ //print 1 → 5
prinf(“%d\n”,index);
index++;
}
Do-while loop
int index = 1;
do{ //print 1 → 5
prinf(“%d\n”,index);
index++;
} while(index <= 5);
For loop
int i;
for (i = 1; i <= 5; i++){
printf(“%d\n”,i);
}
2D Array
int nums [3][2] = {{1,2},{3,4},{5,6}};
prinf{“%d”, nums[0][0]};
Nested-for loop
int i, j;
for(i = 0; i < 3; i++){
for(j = 0; j < 2; j++){
printf(“%d,”, nums[i][j]);
}
printf(“\n”);
}
Pointers (Memory address) & Print memory address
int age = 30;
printf(“age: %p”, &age); //&age = a pointer (Memory address)
Pointer Variable (Memory address)
int age = 30;
int * pAge = &age; //this pointer variable store the address of age
printf("%p",pAge); //&age = a pointer (Memory address)
Deferencing pointer (Memory address)
int age = 30;
int * pAge = &age; //this pointer variable store the address of age
printf("%d",*pAge); //print out 30
Create a file
int main()
{
FILE * fpointer = fopen("employees.txt","w"); //w = overwrite a file

fprintf(fpointer,"Jim, Salesman\nPam, Receptionist\nOscar, Accounting");

fclose(fpointer);
return 0;
}
int main()
{
FILE * fpointer = fopen("employees.txt","a"); //a = append

fprintf(fpointer,"\nKelly, Boss");

fclose(fpointer);
return 0;
}
Read a file
int main()
{
char line[255];
FILE * fpointer = fopen("employees.txt","r");

fgets(line,255,fpointer);
printf("%s",line);
fgets(line,255,fpointer);
printf("%s",line);

fclose(fpointer);
return 0;
}

You might also like