22858 total geeks with 3297 solutions
Recent challengers:
best bread maker
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
sefo
anilg, new comments are deleted automaticall y because of some abuse recently
anilg
this is plain wierd. I submitted comments twice to article 950, and they dont seem to be there. Something wrong with the comment code?
CodeX
shout-boxes in general are old + the staff thing happened to everyone after an issue 2 months ago
anilg
/me is no longer staff :(
anilg
Also, osix's shoutbox predated twitter. Heh.

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jul 31
July Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
UK.gov sticks to IE
6 cos it"s more
"cost effective",
innit
T-Mobile UK pumps
out the iPhone 4
Polaroid 300
instant print
camera
NatWest dumps O2
Money
YouTube ups video
time limit
Alleged expenses
fiddlers to face
justice
Nude trampolinist
bounces free from
court
Nexus One phone
rockets to 28,000ft
UK.gov drops £6m on
Google
Fake Firefox update
used to sling
scareware
Slashdot
British ISPs Favour
Well-Connected
Customers
"Bizarre"
Nanobubbles Found
In Strained
Graphene
1-in-1,000 Chance
of Asteroid Impact
In
...
2182?
2 Chinese ISPs
Serve 20% of World
Broadband Users
World"s Fastest
Hybrid OK"d For
Production
Sometimes It"s OK
To Steal My Games
Thermoelectrics
Could Let You Feel
the Heat In Games
KDE SC 4.7 May Use
OpenGL 3 For
Compositing
Perl 6, Early, With
Rakudo Star
Internal Costs Per
Gigabyte —
What Do You Pay?
Article viewer

Using the C/C++ preprocessor.



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:C
Search OSI about C.More articles by dimport.
 viewed 9769 times send this article printer friendly

Digg this!
    Rate this article :
There are many different usage for the C/C++ preprocessor, but first of all, heres a little background on what they are, and what theyre used for.

The preprocessor works as a script language for the C/C++ complier, for example there are a if command in the preprocessor for C/C++ and only if the condition is true the code within the if will be complied... So you see some of the usage area's now...
Someof the usage of the C/C++ preprocessor can be to make you code multiplatform, so the source code "change" after what platform is being complied on. :)

All of the C/C++ preprocessors start with a #.

The #include preprocessor.

Didn't think this was a preprocessor did'nt you?
Anyway this preprocessor works thte way that it include the file specified to the current source file, if the file that will be included exist in the same directry in a didrectory "neer" the file the " can be used insted of:

Syntax(defualt): #include
Syntax(local): #include "filename"
The #define, #undef preprocessor.

The #define preprocessor can be used as a variable. the "only" different is that a #define must be #undef before it can be re #define'd, it don't have to be asigned a value but then it will just "return" true, it can also be used to creath a "function", the thing that the #define really do is that it replace itseld with the code/value that's it been asigned in the source code.

Variable Syntax: #define name value
Function Syntax: #define function(variables) code
Syntax: #undef name

The #if,#else,#ifdef,#ifndef,#elif,#endif preprocessor
The #if preprocessor works almost like an normal if condition in the C/C++ language. If the condition is true then is complie the code inside the #if, the #else is to be placed before the #endif and it is just what it sounds if the first #if condition is'nt true then it complie the code inside the #else. O yeah, the #endif end the #if started. the #elif is a "else if" statement it works as an #if just that it must be placed inside a #if and only become "tested" if the first #if is not true or if a #elif before is not true. The #ifdef works as a #if that only check if something is #define'd, just the ended with a #endif. The #ifndef works the opposite way as the #ifdef exapt that it must be ended with a #endif too.

Syntax: #if VARIABLE boolean VARIABLE
Syntax: #ifdef VARIABLE
Syntax: #ifndef VARIABLE
Syntax: #elif VARIABLE boolean VARIABLE
Syntax: #else
Syntax: #endif

The #error, #line C/C++ preprocessor.

The #error can be used to create a compiler error like the one that a variable is not defined it can be used to show that #define is not #define'd, have some imadination... :)

Syntax: #error Mooooo ;P

This article was originally written by shab

Did you like this article? There are hundreds more.

Comments:
Anonymous
2009-07-22 17:11:31
i need c++ preprocessor
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006