12.5 – Working with macros – Part 2

by subbu on December 18, 2013

Point 2: Macro is not a variable, takes no space; its value can’t be changed by assigning a new value

Example


int main()
 {
 printf("Value of PI %f",PI);
 PI=3.1435;
 printf("\nValue of PI %f",PI);
 return 0;
 }

Output:
Error: Lvalue required in function main

Example explained:

Show Expanded Source

 prog.c 2: int main()
 prog.c 3: {
 prog.c 4: printf("Value of PI %f",3.14);
 prog.c 5: 3.14=3.1435;
 prog.c 6: printf("\nValue of PI %f",3.14);
 prog.c 7: return 0;
 prog.c 8: }

By looking at the Expanded source, we come to know that in the line 5, we are assigning constant (Rvalue) to another constant (Lvalue), resulted error because Lvalue must be a variable in an assigning statement

Example


#define PI 3.14
#include<stdio.h>
int main()
{
 int a;
 a=sizeof(PI);
 printf("%d",a);
 return 0;
}

Output:
8

Example explained:

Expanded source to


a=sizeof(PI);

is


a=sizeof(3.14);

As the default type of any floating point constant in C language is double, it returned 8

Point 3: The scope of macro is from its point of declaration to the end of source file being compiled

Example:


#include<stdio.h>
int main()
{
 printf("%d",A);
 #define A 10
 printf("\n%d",A);
 return 0;
}

Output:
Error: Undefined symbol ‘A’ in function main()

Example explained:
Macro A is defined in the 5th line of the program, can’t be accessed to the portion before to its definition

Example:


#include<stdio.h>
void display();
int main()
{
 if(40<20)
 {
   #define A 10
 }
 display();
 printf("\n%d",A);
 return 0;
}
void display()
{
 printf("%d",A);
}

Output:
10
10

Example explained:

Show Expanded Source

 prog.c 2: void display();
 prog.c 3: int main()
 prog.c 4: {
 prog.c 5: if(40<20)
 prog.c 6: {
 prog.c 7:
 prog.c 8: }
 prog.c 9: display();
 prog.c 10: printf("\n%d",10);
 prog.c 11: return 0;
 prog.c 12: }
 prog.c 13: void display()
 prog.c 14: {
 prog.c 15: printf("%d",10);
 prog.c 16: }

Here macro A is defined in the if block which gives false but, replaced properly from its point of definition to the end of source file because macro substitution happens before execution of program.

Point 4: A macro definition may use previous definition

Example:


#define OUT printf
#define PRINT OUT("Hello World")
int main()
{
 PRINT;
 return 0;
}

Output:
Hello World

Example explained:

Show Expanded Source

 prog.c 3: int main()
 prog.c 4: {
 prog.c 5: printf("Hello World");
 prog.c 6: return 0;
 prog.c 7: }

Here Macro OUT is used in the macro definition PRINT, finally the replacement text of


PRINT;

is


printf("Hello World");

Example:


#define PRINT OUT("Hello World")
#define OUT printf
int main()
{
 PRINT;
 return 0;
}

Output:
Error

Example explained:
Here the scope of OUT would be from the 2nd line to the total source but, can’t be accessed to the previous lines

Point 5: Macro substitution is only done for tokens but not for strings

Example:


#define YES 1
int main()
{
 int a=YES;
 printf("%d",a);
 printf("\nYES",a);
 return 0;
}

Output:
1
YES

Example explained:

Show Expanded Source

 prog.c 2: int main()
 prog.c 3: {
 prog.c 4: int a=1;
 prog.c 5: printf("%d",a);
 prog.c 6: printf("\nYES",a);
 prog.c 7: return 0;
 prog.c 8: }

Here macro YES is replaced every where but, not with in the string

Point 6: A macro can be undefined using #undef

Example:


#include<stdio.h>
int main()
{
 #define A 10
 printf("\n%d",A);
 #undef A
 printf("\n%d",A);
 return 0;
}

Output:
Error: Undefined symbol ‘A’ in function main()

Example explained:


prog.c 2: int main()
prog.c 3: {
prog.c 4:
prog.c 5: printf("\n%d",10);
prog.c 6:
prog.c 7: printf("\n%d",A);
prog.c 8: return 0;
prog.c 9: }

Here A is replaced before #undef statement but, not replaced after #undef statement. Compiler assumes that A is an undeclared variable, which resulted error

Point 7: A variable can’t be declared with a name of macro within the scope of macro. Macro replaces the variable with the replacement text because macro substitution happens before compilation of program.

Example:


#include<stdio.h>
#define a 10
int main()
{
 int a=40;
 printf("%d",a);
 return 0;
}

Output:
Error

Example explained:

Show Expanded Source

 prog.c 3: int main()
 prog.c 4: {
 prog.c 5: int 10=40;
 prog.c 6: printf("%d",10);
 prog.c 7:
 prog.c 8:
 prog.c 9: return 0;
 prog.c 10: }

By looking at the Expanded source, we come to know that variable name is replaced with its replacement text.
Compiler doesn’t accept to declare variable with a number

Example:


#include<stdio.h>
#define a x
int main()
{
 int a=40;
 printf("%d",a);
 return 0;
}

Output:
40

Example explained:


prog.c 3: int main()
prog.c 4: {
prog.c 5: int x=40;
prog.c 6: printf("%d",x);
prog.c 7: return 0;
prog.c 8: }

It seems it is valid because preprocessor replaces a with x, compiler treats x as an int type variable.

Previous post:

Next post: