c Language Lecture
c Language Lecture
Memory
a b
25 S
Variables
Rules
c. no comma/blank space
Types
Integer Character
Constants Real Constants
1, 2, 3, 0
Constants 'a', 'b', 'A',
, -1, -2 1.0, 2.0, '#', '&'
3.14, -24
Keywords
Reserved words that have special
meaning to the compiler
32 Keywords in C
Keywords
auto double int struct
do if static while
#include<stdio.h>
int main() {
printf("Hello World");
return 0;
}
Comments
Lines that are not part of program
new line
printf(" kuch bhi \n");
Output
CASES
1. integers
printf(" age is %d ", age);
2. real numbers
printf(" value of pi is %f ", pi);
3. characters
printf(" star looks like this %c ", star);
Input
a.exe (windows)
Hello.c C Compiler
a.out (linux & mac)
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
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
Instructions
These are statements in a Program
Types
VALID INVALID
int a = 22; int a = 22;
int b = a; int b = a;
int c = b + 1; int c = b + 2;
int d = 1, e; int d = 2, e;
int a,b,c;
int a,b,c = 1;
a = b = c = 1;
Arithmetic Instructions
a+b
Operand 1 Operand 2
Operator
3%2=1
-3 % 2 = -1
Arithmetic Instructions
Type Conversion
*, /, % x = 4 + 9 * 10
+, -
= x=4*3/6*2
Arithmetic Instructions
Associativity (for same precedence)
Left to Right
x=4*3/6*2
Instructions
Control Instructions
Used to determine flow of program
a. Sequence Control
b. Decision Control
c. Loop Control
d. Case Control
Operators
a. Arithmetic Operators
b. Relational Operators
c. Logical Operators
d. Bitwise Operators
e. Assignment Operators
f. Ternary Operator
Operators
Relational Operators
==
>, >=
<, <=
!=
Operators
Logical Operators
&& AND
|| OR
! NOT
Operator Precendence
Priority Operator
1 !
2 *, /, %
3 +, -
4 <, <=, >, >=
5 ==, !=
6 &&
7 ||
8 =
Operators
Assignment Operators
=
+=
-=
*=
/=
%=
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;
}
Conditional Statements
Types
if-else Switch
if-else
if(Condition) {
//do something if TRUE
}
else {
//do something if FALSE
}
if(Condition 1) {
//do something if TRUE
}
else if (Condition 2) {
//do something if 1st is FALSE & 2nd is TRUE
}
Conditional Operators
Ternary
Condition ? doSomething if TRUE : doSomething if FALSE;
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;
}
Loop Control Instructions
To repeat some parts of the program
Types
for do while
while
for Loop
}
Special Things
- Increment Operator
- Decrement Operator
- Infinite Loop
while Loop
while(condition) {
//do something
}
do while Loop
do {
//do something
} while(condition);
break Statement
for( .. ) {
for( .. ) {
}
}
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;
}
Functions
Take Do Return
Argument Work Result
void printHello( );
Library User-
function defined
Special functions declared & defined by
inbuilt in C programmer
scanf( ), printf( )
Passing Arguments
functions can take value & give some value
void printHello( );
actual formal
parameter parameters
NOTE
calling function.
Because a copy of argument is passed to the function
Recursion
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;
}
Memory
age ptr
22 2010
2010 2013
Syntax
int age = 22;
int *ptr = &age;
int _age = *ptr;
Memory
age ptr
22 2010
2010 2013
Declaring Pointers
int *ptr;
char *ptr;
float *ptr;
Format Specifier
printf("%p", &age);
printf("%p", ptr);
printf("%p", &ptr);
Pointer to Pointer
A variable that stores the memory
address of another pointer
Memory
age pptr ptr
22 2013 2010
int **pptr;
char **pptr;
float **pptr;
Pointers in Function Call
Call by call by
Value Reference
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]);
}
}
char name[10];
float price[2];
Input & Output
scanf("%d", &marks[0]);
printf("%d", marks[0]);
Inititalization of Array
int marks[ ] = {97, 98, 89};
Memory Reserved :
Pointer Arithmetic
Pointer can be incremented
& decremented
CASE 1
Pointer Arithmetic
CASE 2
CASE 3
Pointer Arithmetic
//Function Call
printNumbers(arr, n);
Multidimensional Arrays
2 D Arrays
int arr[ ][ ] = { {1, 2}, {3, 4} }; //Declare
//Access
arr[0][0]
arr[0][1]
arr[1][0]
arr[1][1]