C Programming Concepts
C Programming Concepts
(ACTS)
Advanced Computing for Human
Advancement
C Programming Concepts
C-DAC/ACTS
typedef:
typedef int INT;
typedef unsigned int UI;
typedef int * IP; IP ptr;
C-DAC/ACTS
typedef struct {
tnt member1;
float member2;
}newType;
newType var1, var2;
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
Advancement
arrayPtr aptr1;
Preprocessor:
#defie
#define MAX 100
#define SIZE 10
#define AND &&
C-DAC/ACTS
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
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
Preprocessor:
#Geieric Fuictioi
C-DAC/ACTS
Advanced Computing Training School
(ACTS)
Advanced Computing for Human
5
Advancement
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
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
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
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)