0% found this document useful (0 votes)
9 views

Chapter2_Basics of C and C++_part2

Chapter 2 of EE3490E covers the basics of C and C++ programming, including program structure, data types, control flow statements (if, switch, loops), and input/output functions. It provides examples of decision-making and looping constructs, as well as detailed explanations of printf and scanf functions for formatted output and input. The chapter emphasizes the importance of understanding control flow and data handling in embedded programming.

Uploaded by

Lập Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter2_Basics of C and C++_part2

Chapter 2 of EE3490E covers the basics of C and C++ programming, including program structure, data types, control flow statements (if, switch, loops), and input/output functions. It provides examples of decision-making and looping constructs, as well as detailed explanations of printf and scanf functions for formatted output and input. The chapter emphasizes the importance of understanding control flow and data handling in embedded programming.

Uploaded by

Lập Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

EE3490E - Fundamentals of

Embedded Programmings
Chapter 2: Basics of C
and C++
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Automation Engineering, SEEE
© DIA 2021.2
Content
2.1. C/C++ Program Structure
2.2. Variables and Basis Data Types
2.3. Derived Data Types
2.4. User-defined data type
2.5. Control flow: Decision making
2.6. Control flow: Loop
2.7. Input/Output
2.8. Pre-processor

Chapter 2: Basic of C & C++ © DIA 2021.2 2


2.5 Control flow: decision making
 Making decisions (branching statement)
 if .. else: decision statement to select 1 or 2 cases
 switch .. case: decision statement to select multiple
cases
 break: jumping instruction to end (early) a scope
 continue: skips the current iteration of the loop and
continues with the next iteration
 return: jumping instruction and end (early) a function
 goto: jumping to a label (should not use!)

Chapter 2: Basic of C & C++ © DIA 2021.2 3


2.5.1 if .. else
 Decide on one case: use if  Decide on two case: use if .. else
if(npoints >= 60) if(npoints >= 90)
cout<< "Passed"; cout<< ‘A’;
if( npoints>= 80 && else if ( npoints>= 80)
npoints<= 90) cout<< ‘B’;
{ else if (npoints>= 70)
grade = ‘A’; cout<< ‘C’;
cout<< grade; else if (npoints>= 60)
} cout<< ‘D’;
else cout<< ‘F’;

Chapter 2: Basic of C & C++ © DIA 2021.2 4


Example: max() function
int max1(inta, intb) { int max4(int a, int b) {
int c; if(a > b)
if (a > b)c = a; return a;
else c = b; else
return c; return b;
} }
int max2(int a,int b) { int max5(int a, int b) {
int c = a; if (a > b)
if (a < b) return a;
c = b; return b;
return c; }
} int max6(int a, int b) {
int max3(int a, int b) { return(a > b)? a: b;
if (a < b)
}
a = b;
return a;
}
Chapter 2: Basic of C & C++ © DIA 2021.2 5
2.5.2 switch .. case
Signal input;
int i = 0;
while(i++ < 8) {
input = readInput(i); // read from input module i
switch(input.type) {
case BINARY_8:
cout<< input.value.byte; break;
case BINARY_16:
cout<< input.value.word; break;
case ANALOG_1:
cout<< input.value.real; break;
case ANALOG_2:
cout<< input.value.lreal; break;
default:
cout<< "Unknown signal type";
}
}
Chapter 2: Basic of C & C++ © DIA 2021.2 6
2.6 Control flow: loop
 Loop statements in C/C++:
 while (condition) { }
 do { } while (condition)
 for (init; condtion; post_action) { }
 Loop can be performed with if .. else and goto,
however never do that way
 Loop is usually applied to work with arrays and other
data structure access via array variable and index,
pointer or iterator (may be discussed later)

Chapter 2: Basic of C & C++ © DIA 2021.2 7


