You are on page 1of 42

C is an attitude!

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

ANSI C (C89) 1989

ANSI C (C9X) C++


Life time of a C program
The main thing…
• Significance of main

– parameters are restricted ( 0/2/3 )


– parameters passed from command line,
– declared by compiler and defined by user
– by convention a unique external function,
– implicit return 0
– return type is always is an int
And now for something that is
completely different…
# include <stdio.h>
# include <conio.h>

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;
}

Are the scope, visibility and life


time of i & j are different?
char ch;
for(ch=0;ch<=127;ch++)
printf(“%c %d \n“, ch, ch);
main(){
int i= -3,j=i;
i>>=2;
i<<=2;
if (i == j)
printf(“U are smart”);
else
printf(“U are not smart enough”);
}
int foo(int i)
{
static int j = foo(i);
return j+1;
}

printf(“%d”, foo(5));
++variable++

char *p= "C always have someway


around“;
char *p1;
p1=p;
while(*p!='\0')
++*p++;
printf(“%d”, sizeof(32767));
printf(“%d”, sizeof(32768));

(1) 2. 32767 is in positive range of


int, So it can be stored in an int.

(2) 4. 32768 is beyond the positive


range of int, it can’t be stored in
an int. It requires long so it prints
4.
printf(“%d”, sizeof(-32767));
printf(“%d”, sizeof(-32768));
(1) 2. Both -32767 and +32767 are in valid
range of int, so we cannot find out if it is a
negative integer constant or a constant
expression
(2) 4. If –32768 were treated as negative
integer constant it should have printed 2,
because it is in valid integer range. +32768
cannot be represented so promoted to
integer and – operator is applied on it. So
the only possibility is that this is a constant
expression.
printf(“%d”, sizeof((int)-32768));
int i =10;
if( (-i == ~i+1) && (-i == ~(i-1) )
printf(“Will this get printed.”);

This shows the equivalence between these


operators because of the simple reason
that ~ produces ones' complement in
twos' complement machines

~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)

int size(int arr[]){


return sizeof( arr ) / sizeof( int );
}
int arr[10];
printf(“%d”, DIM(arr, int));
printf(“%d”, size(arr));
Queries?
• It is flexible [to apply to any programming
area]…
• It is efficient [due to low-level semantics of
the language]…
• It is available [due to availability of C
compilers in essentially every platform]…
• It is portable [can be executed on multiple
platforms, even though the language has
many non-portable features]…”.

- Bjarne Stroustrup

You might also like