By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

what does # mean in c programming

P: 1
i know that it's a part of pre-processor i.e;#include<stdio.h>
but i want to know it's meaning individually in c programming.
Nov 3 '15 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. #define RATE 2.0
  2.  
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. #define RATE 2.0
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. /*MyHeader.h */
  2.  
  3. #define RATE 2.0
  4.  
Now go to each source file ad remove the #define and replace it with:

Expand|Select|Wrap|Line Numbers
  1. #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.
Nov 3 '15 #2

hefaz
P: 23
well, the reason # is used, because its ASCII value is less than others and thus it will be easy for compiler.
Nov 16 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Perhaps, I hadn't heard that. However, it is the preprocessor that uses it and not the compiler.
Nov 16 '15 #4

Expert 100+
P: 2,400
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.
Expand|Select|Wrap|Line Numbers
  1. #define TEST(a,b) printf( #a "<" #b "=%d\n", (a)<(b) )
  2. TEST(0, 0xFFFF);
  3. TEST('\n',10);
This expands to the following (after concatenation of adjacent strings):
Expand|Select|Wrap|Line Numbers
  1. printf( "0<0xFFFF=%d\n", (0)<(0xFFFF) );
  2. 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.
Nov 17 '15 #5

Expert 100+
P: 2,400
The hash character also has special meaning within a #define macro definition. A pair of hashes is the merge operator.
Expand|Select|Wrap|Line Numbers
  1. #define TEMP(i) temp ## i
  2. TEMP(1) = TEMP(2 + k) + x;
This expands to:
Expand|Select|Wrap|Line Numbers
  1. temp1 = temp2 + k + x;
Nov 17 '15 #6

hefaz
P: 23
by compiler i mean to compile it easily, because the value is less and can be compiled easily.
Feb 18 '16 #7

Expert 100+
P: 2,400
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.
Feb 18 '16 #8

Post your reply

Sign in to post your reply or Sign up for a free account.