2.6.1 while
#include <iostream.h>
void main() {
char input[32];
cout << "\nEnter your full name:";
cin.getline(input,31);
short nLetters=0, nSpaces=0;
short i=0;
while (input[i] != 0) {
if (input[i] == ' ')
++nSpaces;
else
++nLetters;
++i;
}
cout << "\nYour name has " << nLetters << " letters";
cout << "\nYou have " << nSpaces -1 << " middle name";
cin >> i;
}
Chapter 2: Basic of C & C++ © DIA 2021.2 8
while and condition statement
#include <iostream.h>
void main() {
char input[32], family_name[16]={0};
cout << "\nEnter your full name:";
cin.getline(input,31);
short i=0;
while (input[i] != 0) {
if (input[i] !=' ') break;
family_name[i]= input[i];
++i;
}
cout << "\nYour family name is" << family_name;
cin >> i;
}

Chapter 2: Basic of C & C++ © DIA 2021.2 9


2.6.2 do while...
#include <iostream.h>
void main() {
char input[32], family_name[16]={0};
short i;
do {
cout << "\nEnter your full name:";
cin.getline(input,31);
i=0;
while(input[i] != 0&& input[i] !=' ') {
family_name[i]= input[i];
++i;
}
cout << "\nYour family name is" << family_name;
cout<< "\nDoyou want to continue? (Y/N):“;
cin >> i;
} while(i == ‘Y’|| i == ‘N’)
}

Chapter 2: Basic of C & C++ © DIA 2021.2 10


2.6.3 for
short i =0; for (short i =0;input[i]!=0; ++i)
while(input[i]!= 0) {
{ if (input[i]==' ')
if (input[i]==' ') ++nSpaces;
++nSpaces; else
else ++nLetters;
++nLetters; ++i;
++i; }
}

short i =0; short i =0;


for (;input[i]!=0;) for (;input[i]!=0; ++i)
{ {
if (input[i]==' ') if (input[i]==' ')
++nSpaces; ++nSpaces;
else else
++nLetters; ++nLetters;
++i; }
}
Chapter 2: Basic of C & C++ © DIA 2021.2 11
Range-based for loop
 Added since C++ 11
 Executes a for loop over a range
for ( range_declaration : range_expression)
loop_statement
 E.g.:
int main() int main()
{ {
// Iterating over whole array // Iterating over array
std::vector<int> v = int a[] =
{0, 1, 2, 3, 4, 5}; {0, 1, 2, 3, 4, 5};
for (auto i : v) for (int n : a)
std::cout << i << ' '; std::cout << n << ' ';
} }
Chapter 2: Basic of C & C++ © DIA 2021.2 12
Loop summary
 while and for structure are similar, only one is needed
 do .. while is a bit different but can be used the same as
while or for
 Loop structures can be nested freely, however one
should avoid to nest too many loops to handle the
program, functions can be used instead
 Loop control can be combined with if .. else and break,
return
 Check loop condition carefully (array index, pointer,
…)
Chapter 2: Basic of C & C++ © DIA 2021.2 13
2.7 Input and Output
 The functions printf and scanf provide output and input
respectively
printf(“control string”, list of expressions);
scanf(“control string”, list of &variables);

 Declared in <stdio.h>
 Control string: a character array defines the format of
input/output string
 Expressions: contain data to be presented
 Variables: variables store input data (may be provided by
users)
 & (ampersand): required symbol
Chapter 2: Basic of C & C++ © DIA 2021.2 14
printf()
int printf ( const char * format, ... );
 A format specifier follows this prototype:
%[flags][width][.precision][length]specifier

 Returning value:
 On success, the total number of characters written is returned
 If a writing error occurs, the error indicator (ferror) is set and a
negative number is returned
 If a multibyte character encoding error occurs while writing
wide characters, errno is set to EILSEQ and a negative number
is returned.

Chapter 2: Basic of C & C++ © DIA 2021.2 15


printf()
%[flags][width][.precision][length]specifier
specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
Nothing printed.
n The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
% A % followed by another % character will write a single % to the stream. %

Chapter 2: Basic of C & C++ © DIA 2021.2 16


