This blog is under construction

Saturday 29 June 2013

C program to illustrate macro with arguments

Write a C program to illustrate macro with arguments.


  #include <stdio.h>
  #include <stdlib.h>

  /* macro with arguments */
  #define cube(x) (x * x * x)
  #define max(x, y, z) ((x = (x > y ? x : y)) > z ? x : z)
  #define sum(x, y, z) (x + y + z)

  int main() {
        int x, y, z, ch, output;
        printf("1. Cube\n2. Biggest of three numbers\n");
        printf("3. Sum of 3 numbers\n4. Exit\n");
        printf("Enter your choice:");
        scanf("%d", &ch);
        switch (ch) {
                case 1:
                        printf("Enter your input:");
                        scanf("%d", &x);
                        output = cube(x);
                        break;
                case 2:
                        printf("Enter your value for x, y & z:");
                        scanf("%d%d%d", &x, &y, &z);
                        output = max(x,y,z);
                        break;
                case 3:
                        printf("Enter your value for x, y & z:");
                        scanf("%d%d%d", &x, &y, &z);
                        output = sum(x, y, z);
                        break;
                case 4:
                        exit(0);
                default:
                        printf("You have entered wrong option\n");
                        break;
        }
        printf("Output: %d\n", output);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  1. Cube
  2. Biggest of three numbers
  3. Sum of 3 numbers
  4. Exit
  Enter your choice:2
  Enter your value for x, y & z:100 200 300
  Output: 300



1 comment:

  1. It is one of the most interesting blog articles. I appreciate your contents; I have incredible knowledge of this article. This blog is very helpful for programmers. I will be glad to read much more from your next updates. Now it's time to get car services Scottsdale AZ for more information.

    ReplyDelete