Thread: macro

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    macro

    i need a good tutorial about macros
    what i know until now is that we
    Code:
    #define oneword
    but i dont know how to return to the main the code
    also i think we can use ## to merge numbers and words (i have tried to use ## to merge 2 variables in main but it didn't work ,looks like it works only with macros )

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by cable View Post
    i need a good tutorial about macros
    what i know until now is that we
    Code:
    #define oneword
    but i dont know how to return to the main the code
    also i think we can use ## to merge numbers and words (i have tried to use ## to merge 2 variables in main but it didn't work ,looks like it works only with macros )
    Hello. Do you want to learn how macros work?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Macros are text substitution. They just submit one block of text for another:
    Code:
    #define DOG 1
    printf( "the macro DOG is the value %d\n", DOG );
    When that compiles, the preprocessor turns the code into:
    Code:
    printf( "the macro DOG is the value %d\n", 1 );
    Quote Originally Posted by cable View Post
    i need a good tutorial about macros
    Let me google that for you


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    One simple example from me.

    Code:
     
    #include<stdio.h>
    #define N 4   // define size of the array below which equals 4
    #define T 0   // define Termination which equals 0 
    int main()
    {
    int x[N]={1,2,3,4};
    int i=0;
    
    for(; i<N; i++)
    printf("The elements of array are : %d ", x[i]);
    
    return T;
    }
    with the preprocessor command #define you will substitute the N and T with the values 4 and 0 respectively.
    The purpose that someone do this is to reuse the N & T in many points into the code.
    This example supports and contains only the basic "function" of the preprocessor command #define .

    Mr. Lnx
    Last edited by Mr.Lnx; 09-16-2011 at 04:13 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What are your error messages? Post the complete error message exactly as it appears in your development environment.

    Also can you explain what you think the last two lines in this snippet are doing?
    Code:
    const int SIZE = 5;
    int runtime_error = 0;
    
    int main(void)
    {
    	extern const int SIZE;
    	extern int runtime_error;
    Jim

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    problem solved
    Last edited by cable; 09-17-2011 at 07:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does following macro mean?
    By sanddune008 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 06:27 PM
  2. Use of Macro
    By NKP in forum C Programming
    Replies: 5
    Last Post: 06-02-2010, 08:30 PM
  3. MACRO-need help
    By new-b in forum C Programming
    Replies: 13
    Last Post: 06-22-2009, 10:17 PM
  4. need a macro that does nothing
    By Nyda in forum C Programming
    Replies: 5
    Last Post: 11-18-2004, 10:16 AM
  5. macro
    By sballew in forum C Programming
    Replies: 2
    Last Post: 10-10-2001, 07:51 PM