printf()
%[flags][width][.precision][length]specifier
flags description
Left-justify within the given field width; Right justification is the default (see width sub-
-
specifier).
Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By
+
default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values
different than zero.
#
Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if
no more digits follow. By default, if no digits follow, no decimal point is written.
Left-pads the number with zeroes (0) instead of spaces when padding is specified
0
(see width sub-specifier).

width description
Minimum number of characters to be printed. If the value to be printed is shorter than this
(number) number, the result is padded with blank spaces. The value is not truncated even if the result is
larger.
The width is not specified in the format string, but as an additional integer value argument
*
Chapter 2: Basic ofpreceding
C & C++ the argument that has to be formatted. © DIA 2021.2 17
printf()
%[flags][width][.precision][length]specifier

.precision description
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value
to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated
even if the result is longer. A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this
.number is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the
ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.
The precision is not specified in the format string, but as an additional integer value argument preceding the
.*
argument that has to be formatted.

Chapter 2: Basic of C & C++ © DIA 2021.2 18


printf()
%[flags][width][.precision][length]specifier
 The length sub-specifier modifies the length of the data type. This is a chart
showing the types used to interpret the corresponding arguments with and
without length specifier (if a different type is used, the proper type promotion
or conversion is performed, if allowed):
specifiers
length di uoxX fFeEgGaA c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char signed char*
unsigned
h short int short int*
short int
unsigned long
l long int wint_t wchar_t* long int*
int
unsigned long
ll long long int long long int*
long int
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L long double
Chapter 2: Basic of C & C++ © DIA 2021.2 19
Examples
%10.2f ____123.55 double
%10.4f __123.5500
%.2f 123.55
%10d _______475 int
%-10d 475_______
%10c _________a char

Chapter 2: Basic of C & C++ © DIA 2021.2 20


Examples
/* printf example */
#include <stdio.h>

int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n",
100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}

Chapter 2: Basic of C & C++ © DIA 2021.2 21


scanf()
scanf(“control string”, &input list);

int numPushups;

printf(“Hello. Do how many pushups? ”);


scanf(“%d”, &numPushups);
printf(“Do %d pushups.\n”, numPushups);

output: Hello. Do how many pushups? 5


Do 5 pushups.
 Variables in the input list must come with the & sign as a prefix

Chapter 2: Basic of C & C++ © DIA 2021.2 22


If you miss &
 The program is compiled successfully but…

Chapter 2: Basic of C & C++ © DIA 2021.2 23


scanf()
int scanf ( const char * format, ... );
 A format specifier for scanf follows this prototype:
%[*][width][length]specifier
 Returning value:
 On success, the function returns the number of items of the
argument list successfully filled
 If a reading error happens or the end-of-file is reached while
reading, the proper indicator is set (feof or ferror). And, if
either happens before any data could be successfully read, EOF
is returned.
 If an encoding error happens interpreting wide characters, the
function sets errno to EILSEQ.

Chapter 2: Basic of C & C++ © DIA 2021.2 24


scanf()
%[*][width][length]specifier
specifier Description Characters extracted
Any number of digits, optionally preceded by a sign (+ or -).
i Integer Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
Signed argument.
Decimal Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
d or u
integer d is for a signed argument, and u for an unsigned.
Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
o Octal integer
Unsigned argument.
Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign
Hexadecimal
x (+ or -).
integer
Unsigned argument.
f, e, g A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally
Floating point
followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod).
a number
Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the
c Character
successive locations of the array passed as argument. No null character is appended at the end.
String of Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character
s
characters is automatically added at the end of the stored sequence.
Pointer A sequence of characters representing a pointer. The particular format used depends on the system and library
p
address implementation, but it is the same as the one used to format %p in fprintf.
Any number of the characters specified between the brackets.
[characters] Scanset
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
Negated
[^characters] Any number of characters none of them specified as characters between the brackets.
scanset
No input is consumed.
n Count
The number of characters read so far from stdin is stored in the pointed location.
% % A % followed by another % matches a single %.
Chapter 2: Basic of C & C++ © DIA 2021.2 25
scanf()
%[*][width][length]specifier

 The format specifier can also contain sub-


