how can I create macro definitions for the lines commented in the code.

Posted by yaprak on Stack Overflow See other posts from Stack Overflow or by yaprak
Published on 2010-03-29T15:47:44Z Indexed on 2010/03/29 15:53 UTC
Read the original article Hit count: 125

Filed under:
|
|
#include <stdio.h>
//Here use a macro definition that assigns a value to SIZE (for example 5)
int main()
{
       int i;
       int array[SIZE];
       int sum=0;
       for(i=0; i<SIZE; i++)
       {
               //Here use a macro definition named as CALCSUM to make the
               //following addition operation for the array
               printf("Enter a[%d] = ",i);
               scanf("%d", &array[i]);
               sum+=array[i];
               //Here use a macro definition named as VERBOSE to print
               //what program does to the screen
               printf("The user entered %d\n", array[i]);
               //
               //
               //If the macro definition CALCSUM is not used, the program
               //should assign 0 to the i-th element of the array
               array[i]=0;
               //Here, again use VERBOSE to print what program does to the screen
               printf("a[%d] is assigned to zero\n", i);
               //
               //
       }
       //If CALCSUM is defined, print the summation of the array elements to the screen
       printf("Summation of the array is %d\n",sum);
       //
       //If CALCSUM is not defined, but VERBOSE mode is used, print the following
       printf("All the elements in the array are assigned to zero\n");
       //
       printf("Program terminated\n");
       return 0;
}

When CALCSUM is defined, the program will sum up the values of each element in the given array.

  • If CALCSUM is not defined, each array element will be assigned to zero.
  • Besides, when VERBOSE mode is defined, the program will make print statements pointed out active.

[root@linux55]# gcc code.c
[root@linux55]# ./a.out
Program terminated
[root@linux55]# gcc code.c -D CALCSUM
[root@linux55]# ./a.out
Enter a[0] = 3
Enter a[1] = 0
Enter a[2] = 2
Enter a[3] = 5
Enter a[4] = 9
Summation of the array is 19
Program terminated
[root@linux55]# gcc code.c -D CALCSUM -D VERBOSE
[root@linux55]# ./a.out
Enter a[0] = 2
The user entered 2
Enter a[1] = 10
The user entered 10
Enter a[2] = 3
The user entered 3
Enter a[3] = 8
The user entered 8
Enter a[4] = 1
The user entered 1
Summation of the array is 24
Program terminated
[root@linux55]# gcc code.c -D VERBOSE
[root@linux55]# ./a.out
a[0] is assigned to 0
a[1] is assigned to 0
a[2] is assigned to 0
a[3] is assigned to 0
a[4] is assigned to 0
All the elements in the array is assigned to zero
Program terminated

© Stack Overflow or respective owner

Related posts about c

    Related posts about macro