Thread: How do you remember difference between Directive, Macros and Preprocessors in C

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    51

    How do you remember difference between Directive, Macros and Preprocessors in C

    Directive, Macros and Preprocessors always confuse me in C. I have been reading all but I don't understand difference them

    so to understand the difference between all these I thought that it can be understood by writing code

    Code:
    #include<stdio.h>
    
    
    #define tag 'A'  
    
    
    void main()
    {
    	char code = 'A';
    	
    	if (code == tag)
    	{
    		printf ("Yes \n");
    	}
    	else 
    	{
    		printf ("No \n");
    	}
    }
    Which is Directive, Macros and Preprocessors in code ?

    What's best way to remember difference between Directive, Macros and Preprocessors in C. ?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,069
    Links that might help understand the differences.

    C Preprocessor Directives - C and C++ Syntax Reference - Cprogramming.com
    Tutorials - C Preprocessor Tricks - Cprogramming.com

    To me a macro is an "#define" that looks like a function; but, that may not be the best way to remember it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    12
    It's a matter of definition.

    The preprocessor is conceptually a separate process run before the compile. Its job is to make changes to the source code before passing it to the compiler. It looks for and removes preprocessor directives and macro "calls", and inserts replacement C code as required. It is most definitely not part of your source program, so you can't find a "preprocessor" anywhere in your example.

    By "directive", I'll guess you mean "preprocessor directive". A preprocessor directive is a line in the source program to be pass information to or request some action to be performed by the preprocessor. They're easy to spot. The first character on a preprocessor line is a '#' character, followed by the name of the directive. Those two are usually run together, as in "#define" or "#include", but are really separate tokens. Your program contains one #include directive and one #define directive.

    A macro is a name defined by a preprocessor #define directive (or by some other means, such as a compiler command line switch or IDE project setting). It's either a single C identifier, or an identifier followed immediately by a '(' right parenthesis character, zero or more comma separated macro arguments and a ')' right parenthesis. You have one obvious macro ("tag") in your program, created on line 4 using a #define directive, and the used in line 11 where the preprocessor will substitute character constant 'A' for the name tag before compiling the source.

    There are two different kinds of macros. An object-like macro is a single identifier that is replaced by the same sequence of source program tokens every time the preprocessor sees it in the C program but not in quotes or in a comment. A function-like macro is an identifier followed by parenthesized arguments. These are also replaced before compilation, but the arguments following the macro name may be used to customize the replacement text.

    Some programmers think of the first kind as "constants" (a common use of object-like macros is to define symbolic constants such as NULL or EOF) and the second kind as "macros" (since function-like macros are used for library definitions like assert() or getc()). Either type may be used to generate any legal sequence of C tokens, though, and both types are called "macros" in the C Standard and in good compiler reference manuals.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define directive
    By abhi143 in forum C Programming
    Replies: 8
    Last Post: 12-09-2019, 02:40 PM
  2. #pragma directive
    By Usha in forum C Programming
    Replies: 1
    Last Post: 07-13-2014, 12:57 AM
  3. using directive.
    By leonm54 in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:08 AM
  4. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  5. Are preprocessors necessary?
    By Panopticon in forum C++ Programming
    Replies: 16
    Last Post: 01-05-2003, 07:46 PM

Tags for this Thread