specifiers: asterisk (*), width and length (in that order),
which are optional and follow these specifications:
sub-specifier description
An optional starting asterisk indicates that the data is to be read from the
* stream but ignored (i.e. it is not stored in the location pointed by an
argument).
Specifies the maximum number of characters to be read in the current
width
reading operation (optional).
One of hh, h, l, ll, j, z, t, L (optional).
length This alters the expected type of the storage pointed by the corresponding
argument (see below).

Chapter 2: Basic of C & C++ © DIA 2021.2 26


scanf()
%[*][width][length]specifier

specifiers

length di uox fega c s [] [^] p n


(none) int* unsigned int* float* char* void** int*

hh signed char* unsigned char* signed char*


unsigned short
h short int* short int*
int*
unsigned long
l long int* double* wchar_t* long int*
int*
unsigned long
ll long long int* long long int*
long int*
j intmax_t* uintmax_t* intmax_t*
z size_t* size_t* size_t*
t ptrdiff_t* ptrdiff_t* ptrdiff_t*
L long double*

Chapter 2: Basic of C & C++ © DIA 2021.2 27


Common errors with Input/Ouput
 Assume that the following statement is used
scanf("%lf", &fahrenheit);
 The string “comfortable” is entered as an input
 This input is not read by the program, and thus fahrenheit is
not initialized
 A number of errors will occur as results of the above issue
 This is a mistake of the programmer, not the user

Chapter 2: Basic of C & C++ © DIA 2021.2 28


Consequence

Chapter 2: Basic of C & C++ © DIA 2021.2 29


What to do?
 Function scanf() return the input variable which has
been read successfully

int scanfCount;
scanfCount = scanf(“%d”, &studentID);
/* if scanfCount is not equal to 1 at this point,
the user has made some kind of mistake.
Handle it. */

Chapter 2: Basic of C & C++ © DIA 2021.2 30


Example
/* scanf example */
#include <stdio.h>

