what does # mean in c programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikavya
    New Member
    • Nov 2015
    • 1

    what does # mean in c programming

    i know that it's a part of pre-processor i.e;#include<st dio.h>
    but i want to know it's meaning individually in c programming.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Any line that starts with a # is a preprocessor directive.

    The preprocessor goes through your code first and makes any changes called for by those lines starting with #.

    Suppose you have in your code a value, say 1.25, that is used in hundreds of functions in hundreds of source files. Now your boss says the rate is changed to 2.0 and you have to make hundreds of changes in those sources file without screwing up and changing a 1.25 to 2 where the 1.25 was something else and needed to be left alone.

    Improvement #1:
    At the beginning of each file you code:

    Code:
    #define RATE 2.0
    Now you change the 1.25 in those functions to RATE.

    When you compile the preprocessor scans the code and changes all RATE to 2.0 and creates a new source file with 2.0 in it. This file is the one the compiler compiles and it is called a translation file.

    To change the rate to 2.0 all you have to do is change the #define in each file to:

    Code:
    #define RATE 2.0
    Of course, you have to do this in each of the hundreds of source files.

    Improvement #2:

    Write a header file with the #define in it.

    Code:
    /*MyHeader.h */
    
    #define RATE 2.0
    Now go to each source file ad remove the #define and replace it with:

    Code:
    #include <MyHeader.h>
    The first time you need to change each source file but after that to change the rate to 3.0 all you have to do is change the #define in the header file to 3.0 and then recompile your code and you are done.

    #include inserts a copy of MyHeader.h in the translation file right where the #include appears in the original code. The #include is then commented out by the preprocessr so it doesn't cause a compile time error.

    Experienced developers start off with a #include <MyHeader.h> in each source file to avoid all the rigmarole of not having it.

    Comment

    • hefaz
      New Member
      • Nov 2015
      • 24

      #3
      well, the reason # is used, because its ASCII value is less than others and thus it will be easy for compiler.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Perhaps, I hadn't heard that. However, it is the preprocessor that uses it and not the compiler.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The hash character also has special meaning within a #define macro definition. A single hash character preceding the name of a macro argument is the unary stringization operator.
          Code:
          #define TEST(a,b) printf( #a "<" #b "=%d\n", (a)<(b) )
          TEST(0, 0xFFFF);
          TEST('\n',10);
          This expands to the following (after concatenation of adjacent strings):
          Code:
          printf( "0<0xFFFF=%d\n", (0)<(0xFFFF) );
          printf("'\\n'<10=%d\n", ('\n')<(10) );
          Notice that the prepreprocessor inserted a second backslash to insure the argument was properly converted to a string.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            The hash character also has special meaning within a #define macro definition. A pair of hashes is the merge operator.
            Code:
            #define TEMP(i) temp ## i
            TEMP(1) = TEMP(2 + k) + x;
            This expands to:
            Code:
            temp1 = temp2 + k + x;

            Comment

            • hefaz
              New Member
              • Nov 2015
              • 24

              #7
              by compiler i mean to compile it easily, because the value is less and can be compiled easily.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                I don't think the value of the character code for hash matters (whether it is small or it is large). The ASCII code for hash is 0x23; the EBCDIC code for hash is 0x7B.

                Consider the punctuation characters available on a standard keyboard. There are only four that didn't already have a meaning to the C compiler: ` @ # $
                I think the C preprocesser designers simply chose from what was available ... but it is all supposition.

                Comment

                Working...