c programming - refresher - part iv

Post on 11-Nov-2014

519 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

C programming refresher from Embedded point of view. Fourth part talks about pre-processing

TRANSCRIPT

C Refresher

Team Emertxe

Day4

Compiling a C program

Compilation

C program consists of source code in one or more files

Each source file is run through the preprocessor and

compiler, resulting in a file containing object code

Object files are tied together by the linker to form a

single executable program

Pre processing and macros

Preprocessing

Preprocessing• Occurs before program compiled

• Inclusion of external files

• Definition of symbolic constants

• Macros

• Conditional compilation

• Conditional execution

All directives begin with #• Can only have whitespace before directives

Directives not C statements• Do not end with

#include

Usage

Loading header files

• #include <stdio.h>

Programs with multiple source files

Header file

• Has common declarations and definitions

• structures, enumerations, function prototypes

• Extract commonality of multiple program files

Symbolic constants

#define

• Symbolic constants

• Constants represented as symbols

• When program compiled, all occurrences replaced

Format

• #define identifier replacement-text

• #define MAX_ARRAY_SIZE 30

Symbolic constants

Advantages

• Takes no memory

Disadvantages

• Name not be seen by debugger (only replacement text)

• Do not have specific data type

Preprocessor directives

- Macros

Macro

• Operation specified in #define

• Intended for legacy C programs

• Macro without arguments

• Treated like a symbolic constant

Macro with arguments

• Arguments substituted for replacement text

• Macro expanded

• Performs a text substitution

• No data type checking

Example

#define PI 3.14159

#define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )

area = CIRCLE_AREA( 4 );

becomes

area = ( 3.14159 * ( 4 ) * ( 4 ) );

Preprocessor directives

- Conditional compile

Control preprocessor directives and compilation

Cannot evaluate cast expressions, sizeof, enumeration

#ifdef BOARD_TYPE_1

#define REG_SIZE 32

#else

#define REG_SIZE 64

#endif

Stay connected

About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary focus is on Embedded with diversification focus on Java, Oracle and Android areas

Emertxe Information Technologies,

No-1, 9th Cross, 5th Main,Jayamahal Extension,

Bangalore, Karnataka 560046

T: +91 80 6562 9666E: training@emertxe.com

https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides

THANK YOU

top related