int main ()
{
char str [80];
int i;

printf ("Enter your family name: ");


scanf ("%79s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i);
printf ("Enter a hexadecimal number: ");
scanf ("%x",&i);
printf ("You have entered %#x (%d).\n",i,i);

return 0;
}

Chapter 2: Basic of C & C++ © DIA 2021.2 31


Pause at a Breakpoint

Chapter 2: Basic of C & C++ © DIA 2021.2 32


assert()

It’s a must!

Chapter 2: Basic of C & C++ © DIA 2021.2 33


Input/Output summary
printf(“control string”, output list);
 control string : data type and desired format
 output list : operations, values to be written (on the screen)
scanf(“control string”, &input list);
 control string: variables, values to be read
 input list: data type and desired format
 Can be used to initialized variables
 Attention:
 do not use & with printf(),
 must use & with scanf()
 In these two statements: conversion specification in the
control string must be compatible with input/output stream
in terms of quantity, order and data type

Chapter 2: Basic of C & C++ © DIA 2021.2 34


2.8. Preprocessor
 Preprocessor statements appear before the program is
compiled
 It is not a part of the compiler, but is a separate step in the
compilation process
 Tasks to do:
 Combine other files into the file being compiled
 Define constant, macro
 Compile the program with conditions
 Implement precompile statements with conditions
 Precompile statements start with # (hash)
 Only space characters can be put in front of pre-processor statements
in that line

Chapter 2: Basic of C & C++ © DIA 2021.2 35


Preprocessor
 A preprocessor is effective within the file where it is
defined
 A preprocessor is not the basic syntax of C
 However, its utilization may change the program
structure
 All preprocessor commands begin with a hash symbol
(#)
 Two most frequently used features are:
 #include
 #define
Chapter 2: Basic of C & C++ © DIA 2021.2 36
2.8.1 File Inclusion: #include
 #include is usually used with header files, e.g.:
#include <standard.h> searching follows an
implementation-defined rule to find the file
#include “myheader.h” searching for the file typically
begins where the source program was found

 These source lines are replaced by the content of the


file, i.e. standard.h or myheader.h

Chapter 2: Basic of C & C++ © DIA 2021.2 37


2.8.2 Macro Substitution
 Keyword #define can be used to define
 Constants
 Macros

Chapter 2: Basic of C & C++ © DIA 2021.2 38


Constant definition
 It is used to avoid “magic numbers \ hard code” in the
program
 Common form
#define name text
 It calls for a macro substitution of the simplest kind -
subsequent occurrences of the token name will be
replaced by the text before the program is compiled
 The name in a #define has the same form as a variable
name; the replacement text is arbitrary

Trang
Chapter 2: Basic of C & C++ © DIA 2021.2
39
Constant definition
#define BUFFERSIZE 256
#define MIN_VALUE -32
#define PI 3.14159
 Attention:
 There are neither = nor ; in the line
 Name defined by #define can be removed by using #undef
(it may be redefined later)
 Substitutions are made only for tokens, and do not take place
within quoted strings, e.g. there is no substitution for PI in
printf("PI")
 UPPERCASE WORD is used as the constant name in
order to differentiate with variable and function names
Chapter 2: Basic of C & C++ © DIA 2021.2 40
#define, const and enum
 It is possible to replace #define with constant
or enum
#define ARRAYSIZE 10
const int ArraySize = 10;
double array[ARRAYSIZE]; /* Valid. */

#define PI 3.14159
#define ARRAYSIZE 10
const double Pi = 3.14159; /* Preferred */
enum {ARRAYSIZE = 10}; /* Preferred */

Chapter 2: Basic of C & C++ © DIA 2021.2 41


Macro definition
 #define is usually used to create macros
#define max(x,y) ((x)>(y) ? (x) : (y))
 Macro looks like a function call, however it is not really
a function. Macro name (max) is replaced by statement
with respect to operands before the program is
compiled
int a = 4, b = -7, c;
c = MAX(a,b);
is replaced by:
c = ((a)>(b) ? (a) : (b));

Chapter 2: Basic of C & C++ © DIA 2021.2 42


Why to used Macro
 Speed
 Implementation as a function, the code is copied to the program
before being compiled
 It is not really effective with program running on PC
 Common code
 Macro enable to work with all kinds of data type (int,
double…)
int max(int x, int y) {
return x > y ? x : y;
}

Chapter 2: Basic of C & C++ © DIA 2021.2 43


Macro examples
#define SQR(x) ((x)*(x))
#define SGN(x) (((x)<0) ? -1 : 1)
#define ABS(x) (((x)<0) ? -(x) : (x))
#define ISDIGIT(x) ((x) >= '0' && (x) <= '9‘)
#define NELEMS(array)
sizeof(array)/sizeof(array[0]))

#define CLAMP(val,low,high) \
((val)<(low) ? (low) : (val) > (high) ? (high) :
(val))

#define ROUND(val) \
((val)>0 ? (int)((val)+0.5) : -(int)(0.5-(val)))

Chapter 2: Basic of C & C++ © DIA 2021.2 44


Disadvantages of Macro
 Be careful with some pitfalls when using macro.
 Inappropriate usage of parentheses
 Side effect of ++, -- operator
 No data type verification

Chapter 2: Basic of C & C++ © DIA 2021.2 45


Macro pitfalls
#define SQR(x) x * x
 Example:
int a = 7;
b = SQR(a+1);
will be replaced by
b = a+1 * a+1;
 Solution: use parentheses to avoid the confusion

Chapter 2: Basic of C & C++ © DIA 2021.2 46


Macro pitfalls
 Example
b = ABS(a++);
would become
b = (((a++)<0) ? -(a++) : (a++));
 Solution: do not use these operators in the macro

Chapter 2: Basic of C & C++ © DIA 2021.2 47


Macro pitfalls
 Inconsistent use of data type would result in wrong
calculation
int a = 7, b;
double c = 5.3, d;
d = SQR(a);
b = SQR(c);

Chapter 2: Basic of C & C++ © DIA 2021.2 48


Long Macro statement
 If the macro line is too long, it should be broken into
multiple line by using \
#define ERROR(condition, message) \
if (condition) printf(message)

Chapter 2: Basic of C & C++ © DIA 2021.2 49


Example
#define TIMELOOP(CODE) { \
t0 = clock(); \
for (i = 0; i < n; ++i) { CODE; } \
printf("%7d ", clock() - t0); \
}
 Its usage would be:
