CONTENTS CONTENTS
C/C++ programming language notes
,CONTENTS CONTENTS
Contents
Preface iii
0.1 Target audience ..................................................................................................................................................... iii
0.2 Thanks .................................................................................................................................................................... iii
1 Common for C and C++ 1
1.1 Header files...................................................................................................................................................................... 1
1.2 Definitions and declarations ............................................................................................................................................ 1
1.2.1 C/C++ declarations .............................................................................................................................................. 1
1.2.2 Definitions .......................................................................................................................................................... 4
1.3 Language elements ................................................................................................................................................. 5
1.3.1 Comments........................................................................................................................................................... 5
1.3.2 goto ............................................................................................................................................................. 6
1.3.3 for ....................................................................................................................................................................... 7
1.3.4 if .......................................................................................................................................................................... 8
1.3.5 switch .......................................................................................................................................................... 9
1.3.6 sizeof ................................................................................................................................................................. 10
1.3.7 Pointers ............................................................................................................................................................. 10
1.3.8 Operators .......................................................................................................................................................... 13
1.3.9 Arrays ................................................................................................................................................................ 14
1.3.10 struct................................................................................................................................................................. 15
1.3.11 union ................................................................................................................................................................. 16
1.4 Preprocessor ................................................................................................................................................................. 18
1.5 Standard values for compilers and OS1 .......................................................................................................................................................................... 18
1.5.1 More standard preprocessor macros ............................................................................................................... 19
1.5.2 “Empty” macro ................................................................................................................................................. 19
1.5.3 Frequent mistakes ............................................................................................................................................ 19
1.6 Compiler warnings ................................................................................................................................................ 20
1.6.1 Example #1........................................................................................................................................................ 20
1.6.2 Example #2........................................................................................................................................................ 21
1.7 Threads .......................................................................................................................................................................... 22
1.8 main() function .............................................................................................................................................................. 22
1.9 The difference between stdout/cout and stderr/cerr ................................................................................................... 22
1.10 Outdated features ......................................................................................................................................................... 23
1.10.1 register .............................................................................................................................................................. 23
2 C 24
2.1 Memory in C .................................................................................................................................................................. 24
2.1.1 Local stack ................................................................................................................................................. 24
2.1.2 alloca() .............................................................................................................................................................. 24
2.1.3 Allocating memory in heap ....................................................................................................................... 24
2.1.4 Local stack or heap? .......................................................................................................................................... 27
2.2 Strings in C............................................................................................................................................................. 28
2.2.1 String length storage ................................................................................................................................. 29
2.2.2 String returning ................................................................................................................................................. 31
2.2.3 1: Constant string returning .............................................................................................................................. 31
2.2.4 2: Via global array of characters........................................................................................................................ 31
2.2.5 Standard string C functions ....................................................................................................................... 32
2.2.6 Unicode ............................................................................................................................................................. 35
1Operating System
i
2.2.7 Lists of strings............................................................................................................................................ 35
2.3 Your own data structures in C ............................................................................................................................... 35
2.3.1 Lists in C .................................................................................................................................................... 35
2.3.2 Binary trees in C ........................................................................................................................................ 37
,CONTENTS CONTENTS
2.3.3 One more thing ................................................................................................................................................. 37
2.4 Object-oriented programming in C ....................................................................................................................... 38
2.4.1 Structures initialization ............................................................................................................................. 38
2.4.2 Structures deinitialization ......................................................................................................................... 38
2.4.3 Structures copying .................................................................................................................................... 38
2.4.4 Encapsulation ........................................................................................................................................... 38
2.5 C standard library .................................................................................................................................................. 39
2.5.1 assert ................................................................................................................................................................ 39
2.5.2 UNIX time .......................................................................................................................................................... 40
2.5.3 memcpy() .................................................................................................................................................. 40
2.5.4 bzero() and memset() ............................................................................................................................... 40
2.5.5 printf() ....................................................................................................................................................... 40
2.5.6 atexit() .............................................................................................................................................................. 43
2.5.7 bsearch(), lfind()........................................................................................................................................ 44
2.5.8 setjmp(), longjmp() ................................................................................................................................... 46
2.5.9 stdarg.h ..................................................................................................................................................... 46
2.5.10 srand() and rand() ............................................................................................................................................. 47
2.6 C99 C standard ...................................................................................................................................................... 47
3 C++ 48
3.1 Name mangling ..................................................................................................................................................... 48
3.2 C++ declarations ................................................................................................................................................... 48
3.2.1 C++11: auto....................................................................................................................................................... 48
3.3 C++ language elements ......................................................................................................................................... 49
3.3.1 references ......................................................................................................................................................... 49
3.4 Input/output.................................................................................................................................................................. 49
3.5 Templates ............................................................................................................................................................. 50
3.6 Standard Template Library .................................................................................................................................... 50
3.7 Criticism ................................................................................................................................................................ 50
4 Other 51
4.1 Error codes returning .................................................................................................................................................... 51
4.1.1 Negative error codes......................................................................................................................................... 51
4.2 Global variables ............................................................................................................................................................. 52
4.3 Bit fields ................................................................................................................................................................ 54
4.4 Interesting open-source projects worth for learning .................................................................................................... 55
4.4.1 C ........................................................................................................................................................................ 55
4.4.2 C++ .................................................................................................................................................................... 55
5 GNU tools 56
5.1 gcov ....................................................................................................................................................................... 56
6 Testing 58
Afterword 59
6.1 Questions?..................................................................................................................................................................... 59
Acronyms used 60
Bibliography 61
Glossary 62
Index 63
, 1.2. DEFINITIONS AND DECLARATIONS CHAPTER 1. COMMON FOR C AND C++
Chapter 1
Common for C and C++
1.1 Header files
Header files are an interface description, a documentation in some sense. It is very convenient to work with the code in one
editor window while having header files in another window for reference. It can be said it is advisable to make header files
looks like references.
1.2 Definitions and declarations
The difference between declarations and definitions is:
∙ Declaration declares name/type of variable or name and argument types and also returning value type of function or
method. Declarations are usually enlisted in the header .h or .hpp-files, so the compiler while compiling individual .c
or .cpp-file may have an access to the information about all names and types of external (usually global) variables and
functions/methods.
Data types are also declared.
∙ Definition defines a value of (usually global) variable or a body of function/method. It is usually take place in the indi-
vidual .c or .cpp-file.
1.2.1 C/C++ declarations
Local variable declarations
It was possible to declare variables only at the function beginning in C. And anywhere in C++.
It was also not possible to declare counter or Iterator in for() (it was possible in C++):
The new C99(2.5.10) standard allows this.
static If the global variables (or functions) are declared as static, its scope is limited by current file. However, local variables
inside a function may also be declared as static, then this variable will be global instead of local, but its scope will be limited by
a function.
For example:
4
C/C++ programming language notes
,CONTENTS CONTENTS
Contents
Preface iii
0.1 Target audience ..................................................................................................................................................... iii
0.2 Thanks .................................................................................................................................................................... iii
1 Common for C and C++ 1
1.1 Header files...................................................................................................................................................................... 1
1.2 Definitions and declarations ............................................................................................................................................ 1
1.2.1 C/C++ declarations .............................................................................................................................................. 1
1.2.2 Definitions .......................................................................................................................................................... 4
1.3 Language elements ................................................................................................................................................. 5
1.3.1 Comments........................................................................................................................................................... 5
1.3.2 goto ............................................................................................................................................................. 6
1.3.3 for ....................................................................................................................................................................... 7
1.3.4 if .......................................................................................................................................................................... 8
1.3.5 switch .......................................................................................................................................................... 9
1.3.6 sizeof ................................................................................................................................................................. 10
1.3.7 Pointers ............................................................................................................................................................. 10
1.3.8 Operators .......................................................................................................................................................... 13
1.3.9 Arrays ................................................................................................................................................................ 14
1.3.10 struct................................................................................................................................................................. 15
1.3.11 union ................................................................................................................................................................. 16
1.4 Preprocessor ................................................................................................................................................................. 18
1.5 Standard values for compilers and OS1 .......................................................................................................................................................................... 18
1.5.1 More standard preprocessor macros ............................................................................................................... 19
1.5.2 “Empty” macro ................................................................................................................................................. 19
1.5.3 Frequent mistakes ............................................................................................................................................ 19
1.6 Compiler warnings ................................................................................................................................................ 20
1.6.1 Example #1........................................................................................................................................................ 20
1.6.2 Example #2........................................................................................................................................................ 21
1.7 Threads .......................................................................................................................................................................... 22
1.8 main() function .............................................................................................................................................................. 22
1.9 The difference between stdout/cout and stderr/cerr ................................................................................................... 22
1.10 Outdated features ......................................................................................................................................................... 23
1.10.1 register .............................................................................................................................................................. 23
2 C 24
2.1 Memory in C .................................................................................................................................................................. 24
2.1.1 Local stack ................................................................................................................................................. 24
2.1.2 alloca() .............................................................................................................................................................. 24
2.1.3 Allocating memory in heap ....................................................................................................................... 24
2.1.4 Local stack or heap? .......................................................................................................................................... 27
2.2 Strings in C............................................................................................................................................................. 28
2.2.1 String length storage ................................................................................................................................. 29
2.2.2 String returning ................................................................................................................................................. 31
2.2.3 1: Constant string returning .............................................................................................................................. 31
2.2.4 2: Via global array of characters........................................................................................................................ 31
2.2.5 Standard string C functions ....................................................................................................................... 32
2.2.6 Unicode ............................................................................................................................................................. 35
1Operating System
i
2.2.7 Lists of strings............................................................................................................................................ 35
2.3 Your own data structures in C ............................................................................................................................... 35
2.3.1 Lists in C .................................................................................................................................................... 35
2.3.2 Binary trees in C ........................................................................................................................................ 37
,CONTENTS CONTENTS
2.3.3 One more thing ................................................................................................................................................. 37
2.4 Object-oriented programming in C ....................................................................................................................... 38
2.4.1 Structures initialization ............................................................................................................................. 38
2.4.2 Structures deinitialization ......................................................................................................................... 38
2.4.3 Structures copying .................................................................................................................................... 38
2.4.4 Encapsulation ........................................................................................................................................... 38
2.5 C standard library .................................................................................................................................................. 39
2.5.1 assert ................................................................................................................................................................ 39
2.5.2 UNIX time .......................................................................................................................................................... 40
2.5.3 memcpy() .................................................................................................................................................. 40
2.5.4 bzero() and memset() ............................................................................................................................... 40
2.5.5 printf() ....................................................................................................................................................... 40
2.5.6 atexit() .............................................................................................................................................................. 43
2.5.7 bsearch(), lfind()........................................................................................................................................ 44
2.5.8 setjmp(), longjmp() ................................................................................................................................... 46
2.5.9 stdarg.h ..................................................................................................................................................... 46
2.5.10 srand() and rand() ............................................................................................................................................. 47
2.6 C99 C standard ...................................................................................................................................................... 47
3 C++ 48
3.1 Name mangling ..................................................................................................................................................... 48
3.2 C++ declarations ................................................................................................................................................... 48
3.2.1 C++11: auto....................................................................................................................................................... 48
3.3 C++ language elements ......................................................................................................................................... 49
3.3.1 references ......................................................................................................................................................... 49
3.4 Input/output.................................................................................................................................................................. 49
3.5 Templates ............................................................................................................................................................. 50
3.6 Standard Template Library .................................................................................................................................... 50
3.7 Criticism ................................................................................................................................................................ 50
4 Other 51
4.1 Error codes returning .................................................................................................................................................... 51
4.1.1 Negative error codes......................................................................................................................................... 51
4.2 Global variables ............................................................................................................................................................. 52
4.3 Bit fields ................................................................................................................................................................ 54
4.4 Interesting open-source projects worth for learning .................................................................................................... 55
4.4.1 C ........................................................................................................................................................................ 55
4.4.2 C++ .................................................................................................................................................................... 55
5 GNU tools 56
5.1 gcov ....................................................................................................................................................................... 56
6 Testing 58
Afterword 59
6.1 Questions?..................................................................................................................................................................... 59
Acronyms used 60
Bibliography 61
Glossary 62
Index 63
, 1.2. DEFINITIONS AND DECLARATIONS CHAPTER 1. COMMON FOR C AND C++
Chapter 1
Common for C and C++
1.1 Header files
Header files are an interface description, a documentation in some sense. It is very convenient to work with the code in one
editor window while having header files in another window for reference. It can be said it is advisable to make header files
looks like references.
1.2 Definitions and declarations
The difference between declarations and definitions is:
∙ Declaration declares name/type of variable or name and argument types and also returning value type of function or
method. Declarations are usually enlisted in the header .h or .hpp-files, so the compiler while compiling individual .c
or .cpp-file may have an access to the information about all names and types of external (usually global) variables and
functions/methods.
Data types are also declared.
∙ Definition defines a value of (usually global) variable or a body of function/method. It is usually take place in the indi-
vidual .c or .cpp-file.
1.2.1 C/C++ declarations
Local variable declarations
It was possible to declare variables only at the function beginning in C. And anywhere in C++.
It was also not possible to declare counter or Iterator in for() (it was possible in C++):
The new C99(2.5.10) standard allows this.
static If the global variables (or functions) are declared as static, its scope is limited by current file. However, local variables
inside a function may also be declared as static, then this variable will be global instead of local, but its scope will be limited by
a function.
For example:
4