473,473 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what does # mean in c programming

1 New Member
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
7 1860
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
24 New Member
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
9,208 Recognized Expert Moderator Expert
Perhaps, I hadn't heard that. However, it is the preprocessor that uses it and not the compiler.
Nov 16 '15 #4
donbock
2,426 Recognized Expert Top Contributor
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
donbock
2,426 Recognized Expert Top Contributor
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
24 New Member
by compiler i mean to compile it easily, because the value is less and can be compiled easily.
Feb 18 '16 #7
donbock
2,426 Recognized Expert Top Contributor
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

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

Similar topics

1
by: Charles Hixson | last post by:
I'm getting a list that I build which when printed is displaying, well, e.g.: d = << , 'Do', 'not', 'remove', 'thi', '.', ] >> Clearly I have a problem with the truncation of the final this, but...
4
by: QQ | last post by:
for example ~CcMessage() {} Thanks a lot! I am a beginner for C++, so please forgive my stupid questions
6
by: wintaki | last post by:
Given int x; What does x evaluate to? If x is 0, it equals 0x31, the ascii value of '1'. So for some reason, x evaluates to *(y+x) where x is a non pointer/array type.
1
by: alex14 | last post by:
Hi could someone please tell me what the double colons :: in perl scripts mean and what do they do? kind regards alex
2
by: SMichal | last post by:
Hi, I'm starting from my application simple bat data...test.bat. This file (test.bat) should start another aplication app.exe. I'm starting the test.bat from my application with Start() method of...
9
by: plusk1008 | last post by:
I have finals next week and I am stuck on one question on my review sheet for excel. So once again I beg: Please, please, please, please, please, please, please, please, please, please someone help...
2
by: mantrid | last post by:
in some php script ive seen $something==@$somethingelse whereas something==$somethingelse works the same what does the use of the @ do? Sorry if this is a stupid question
1
by: Daniel Brower | last post by:
I found documentation that uses the ^ notation after a type name. What does that notation mean? Daniel
1
by: Monica Pnade | last post by:
what does /^/ mean in perl?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.