TIMELOOP(y = sin(x));

Chapter 2: Basic of C & C++ © DIA 2021.2 50


Constant character array in Macro
 If the operand of macro has # in front, it will be
replaced by a constant character array
#define PRINT_DEBUG(expr) \
printf(#expr " = %g\n", expr)
Example:
PRINT_DEBUG(x/y);
printf("x/y" " = %g\n", x/y);

Chapter 2: Basic of C & C++ © DIA 2021.2 51


Pre-defined Macros
 Pre-defined macros in compiler

__LINE__ contains the current line number of the


program in the compilation. It gives the line
number where it is called.
__FILE__ holds the file name of the currently executing
program in the computer

__DATE__ gives the date at which source code of this


program is converted into object code

__TIME__ gives the time at which program was compiled


__STDC__ is used to confirm the compiler standard
Chapter 2: Basic of C & C++ © DIA 2021.2 52
Example
#define PRINT_DEBUG(expr, type) \
printf(__FILE__ "[%d](" #expr "): \
%" type##Conv "\n", __LINE__,
(expr))
 The following codes
#define intConv “d”
#define doubleConv “f”
PRINT_DEBUG(x/y, int);
print out the filename, line numbers in order, and results
Chapter 2: Basic of C & C++ © DIA 2021.2 53
2.8.3 Conditional inclusion
 Conditional preprocessor statements in C
#if, #elif, #else, #endif
#ifdef, #ifndef
 Goals
 To add debugging codes in the program
 To add the code which is not standard
 To avoid insert header files multiple times

Chapter 2: Basic of C & C++ © DIA 2021.2 54


Debugging
 Compile the program in debug mode
#define DEBUG
 Debugging code can be inserted
#ifdef DEBUG
printf("Pointer %#x points
to value %f", pd, *pd);
#endif

Chapter 2: Basic of C & C++ © DIA 2021.2 55


Non-standard code
 Applied in the case that codes are used for different
microprocessors
#ifdef __WIN32__
return WaitForSingleObject(Handle,0)==

WAIT_OBJECT_0;
#elif defined(__QNX__)||defined(__linux__)
if(flock(fd,LOCK_EX|LOCK_NB) == -1)
return 0;
else
return 1;
#endif
Chapter 2: Basic of C & C++ © DIA 2021.2 56
Non-standard code
 Applied in the case that codes are used for different microprocessors
#if defined(ARDUINO) && ((ARDUINO) >= 100)
//arduino core v1.0 or later
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#if defined(__AVR__)
#include <avr/pgmspace.h>
//use for PROGMEM Arduino AVR
#elif defined(ESP8266)
#include <pgmspace.h>
//use for PROGMEM Arduino ESP8266
#elif defined(_VARIANT_ARDUINO_STM32_)
#include <avr/pgmspace.h>
//use for PROGMEM Arduino STM32
#endif

Chapter 2: Basic of C & C++ © DIA 2021.2 57


One time inclusion of header files
 A header file should be included only once (although a
few file may use it)
 Duplicated inclusion will result in repeated definition of
variables, functions, labels, and thus the program
cannot be compiled successfully
 Solution: using “header guards”
#ifndef A_HEADER_H_
#define A_HEADER_H_

/* Contents of header file is here. */


#endif
Chapter 2: Basic of C & C++ © DIA 2021.2 58
End of Chapter 2

Chapter 2: Basic of C & C++ © DIA 2021.2 59

You might also like