Professional Documents
Culture Documents
S. G. Ganesh
G. R. Prakash
“C is clearly not the cleanest language
ever designed nor the easiest to use, so
why do many people use it?”
- Bjarne Stroustrup
Agenda
• General Introduction
• Simple “Hello world” program
• Life time of C program
• Memory Organisation
• Dynamic Memory Allocation
• Some Interesting Problems
Open Sesame …
• A brief history
• ANSI C
• Special Features
• Present Status
• Future of C
The evolution of C
Algol-60
CPL
Algol-68
BCPL
Pascal
B
void main(){
char * message = “Hello world”;
clrscr();
puts(message);
}
# include <conio.h>
void main(){
# include <stdio.h>
char * message = “Hello world”;
clrscr();
puts(message);
}
# include <conio.h>
void main(){
char * message = “Hello world”;
# include <stdio.h>
clrscr();
puts(message);
}
# include <conio.h>
void main(){
char * message = “Hello world”;
clrscr();
# include <stdio.h>
puts(message);
}
# include <conio.h>
void main(){
char * message = “Hello world”;
clrscr();
puts(message);
# include <stdio.h>
}
# include <conio.h>
void main(){
char * message = “Hello world”;
clrscr();
puts(message);
}
# include <stdio.h>
# include <stdio.h>
void main(){
char * message = “Hello world”;
clrscr();
puts(message);
}
# include <conio.h>
// file header.h
{
if(1)
printf(“This will always get printed”);
else
printf(“This will never get printed”);
}
// file myfile.c
void main(){
# include “header.h”
}
# include <stdio.h>
# include <conio.h>
void main(){
char * message = “Hello world”;
clrscr();
printf(message);
}
printf(“%s”, message);
char * message = “I got 50%discount”;
# include <stdio.h>
# include <conio.h>
void main(){
char * message = “Hello world”;
clrscr();
printf(message+4);
}
# include <stdio.h>
# include <conio.h>
void main(){
int * message = “Hello world”;
clrscr();
printf(message+4);
}
# include <stdio.h>
# include <conio.h>
void main(){
int number = “Hello world”;
clrscr();
printf(“%s”, number +4 );
}
# include <stdio.h>
# include <conio.h>
void main(){
char * ptr = “Hello world”;
char arr[] = “Hello world”;
char confusion[] = {‘H’,’e’,’l’,..’d’,’\0’};
printf(“%d %d %d”,sizeof(ptr), sizeof(arr),
strlen(confusion));
}
# include <stdio.h>
# include <conio.h>
void main(){
char arr1[12] = “Hello world”;
char arr2[11] = “Hello world”;
printf(“%s %s”, arr1, arr2);
}
Where am I?
Command line
arguments
Stack
free
heap
Initialized Data
segment
Initialized to Zero
(BSS)
Program Code
int initToZero1;
static float initToZero2;
FILE * initToZero3;
double intitialized1 = 20.0;
int main(){
size_t (*fp)(const char *) = strlen;
char *dynamic = (char *)malloc(100);
int stringLength;
static int initToZero4;
static int initialized2 = 10;
strcpy(dynamic,”something”);
stringLength = fp(dynamic);
}
Allocate the Free;
Free the allocated
• Memory Allocator in C
• malloc, calloc & free
• realloc
• realloc – Complete memory manager
– as malloc
– as free
– as extender
– as shrinker
char *ptr=NULL;
ptr = realloc(ptr,100);
ptr = realloc(ptr, 250);
ptr = realloc(ptr,100);
ptr = realloc(ptr,0);
To Boggle your minds…
main()
{
int i;
static int j;
}
printf(“%d”, foo(5));
++variable++
~a = -1 ^ a
the reason being that -1 is represented as
all 1's and ex-or-ing it with the variable is
same as ones compliment
#define DIM( array, type) \
sizeof(array)/sizeof(type)
- Bjarne Stroustrup