[SOLVED] difference between #define and #include <stdio.h>

Status
Not open for further replies.

praveenkumar450

Newbie level 5
Joined
Jul 2, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Hyderabad, Andhra Pradesh, India
Visit site
Activity points
55
program 1


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#define AGE (14 / 2)
 
int main()
{
int x;
x=AGE;
   printf("think embedded is over %d years old.\n", x);
   return 0;
}


program 2


Code C - [expand]
1
2
3
4
5
6
7
#include <stdio.h>
int main()
{
 int x=5;
printf("%d",x);
return 0:
}


And the question is
in the first program 1 i had
mentioned my company AGE using variable x with #define preprocessor directive
finally x value is getting
so, now the thing is what happen when iam including #include <stdio.h>
preprocessor directive how the x value is finalizing and why ?
 
Last edited by a moderator:

Basically #define is a macro, used to define the duplicate names or values, and it doesn't take any memory location.
In the program 1 AGE is replaced with (14/2),

#include<stdio.h> is pre defined header file that will come with compiler
installation, printf, scanf like this functions will come under #include<stdio.h>

 

#define is the same of "Find & Replace"



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
#define AGE 20
 
int main()
{ 
   int x;
   x=AGE;
   printf("I have %d years old.\n", x);
   return 0;
}



The preprocessor runs, fins all ocorrences of "AGE" and replace with "20".
This is simple text substitution.

After the preprocessor runs the "find & replace" your code is as folows:


Code C - [expand]
1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
int main()
{
   int x;
   x=20; 
   printf("I have %d years old.\n", x);
   return 0;
}



And you see the result: "I have 20 years old."


#include INSERTS one file INSIDE the other

Look at these 2 files:

File 1: Geometry.h


Code C - [expand]
1
2
3
#define TRIANGLE 3
#define SQUARE 4
#define PENTAGON 5




File 2: Geometry.c


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>  /* the angle brackets means: find in the default directory */
 
#include "Geometry.h" /* the quotes means: find in the current directory */
 
int main()
{
    printf "A Triangle has %d sides\n", TRIANGLE);
    printf "A Square has %d sides\n", SQUARE);
    printf "A Pentagon has %d sides\n", PENTAGON);
    return 0;
}




The preprocessor runs e made 2 actions:

1) Insert "Geometry.h" inside "Geometry.c"
2) Find & replace the contents of "Geometry.h"


In the first pass you have:



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>  /* the angle brackets means: find in the default directory */
 
/* Geometry.h inside Geometry.c */
#define TRIANGLE 3 
#define SQUARE 4
#define PENTAGON 5
 
int main()
{
    printf "A Triangle has %d sides\n", TRIANGLE);
    printf "A Square has %d sides\n", SQUARE);
    printf "A Pentagon has %d sides\n", PENTAGON);
}





In the second pass you have:



Code C - [expand]
1
2
3
4
5
6
7
8
#include <stdio.h>  /* the angle brackets means: find in the default directory */
 
int main()
{
    printf "A Triangle has %d sides\n", 3);
    printf "A Square has %d sides\n", 4);
    printf "A Pentagon has %d sides\n", 5);
}



And the final result will be

A Triangle has 3 sides
A Square has 4 sides
A Pentagon has 5 sides



Hope this help.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…