13
C Refresher Team Emertxe Day4

C Programming - Refresher - Part IV

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: C Programming - Refresher - Part IV

C Refresher

Team Emertxe

Day4

Page 2: C Programming - Refresher - Part IV

Compiling a C program

Page 3: C Programming - Refresher - Part IV

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

Page 4: C Programming - Refresher - Part IV

Pre processing and macros

Page 5: C Programming - Refresher - Part IV

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

Page 6: C Programming - Refresher - Part IV

#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

Page 7: C Programming - Refresher - Part IV

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

Page 8: C Programming - Refresher - Part IV

Symbolic constants

Advantages

• Takes no memory

Disadvantages

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

• Do not have specific data type

Page 9: C Programming - Refresher - Part IV

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

Page 10: C Programming - Refresher - Part IV

Example

#define PI 3.14159

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

area = CIRCLE_AREA( 4 );

becomes

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

Page 11: C Programming - Refresher - Part IV

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

Page 12: C Programming - Refresher - Part IV

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: [email protected]

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

Page 13: C Programming - Refresher - Part IV

THANK YOU