C Code by Shradha Khapra
C Code by Shradha Khapra
C Code by Shradha Khapra
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
V
1. First Program
#include<stdio.h>
int main() {
printf("Hello World");
return 0;
}
int main() {
int number;
int age;
int price;
return 0;
}
#include<stdio.h>
int main() {
int age = 22;
float pi = 3.14;
char percentage = '%';
return 0;
}
3. Comments
#include<stdio.h>
//This program prints Hello World
int main() {
printf("Hello World");
return 0;
}
4. Output
#include<stdio.h>
int main() {
int age = 22;
float pi = 3.14;
char percentage = '%';
int main() {
int a, b;
printf("enter a \n");
scanf("%d", &a);
printf("enter b \n");
scanf("%d", &b);
return 0;
}
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
int main() {
int age = 22;
int oldAge = age;
int newAge = oldAge + 2;
printf("new age is : %d", newAge);
/*
order of declaration is important - Wrong Declaration Order
float pi = 3.14;
float area = pi * rad * rad;
float rad = 3;
*/
// valid declaration
int age1, age2, age3;
age1 = age2 = age3 = 22;
//invalid
//int a1 = a2 = a3 = 22;
return 0;
}
2. Arithmetic Instructions
#include<stdio.h>
int main() {
int a = 1, b = 2, c = 3;
//valid
a = b + c;
//invalid
// b + c = a;
int main() {
printf("sum of 2 & 3 : %d", 2 + 3);
printf("sum of 2.0 & 3 : %f", 2.0 + 3);
printf("sum of 2.0 & 3.0 : %f", 2.0 + 3.0);
return 0;
}
> Associativity
#include<stdio.h>
int main() {
printf(" Output : %d", 5+2/2*3);
return 0;
}
3. Relational Operator
#include<stdio.h>
int main() {
printf("%d \n", 4==4);
4. Logical Operator
#include<stdio.h>
int main() {
printf("%d \n", 3<4 && 3<5);
printf("%d \n", 3<4 && 5<4);
int main() {
int a = 10;
a += 10;
printf("a+10 = %d \n", a);
a -= 10;
printf("a-10 = %d \n", a);
a *= 10;
printf("a*10 = %d \n", a);
a /= 10;
printf("a/10 = %d \n", a);
a %= 10;
printf("a%c10 = %d \n", '%', a);
return 0;
}
V
C Language Tutorial
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
Conditional Statements
(Chapter 3)
1. If-else
#include<stdio.h>
int main() {
int age = 19;
if(age >= 18) {
printf("you are an adult");
}
else {
printf("you are not an adult");
}
return 0;
}
int main() {
int number;
scanf("%d", &number);
if(number % 2 == 0) {
printf("even");
}
else {
printf("odd");
}
return 0;
}
int main() {
int age;
printf("Enter age : ");
scanf("%d", &age);
2. Ternary Operator
#include<stdio.h>
int main() {
int age;
printf("Enter age : ");
scanf("%d", &age);
int number = 7;
int luckyNumber = 7;
return 0;
}
3. Switch (integer)
#include<stdio.h>
#include<math.h>
int main() {
int day = 5;
switch(day) {
case 1 : printf("monday \n");
break;
case 2 : printf("tuesday \n");
break;
case 3 : printf("wednesday \n");
break;
case 4 : printf("thursday \n");
break;
case 5 : printf("friday \n");
break;
case 6 : printf("saturday \n");
break;
case 7 : printf("sunday \n");
break;
}
return 0;
}
4. Switch (character)
#include<stdio.h>
#include<math.h>
int main() {
char day = 'f';
switch(day) {
case 'm' : printf("monday \n");
break;
case 't' : printf("tuesday \n");
break;
case 'w' : printf("wednesday \n");
break;
case 'T' : printf("thursday \n");
break;
case 'f' : printf("friday \n");
break;
case 's' : printf("saturday \n");
break;
case 'S' : printf("sunday \n");
break;
}
return 0;
}
C Language Tutorial
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
1. Syntax of 3 Loops
# include <stdio.h>
int main () {
//for loop
for(int i=1; i<=100; i++) {
printf("%d\n", i);
}
//while loop
int i=1;
while(i<=100) {
printf("%d\n", i);
i++;
}
return 0;
}
C Language Tutorial
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
//function declaration/prototype
void printHello();
int main() {
//function call
printHello();
return 0;
}
//function definition
void printHello() {
printf("Hello!\n");
}
int main() {
int n;
printf("enter n : ");
scanf("%d", &n);
printf("square is : %d", calcSquare(n));
return 0;
}
int calcSquare(int n) {
return n * n;
}
# include <stdio.h>
//function to print factorial of n
int factorial(int n);
int main() {
int n;
printf("enter n : ");
scanf("%d", &n);
printf("factorial is : %d", factorial(n));
return 0;
}
int factorial(int n) {
if(n == 0) {
return 1;
}
int factnm1 = factorial(n-1);
int factn = factnm1 * n;
return factn;
}
C Language Tutorial
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
Pointers
(Chapter 6)
1. Syntax
#include<stdio.h>
int main() {
int age = 22;
int *ptr = &age;
int _age = *ptr;
printf("%d\n", _age);
//address
printf("%p\n", &age);
printf("%p\n", ptr);
printf("%p\n", &ptr);
//data
printf("%d\n", age);
printf("%d\n", *ptr);
printf("%d\n", *(&age));
return 0;
}
int main() {
int number = 4;
//call by value
square(number);
printf("n is : %d\n", number);
//call by reference
_square(&number);
printf("n is : %d\n", number);
return 0;
}
void square(int n) {
n = n * n;
printf("square is : %d\n", n);
}
void _square(int* n) {
*n = *n * *n;
printf("square is : %d\n", *n);
}
3. Swap 2 numbers
# include <stdio.h>
int main() {
int x = 3, y = 5;
//call by value
swap(x, y);
printf("x = %d & y = %d\n", x, y);
//call by reference
_swap(&x, &y);
printf("x = %d & y = %d\n", x, y);
return 0;
}
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
Arrays
(Chapter 7)
1. Syntax
# include <stdio.h>
int main() {
int marks[3];
printf("physics : ");
scanf("%d", &marks[0]);
printf("chem : ");
scanf("%d", &marks[1]);
printf("math : ");
scanf("%d", &marks[2]);
2. Pointer Arithmetic
# include <stdio.h>
int main() {
printf("%u\n", ptr);
ptr++;
printf("%u\n", ptr);
ptr--;
printf("%u\n", ptr);
ptr = ptr - _ptr;
printf("%u\n", ptr);
ptr = &_age;
printf("%d\n", ptr == _ptr);
return 0;
}
3. Accessing an Array
# include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
printNumbers(arr, 6);
printNumbers(arr, 6);
return 0;
}
void printNumbers(int *arr, int n) {
for(int i=0; i<n; i++) {
printf("%d : %d\n", i, arr[i]);
}
}
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
Strings
(Chapter 8)
1. Strings
# include <stdio.h>
# include <string.h>
int main() {
//declaration
char name[] = "Shradha Khapra";
char course[] = {'a','p', 'n', 'a', ' ', 'c', 'o', 'l', 'l', 'e', 'g', 'e',
'\0'};
//printing string
for(int i=0; name[i] != '\0'; i++) {
printf("%c", name[i]);
}
printf("\n");
//input a string
char firstName[40];
printf("enter first name : ");
scanf("%s", firstName);
printf("you first name is %s\n", firstName);
char fullName[40];
printf("enter full name : ");
scanf("%s", fullName);
printf("you first name is %s\n", fullName);
//Library Functions
char name[] = "Shradha";
int length = strlen(name);
printf("the length of name : %d\n", length);
while(ch != '\n') {
scanf("%c", &ch);
str[i] = ch;
i++;
}
str[i] = '\0';
puts(str);
return 0;
}
int main() {
char str[] = "ApnaCollege";
char ch = 'x';
checkChar(str, ch);
}
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
Structures
(Chapter 9)
1. Structures
# include <stdio.h>
# include <string.h>
struct student {
char name[100];
int roll;
float cgpa;
};
int main() {
struct student s1;
// s1.name = "Shradha"; // not a modifiable value
strcpy(s1.name,"Shradha");
s1.roll = 64;
s1.cgpa = 9.2;
//array of structures
struct student IT[60];
struct student COE[60];
struct student ECE[60];
//declaration
struct student s2 = {"Rajat", 1552, 8.6};
struct student s3 = {0};
//pointer to structure
struct student *ptr = &s1;
printf("student.name = %s\n", (*ptr).name);
printf("student.roll = %d\n", (*ptr).roll);
printf("student.cgpa = %f\n", (*ptr).cgpa);
//arrow operator
printf("student->name = %s\n", ptr->name);
printf("student->roll = %d\n", ptr->roll);
printf("student->cgpa = %f\n", ptr->cgpa);
//typedef keyword
coe student1;
student1.roll = 1664;
student1.cgpa = 6.7;
strcpy(student1.name, "sudhir");
return 0;
}
//change
s1.roll = 1660; //but it won't be reflected to the main function
//as structures are passed by value
}
//user defined
typedef struct student {
int roll;
float cgpa;
char name[100];
} stu ;
struct address {
int houseNo;
int block;
char city[100];
char state[100];
};
struct vector {
int x;
int y;
};
void calcSum(struct vector v1, struct vector v2, struct vector sum);
struct complex {
int real;
int img;
};
int main() {
acc acc1 = {123, "shradha"};
acc acc2 = {124, "rajat"};
acc acc3 = {125, "sudhir"};
void calcSum(struct vector v1, struct vector v2, struct vector sum) {
sum.x = v1.x + v2.x;
sum.y = v1.y + v2.y;
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
File I/O
(Chapter 10)
# include <stdio.h>
int main() {
FILE *fptr;
//Reading a file
char ch;
fptr = fopen("Test.txt", "r");
if(fptr == NULL) {
printf("doesn't exist!\n");
} else {
fscanf(fptr, "%c", &ch);
printf("character in file is : %c\n", ch);
fscanf(fptr, "%c", &ch);
printf("character in file is : %c\n", ch);
fclose(fptr);
}
//Writing in a file
ch = 'M';
fptr = fopen("NewFile.txt", "w");
fprintf(fptr, "%cANGO", ch);
fclose(fptr);
//fgets
fptr = fopen("NewFile.txt", "r");
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
fclose(fptr);
//fputc
fptr = fopen("NewFile.txt", "w");
fputc('a', fptr);
fputc('p', fptr);
fputc('p', fptr);
fputc('l', fptr);
fputc('e', fptr);
fclose(fptr);
return 0;
}
C Language Tutorial
(Basic to Advanced)
Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation
# include <stdio.h>
# include <stdlib.h>
//Dynamic Memory Allocation
int main() {
//sizeof function
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(char));
//malloc
// int *ptr;
// ptr = (int *) malloc(5 * sizeof(int));
//calloc
int *ptr = (int *) calloc(5, sizeof(int));
//free
free(ptr);
return 0;
}