Thread: How to use Macros??

  1. #1
    Registered User
    Join Date
    Mar 2010

    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.

    #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);
       return 0;

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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Whenever you have doubts in the macro just give
     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
    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, 02:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. VS2003 Macros: Shortcut key bindings?
    By cboard_member in forum A Brief History of
    Replies: 2
    Last Post: 11-27-2006, 11:38 AM
  5. Question on predefine macros
    By Kaminaga in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2005, 12:03 PM