4. Constants
Values that don't change(fixed)
Types
Integer
Constants
Character
Constants
Real
Constants
1, 2, 3, 0
, -1, -2 1.0, 2.0,
3.14, -24
'a', 'b', 'A',
'#', '&'
5. 32 Keywords in C
Keywords
Reserved words that have special
meaning to the compiler
6. Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
10. Output
printf(" age is %d ", age);
printf(" value of pi is %f ", pi);
printf(" star looks like this %c ", star);
CASES
integers
1.
2. real numbers
3. characters
13. 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
Variables, Data Types + Input/Output
(Chapter 1)
V
1. First Program
#include<stdio.h>
int main() {
printf("Hello World");
return 0;
}
2. Variables & Data Types + Constants & Keywords
#include<stdio.h>
int main() {
int number;
int age;
int price;
return 0;
}
14. #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 = '%';
printf("age is %d", age);
printf("age is %f", pi);
printf("age is %c", percentage);
return 0;
}
5. Input (Sum of 2 numbers)
#include<stdio.h>
int main() {
int a, b;
printf("enter a n");
scanf("%d", &a);
printf("enter b n");
15. scanf("%d", &b);
printf("sum of a & b is : %d n", a+b);
return 0;
}
6. Practice Qs 1 (Area of Square)
#include<stdio.h>
//area of square
int main() {
int side;
scanf("%d", &side);
printf("%d", side * side);
return 0;
}
7. Practice Qs 2 (Area of Circle)
#include<stdio.h>
//area of square
int main() {
float radius;
scanf("%f", &radius);
printf("%f", 3.14 * radius * radius);
return 0;
}
17. Instructions
Type Declaration Instructions Declare var before using it
int a = 22;
int b = a;
int c = b + 1;
int d = 1, e;
VALID INVALID
int a = 22;
int b = a;
int c = b + 2;
int d = 2, e;
int a,b,c;
a = b = c = 1;
int a,b,c = 1;
58. - Execution always starts from main
Properties
- A function gets called directly or indirectly from main
- There can be multiple functions in a program
63. Argument v/s Parameter
values that are
passed in
function call
values in function
declaration &
definition
used to send
value
used to receive
value
actual
parameter
formal
parameters
64. NOTE
a. Function can only return one value at a time
b. Changes to parameters in function don't change the values in
calling function.
Because a copy of argument is passed to the function
66. a. Anything that can be done with Iteration, can be done with
recursion and vice-versa.
Properties of Recursion
b. Recursion can sometimes give the most simple solution.
d. Iteration has infinite loop & Recursion has stack overflow
c. Base Case is the condition which stops recursion.
67. 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
Functions & Recursion
(Chapter 5)
1. Function to print Hello
#include<stdio.h>
//function declaration/prototype
void printHello();
int main() {
//function call
printHello();
return 0;
}
//function definition
void printHello() {
printf("Hello!n");
}
2. Function to calculate square of a number
68. # include <stdio.h>
//function to calculate square of a number
int calcSquare(int 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;
}
3. Function to calculate n factorial (using recursion)
# 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;
}
69. Pointers
A variable that stores the memory
address of another variable
22
age
Memory
2010
ptr
2010
2013
70. Syntax
int age = 22;
int *ptr = &age;
int _age = *ptr;
22
age
Memory
2010
ptr
2010
2013
75. Pointers in Function Call
Call by
Value
call by
Reference
We pass value of
variable as
argument
We pass address of
variable as
argument
76. 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("%dn", _age);
//address
printf("%pn", &age);
printf("%pn", ptr);
printf("%pn", &ptr);
//data
printf("%dn", age);
printf("%dn", *ptr);
printf("%dn", *(&age));
return 0;
77. }
2. Pointers in Function call
# include <stdio.h>
void square(int n);
void _square(int* n);
int main() {
int number = 4;
//call by value
square(number);
printf("n is : %dn", number);
//call by reference
_square(&number);
printf("n is : %dn", number);
return 0;
}
void square(int n) {
n = n * n;
printf("square is : %dn", n);
}
void _square(int* n) {
*n = *n * *n;
printf("square is : %dn", *n);
}
3. Swap 2 numbers
# include <stdio.h>
void swap(int a, int b);
void _swap(int* a, int *b);
int main() {
int x = 3, y = 5;
//call by value
swap(x, y);
78. printf("x = %d & y = %dn", x, y);
//call by reference
_swap(&x, &y);
printf("x = %d & y = %dn", x, y);
return 0;
}
void swap(int a, int b) {
int t = a;
a = b;
b = a;
}
void _swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = *a;
}
88. Arrays as Function Argument
printNumbers(arr, n);
void printNumbers (int arr[ ], int n)
void printNumbers (int *arr, int n)
OR
//Function Declaration
//Function Call
95. What Happens in Memory?
char name[ ] = {'S', 'H', 'R', 'A', 'D', 'H', 'A','0'};
char name[ ] = "SHRADHA";
2000
name
S H R A D H A 0
2001 2002 2003 2004 2005 2006 2007
97. IMPORTANT
scanf( ) cannot input multi-word strings with spaces
Here,
gets( ) & puts( ) come into picture
98. String Functions
gets(str)
input a string
(even multiword)
puts(str)
output a string
fgets( str, n, file)
Dangerous &
Outdated
stops when n-1
chars input or new
line is entered
99. String using Pointers
char *str = "Hello World";
Store string in memory & the assigned
address is stored in the char pointer 'str'
char *str = "Hello World";
char str[ ] = "Hello World";
//cannot be reinitialized
//can be reinitialized
102. Standard Library Functions
3 strcat(firstStr, secStr)
concatenates first string with second string
<string.h>
firstStr should be large
enough
103. Standard Library Functions
4 strcpm(firstStr, secStr)
Compares 2 strings & returns a value
<string.h>
0 -> string equal
positive -> first > second (ASCII)
negative -> first < second (ASCII)
109. Structures
a collection of values of different data types
EXAMPLE
name (String)
For a student store the following :
roll no (Integer)
cgpa (Float)
112. Structures in Memory
struct student {
char name[100];
int roll;
float cgpa;
}
name
2010
cgpa
2114
2110
roll
structures are stored in contiguous memory locations
113. Benefits of using Structures
- Good data management/organization
- Saves us from creating too many variables
120. 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
Structures
(Chapter 9)
1. Structures
# include <stdio.h>
# include <string.h>
struct student {
char name[100];
int roll;
float cgpa;
};
typedef struct ComputerEngineeringStudent{
int roll;
float cgpa;
char name[100];
} coe;
void printInfo(struct student s1);
int main() {
121. struct student s1;
// s1.name = "Shradha"; // not a modifiable value
strcpy(s1.name,"Shradha");
s1.roll = 64;
s1.cgpa = 9.2;
printf("student info : n");
printf("name = %sn", s1.name);
printf("roll no = %dn", s1.roll);
printf("cgpa = %fn", s1.cgpa);
//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};
printf("roll no of s2 = %dn", s2.roll);
printf("roll no of s3 = %dn", s3.roll);
//pointer to structure
struct student *ptr = &s1;
printf("student.name = %sn", (*ptr).name);
printf("student.roll = %dn", (*ptr).roll);
printf("student.cgpa = %fn", (*ptr).cgpa);
//arrow operator
printf("student->name = %sn", ptr->name);
printf("student->roll = %dn", ptr->roll);
printf("student->cgpa = %fn", ptr->cgpa);
//Passing structure to function
printInfo(s1);
//typedef keyword
coe student1;
student1.roll = 1664;
student1.cgpa = 6.7;
122. strcpy(student1.name, "sudhir");
return 0;
}
void printInfo(struct student s1) {
printf("student info : n");
printf("name = %sn", s1.name);
printf("roll no = %dn", s1.roll);
printf("cgpa = %fn", s1.cgpa);
//change
s1.roll = 1660; //but it won't be reflected to the main function
//as structures are passed by value
}
> Some more Qs
# include <stdio.h>
# include <string.h>
//user defined
typedef struct student {
int roll;
float cgpa;
char name[100];
} stu ;
typedef struct computerengineeringstudent {
int roll;
float cgpa;
char name[100];
} coe;
struct address {
int houseNo;
int block;
char city[100];
char state[100];
};
struct vector {
int x;
123. int y;
};
void calcSum(struct vector v1, struct vector v2, struct vector sum);
struct complex {
int real;
int img;
};
typedef struct BankAccount {
int accountNo;
char name[100];
} acc ;
int main() {
acc acc1 = {123, "shradha"};
acc acc2 = {124, "rajat"};
acc acc3 = {125, "sudhir"};
printf("acc no = %d", acc1.accountNo);
printf("name = %s", acc1.name);
return 0;
}
void calcSum(struct vector v1, struct vector v2, struct vector sum) {
sum.x = v1.x + v2.x;
sum.y = v1.y + v2.y;
printf("sum of x is : %dn", sum.x);
printf("sum of y is : %dn", sum.y);
}
125. File IO
- RAM is volatile
FILE - container in a storage device to store data
- Contents are lost when program terminates
- Files are used to persist the data
127. Types of Files
Text Files
.exe, .mp3, .jpg
Binary Files
textual data
.txt, .c
binary data
128. File Pointer
FILE is a (hidden)structure that needs to be created for opening a file
A FILE ptr that points to this structure & is used to
access the file.
FILE *fptr;
129. Opening a File
fptr = fopen("filename", mode);
FILE *fptr;
Closing a File
fclose(fptr);
130. File Opening Modes
open to read
"r"
open to read in binary
"rb"
open to write
"w"
open to write in binary
"wb"
open to append
"a"
133. Writing to a file
fprintf(fptr, "%c", ch);
char ch = 'A';
134. Read & Write a char
fgetc(fptr)
fputc( 'A', fptr)
135. EOF (End Of File)
fgetc returns EOF to show that the file has ended
136. 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
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 : %cn", ch);
fscanf(fptr, "%c", &ch);
printf("character in file is : %cn", ch);
fclose(fptr);
}
//Writing in a file
ch = 'M';
fptr = fopen("NewFile.txt", "w");
137. fprintf(fptr, "%cANGO", ch);
fclose(fptr);
//fgets
fptr = fopen("NewFile.txt", "r");
printf("character in file is : %cn", fgetc(fptr));
printf("character in file is : %cn", fgetc(fptr));
printf("character in file is : %cn", fgetc(fptr));
printf("character in file is : %cn", fgetc(fptr));
printf("character in file is : %cn", 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);
//read the full file (EOF)
fptr = fopen("NewFile.txt", "r");
ch = fgetc(fptr);
while(ch != EOF) {
printf("%c", ch);
ch = fgetc(fptr);
}
printf("n");
fclose(fptr);
return 0;
}
138. Dynamic Memory Allocation
It is a way to allocate memory to a data structure during
the runtime.
We need some functions to allocate
& free memory dynamically.