Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

difference between if else and #if #else

Status
Not open for further replies.

ep.hobbyiest

Full Member level 4
Joined
Jul 24, 2014
Messages
212
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
1,487
what is difference between if else and #if #else in normal coding.
As we can use #if and #else in programming then which is better?
 

# commands are pre-compiler directives. That means the command is for compiler to do tasks before creating actual machine code. Before creating an op-code, the compiler checks the condition and adds or substitutes the code lines to the actual program. You can substitute some portion of code or constants with other using these commands. These commands add or omit portion of code and will result in the output of the executable.

#define DEVICE "SAMSUNG" // pre-compiler directive - this command
// will substitute the word "DEVICE" in overall
// program with word "SAMSUNG" before
// compiling the actual code.

#if(DEVICE=="SAMSUNG") // This is also a pre compiler directive. this
{ // command adds the lines if the condition
codeline 1 // is true. that means if DEVICE is "NOKIA"
codeline 2 // the code will add lines followed by #else
} // that means the code lines 1 and 2 will
#else // not be added to actual executable after
{ // compilation. With the help of these
codeline 3 // commands you can use the same code for
codeline 4 // Nokia and Samsung devices with just minor
} // settings mentioned in #if and #else


Where as normal if-else commands are run time conditions and reflects the output of the actual program.
 
yes but when this preprocessor is used in function then..
when we use it in function then?
 

yes but when this preprocessor is used in function then..
when we use it in function then?

#define USELCD 0

.
.
.
.
.
.
// the function related to LCD will be added only
// if USELCD is true(nonzero) else functions for
// displaying character on 7 segment displays
// will be added to the actual code and functions
// for LCD will be removed from the code

#if USELCD // functions for LCD
void LCD_init(void)
{
// write code for LCD Initialisation
}

void LCD_Clear(void)
{
// write code for clearing the LCD
}

void LCD_putchar(unsigned char x)
{
// write code for displaying value of x on
// LCD
}
#else // function s for 7 segments
void Clear_Segments(void)
{
// write code for clearing 7 segment
// display
}
void SevenSeg(unsigned char x)
{
//write code for displaying value of x,
// on Seven Segment Display
}
#endif

// common function for displaying
// value of a on any display
void Display(unsigned char a)
{
#if USELCD
LCD_putchar(a);
#else
SevenSeg(a)
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top