How to use Macros??

This is a discussion on How to use Macros?? within the C Programming forums, part of the General Programming Boards category; Hey peoples im new to macros and i have seem to have gotten the notation for my VOLUME macro, however ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    How to use Macros??

    Hey peoples im new to macros and i have seem to have gotten the notation for my VOLUME macro, however i dont know what to do for the PRINTSTR macro which is supposed to ask a string for you to input which will then be outputted onto the next line.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
       #define VOLUME(r) ((4.0 /3) * 3.14 * (r)*(r)*(r))
       #define PRINTSTR(str) printf("%s")
       char str[100];
       float r;
    
       printf("Please enter the radius of a sphere \n");
       scanf("%f", &r);
       printf("The volume of the sphere is: %.2f\n", VOLUME(r));
       printf("Enter a string you want to print \n");
       scanf("%s", str);
       PRINTSTR(str);
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    You went wrong into the printf function, replace it with:
    Code:
    #define PRINTSTR(str) printf("%s", str);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Whenever you have doubts in the macro just give
    Code:
     
     gcc -E filename.c 
    
    may be gcc or cc
    This will show you how the macros are expanded

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    This gets tedious on most files as the preprocessor also includes everything. Checking the macros and warnings is enough most of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 01:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:51 AM
  4. VS2003 Macros: Shortcut key bindings?
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-27-2006, 10:38 AM
  5. Question on predefine macros
    By Kaminaga in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2005, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21