0% found this document useful (0 votes)
139 views17 pages

C Programming Concepts

This document discusses the C preprocessor and its directives. It covers typedef, macros, stringizing and token pasting operators, preprocessor symbols, and conditional compilation directives like #if, #ifdef, #ifndef, #elif, #else, and #undef. The goal is to introduce common preprocessor concepts and show examples of how they can be used.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views17 pages

C Programming Concepts

This document discusses the C preprocessor and its directives. It covers typedef, macros, stringizing and token pasting operators, preprocessor symbols, and conditional compilation directives like #if, #ifdef, #ifndef, #elif, #else, and #undef. The goal is to introduce common preprocessor concepts and show examples of how they can be used.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Advanced Computing Training School

(ACTS)
Advanced Computing for Human
Advancement

C Programming Concepts
C-DAC/ACTS

Kaushal Kishor Sharma


Advanced Computing Training School ( ACTS )
CDAC, Pune
Welcome
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

typedef:
typedef int INT;
typedef unsigned int UI;
typedef int * IP; IP ptr;
C-DAC/ACTS

typedef struct Student


{ char name[20];
int rollNo;
float accBal;
}record;
record stud1, stud2;
record *ptr;
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

typedef int INT;


unsigned INT a; // wrong

typedef struct student sType;


struct sType; // wrong
C-DAC/ACTS

typedef struct {
tnt member1;
float member2;
}newType;
newType var1, var2;
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

int fadd(int, int);


typedef int (*ptype)( int, int);
ptype pf1, pf2;
pf1=fadd; pf1(10,20);

typedef int (*arrayPtr) [5];


C-DAC/ACTS

arrayPtr aptr1;

typedef void (*fptr) (int a, void (*) (float *));


fptr fp1;
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
1
Advancement

Preprocessor:
#defie
#define MAX 100
#define SIZE 10
#define AND &&
C-DAC/ACTS

#define BEGIN int main(void) {


#define END }
#define NEWLINE printf(“\n”);
#define INFINITE while(1);
#define MSG “hello world”;
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
2
Advancement

Preprocessor:
#parameterized Macros
#define MUL(a,b) ((a) * (b))
#define MINVAL(a,b) ((a) < (b) ? (a) : (b))
#define ISLOWER(ch) (ch>=97 && ch<=122)
C-DAC/ACTS

#define MUL(a,b) a*b // it can create prob


#define SQRT(a) a*a // it can create prob

MUL(1+2, 2+3);
SQRT(a++);
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
3
Advancement

Preprocessor:
#Nested Macros
#define ISLOWER(ch) (ch>=97 && ch <= 122)
#define ISUPPER(ch) (ch >= 65 && ch <=90)
#define ISALPHA(ch) ISLOWER(ch) || ISUPPER(ch)
C-DAC/ACTS
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
4
Advancement

#define SWAP(type, a, b) {type t ; t=a; a=b ; b=t ;}


Int main()
{
int x=10, y=20;
SWAP(int, x,y);
C-DAC/ACTS

printf(“x=%d y=%d”, x,y);


}
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

Preprocessor:
#Geieric Fuictioi
C-DAC/ACTS
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
5
Advancement

#define SQRT(FNAME, DTYPE) \


DTYPE FNAME(DTYPE X) \
{ \
return X*X; \
}
SQRT(sqrt_int, int)
C-DAC/ACTS

SQRT(sqrt_float, float)
SQRT(sqrt_double, double)
int main()
{ printf("%d\n", sqrt_int(10));
printf("%f\n", sqrt_float(10.1));
printf("%lf\n", sqrt_double(10.12));

}
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

Preprocessor:
-Tokei Pastiig Operator(##)
-Striigiziig Operator(#)
C-DAC/ACTS
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
6
Advancement

#define CONCAT(a,b) a##b


#define PRINT(var,format) printf(#var"=%"#format"\n", var);

int main()
{
int ab=20;
C-DAC/ACTS

char str[]="abcde";
printf("a##b = %d\n",CONCAT(a,b));
(CONCAT(print,f))("vlaue of ab is %d\n", ab);
PRINT(ab,d);
PRINT(str,s);
}
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

Preprocessor:
-Preprocessor Symbols
C-DAC/ACTS
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
7
Advancement

int main()
{
printf("File name: %s\n", __FILE__);
#line 1000
printf("On line no.: %d\n", __LINE__);
printf("Inside function: %s\n", __FUNCTION__);
C-DAC/ACTS

printf("Compiled at: %s\n", __TIMESTAMP__);


printf("Compiled on: %s\n", __DATE__);
printf("Executed at: %s\n", __TIME__);
}
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

Preprocessor:
-Coiditioial compilatioi
#if
#ifdef - if defied
#ifidef - if iot defied
C-DAC/ACTS

#elif - else if
#else
#uidef
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
8
Advancement

#define ARCH_32
#define FLAG 1
int main()
{ #ifdef ARCH_32
printf("this is for 32 bit\n");
#else
C-DAC/ACTS

printf("this is for 16 bit\n");


#endif
#if FLAG==1
printf("FLAG is true\n");
#else
printf("FLAG is flase\n");
#endif
}
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement

Preprocessor directive :
- avoid multiple time iiclusioi of a
siigle header fle

#ifidef
C-DAC/ACTS

#eidif

#pragma oice
Some time after applyiig this all to our header fles, still same problem comes, thei ii
case compile header fles ( eg gcc -c test.h, gcc -c a.h, gcc -c b.h)

You might also like