C Notes For Professionals. 300+ pages of professional hints and tricks (GoalKicker.com) (Z-Library)

Author: GoalKicker.com

非小说

No Description

📄 File Format: PDF
💾 File Size: 2.4 MB
42
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
C Notes for ProfessionalsC Notes for Professionals GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial C group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners 300+ pages of professional hints and tricks
📄 Page 2
Contents About 1 ................................................................................................................................................................................... Chapter 1: Getting started with C Language 2 .................................................................................................. Section 1.1: Hello World 2 ................................................................................................................................................. Section 1.2: Original "Hello, World!" in K&R C 4 .............................................................................................................. Chapter 2: Comments 6 ................................................................................................................................................. Section 2.1: Commenting using the preprocessor 6 ...................................................................................................... Section 2.2: /* */ delimited comments 6 ........................................................................................................................ Section 2.3: // delimited comments 7 ............................................................................................................................ Section 2.4: Possible pitfall due to trigraphs 7 .............................................................................................................. Chapter 3: Data Types 9 ............................................................................................................................................... Section 3.1: Interpreting Declarations 9 .......................................................................................................................... Section 3.2: Fixed Width Integer Types (since C99) 11 ................................................................................................. Section 3.3: Integer types and constants 11 .................................................................................................................. Section 3.4: Floating Point Constants 12 ........................................................................................................................ Section 3.5: String Literals 13 .......................................................................................................................................... Chapter 4: Operators 14 ............................................................................................................................................... Section 4.1: Relational Operators 14 ............................................................................................................................... Section 4.2: Conditional Operator/Ternary Operator 15 ............................................................................................. Section 4.3: Bitwise Operators 16 ................................................................................................................................... Section 4.4: Short circuit behavior of logical operators 18 .......................................................................................... Section 4.5: Comma Operator 19 ................................................................................................................................... Section 4.6: Arithmetic Operators 19 .............................................................................................................................. Section 4.7: Access Operators 22 ................................................................................................................................... Section 4.8: sizeof Operator 24 ....................................................................................................................................... Section 4.9: Cast Operator 24 ......................................................................................................................................... Section 4.10: Function Call Operator 24 ......................................................................................................................... Section 4.11: Increment / Decrement 25 ......................................................................................................................... Section 4.12: Assignment Operators 25 .......................................................................................................................... Section 4.13: Logical Operators 26 .................................................................................................................................. Section 4.14: Pointer Arithmetic 27 .................................................................................................................................. Section 4.15: _Alignof 28 .................................................................................................................................................. Chapter 5: Boolean 30 .................................................................................................................................................... Section 5.1: Using stdbool.h 30 ........................................................................................................................................ Section 5.2: Using #define 30 .......................................................................................................................................... Section 5.3: Using the Intrinsic (built-in) Type _Bool 31 ............................................................................................... Section 5.4: Integers and pointers in Boolean expressions 31 .................................................................................... Section 5.5: Defining a bool type using typedef 32 ...................................................................................................... Chapter 6: Strings 33 ....................................................................................................................................................... Section 6.1: Tokenisation: strtok(), strtok_r() and strtok_s() 33 .................................................................................. Section 6.2: String literals 35 ........................................................................................................................................... Section 6.3: Calculate the Length: strlen() 36 ................................................................................................................ Section 6.4: Basic introduction to strings 37 .................................................................................................................. Section 6.5: Copying strings 37 ....................................................................................................................................... Section 6.6: Iterating Over the Characters in a String 40 ............................................................................................. Section 6.7: Creating Arrays of Strings 41 ..................................................................................................................... Section 6.8: Convert Strings to Number: atoi(), atof() (dangerous, don't use them) 41 ........................................... Section 6.9: string formatted data read/write 42 .........................................................................................................
📄 Page 3
Section 6.10: Find first/last occurrence of a specific character: strchr(), strrchr() 43 ............................................... Section 6.11: Copy and Concatenation: strcpy(), strcat() 44 ........................................................................................ Section 6.12: Comparsion: strcmp(), strncmp(), strcasecmp(), strncasecmp() 45 .................................................... Section 6.13: Safely convert Strings to Number: strtoX functions 47 .......................................................................... Section 6.14: strspn and strcspn 48 ................................................................................................................................. Chapter 7: Literals for numbers, characters and strings 50 ...................................................................... Section 7.1: Floating point literals 50 ............................................................................................................................... Section 7.2: String literals 50 ........................................................................................................................................... Section 7.3: Character literals 50 .................................................................................................................................... Section 7.4: Integer literals 51 ......................................................................................................................................... Chapter 8: Compound Literals 53 ............................................................................................................................. Section 8.1: Definition/Initialisation of Compound Literals 53 ...................................................................................... Chapter 9: Bit-fields 55 .................................................................................................................................................. Section 9.1: Bit-fields 55 .................................................................................................................................................... Section 9.2: Using bit-fields as small integers 56 .......................................................................................................... Section 9.3: Bit-field alignment 56 .................................................................................................................................. Section 9.4: Don'ts for bit-fields 57 ................................................................................................................................. Section 9.5: When are bit-fields useful? 58 .................................................................................................................... Chapter 10: Arrays 60 ...................................................................................................................................................... Section 10.1: Declaring and initializing an array 60 ....................................................................................................... Section 10.2: Iterating through an array eciently and row-major order 61 ............................................................ Section 10.3: Array length 62 ........................................................................................................................................... Section 10.4: Passing multidimensional arrays to a function 63 ................................................................................. Section 10.5: Multi-dimensional arrays 64 ...................................................................................................................... Section 10.6: Define array and access array element 67 ............................................................................................. Section 10.7: Clearing array contents (zeroing) 67 ....................................................................................................... Section 10.8: Setting values in arrays 68 ........................................................................................................................ Section 10.9: Allocate and zero-initialize an array with user defined size 68 ............................................................. Section 10.10: Iterating through an array using pointers 69 ........................................................................................ Chapter 11: Linked lists 71 ............................................................................................................................................. Section 11.1: A doubly linked list 71 .................................................................................................................................. Section 11.2: Reversing a linked list 73 ............................................................................................................................ Section 11.3: Inserting a node at the nth position 75 ..................................................................................................... Section 11.4: Inserting a node at the beginning of a singly linked list 76 .................................................................... Chapter 12: Enumerations 79 ...................................................................................................................................... Section 12.1: Simple Enumeration 79 ............................................................................................................................... Section 12.2: enumeration constant without typename 80 .......................................................................................... Section 12.3: Enumeration with duplicate value 80 ....................................................................................................... Section 12.4: Typedef enum 81 ....................................................................................................................................... Chapter 13: Structs 83 ..................................................................................................................................................... Section 13.1: Flexible Array Members 83 ......................................................................................................................... Section 13.2: Typedef Structs 85 ..................................................................................................................................... Section 13.3: Pointers to structs 86 .................................................................................................................................. Section 13.4: Passing structs to functions 88 ................................................................................................................. Section 13.5: Object-based programming using structs 89 ......................................................................................... Section 13.6: Simple data structures 91 .......................................................................................................................... Chapter 14: Standard Math 93 ................................................................................................................................... Section 14.1: Power functions - pow(), powf(), powl() 93 .............................................................................................. Section 14.2: Double precision floating-point remainder: fmod() 94 ..........................................................................
📄 Page 4
Section 14.3: Single precision and long double precision floating-point remainder: fmodf(), fmodl() 94 ............... Chapter 15: Iteration Statements/Loops: for, while, do-while 96 ............................................................ Section 15.1: For loop 96 ................................................................................................................................................... Section 15.2: Loop Unrolling and Du's Device 96 ........................................................................................................ Section 15.3: While loop 97 ............................................................................................................................................... Section 15.4: Do-While loop 97 ........................................................................................................................................ Section 15.5: Structure and flow of control in a for loop 98 ......................................................................................... Section 15.6: Infinite Loops 99 .......................................................................................................................................... Chapter 16: Selection Statements 100 .................................................................................................................... Section 16.1: if () Statements 100 ..................................................................................................................................... Section 16.2: Nested if()...else VS if()..else Ladder 100 .................................................................................................. Section 16.3: switch () Statements 102 ........................................................................................................................... Section 16.4: if () ... else statements and syntax 104 ..................................................................................................... Section 16.5: if()...else Ladder Chaining two or more if () ... else statements 104 ....................................................... Chapter 17: Initialization 105 ........................................................................................................................................ Section 17.1: Initialization of Variables in C 105 .............................................................................................................. Section 17.2: Using designated initializers 106 ............................................................................................................... Section 17.3: Initializing structures and arrays of structures 108 ................................................................................ Chapter 18: Declaration vs Definition 110 ............................................................................................................ Section 18.1: Understanding Declaration and Definition 110 ....................................................................................... Chapter 19: Command-line arguments 111 ......................................................................................................... Section 19.1: Print the arguments to a program and convert to integer values 111 ................................................. Section 19.2: Printing the command line arguments 111 ............................................................................................. Section 19.3: Using GNU getopt tools 112 ...................................................................................................................... Chapter 20: Files and I/O streams 115 .................................................................................................................. Section 20.1: Open and write to file 115 ......................................................................................................................... Section 20.2: Run process 116 ........................................................................................................................................ Section 20.3: fprintf 116 ................................................................................................................................................... Section 20.4: Get lines from a file using getline() 116 .................................................................................................. Section 20.5: fscanf() 120 ................................................................................................................................................ Section 20.6: Read lines from a file 121 ......................................................................................................................... Section 20.7: Open and write to a binary file 122 ......................................................................................................... Chapter 21: Formatted Input/Output 124 ............................................................................................................. Section 21.1: Conversion Specifiers for printing 124 ...................................................................................................... Section 21.2: The printf() Function 125 ........................................................................................................................... Section 21.3: Printing format flags 125 ........................................................................................................................... Section 21.4: Printing the Value of a Pointer to an Object 126 .................................................................................... Section 21.5: Printing the Dierence of the Values of two Pointers to an Object 127 ............................................... Section 21.6: Length modifiers 128 ................................................................................................................................. Chapter 22: Pointers 129 ................................................................................................................................................ Section 22.1: Introduction 129 ......................................................................................................................................... Section 22.2: Common errors 131 .................................................................................................................................. Section 22.3: Dereferencing a Pointer 134 .................................................................................................................... Section 22.4: Dereferencing a Pointer to a struct 134 .................................................................................................. Section 22.5: Const Pointers 135 ..................................................................................................................................... Section 22.6: Function pointers 138 ................................................................................................................................ Section 22.7: Polymorphic behaviour with void pointers 139 ...................................................................................... Section 22.8: Address-of Operator ( & ) 140 ................................................................................................................. Section 22.9: Initializing Pointers 140 .............................................................................................................................
📄 Page 5
Section 22.10: Pointer to Pointer 141 .............................................................................................................................. Section 22.11: void* pointers as arguments and return values to standard functions 141 ....................................... Section 22.12: Same Asterisk, Dierent Meanings 142 ................................................................................................. Chapter 23: Sequence points 144 .............................................................................................................................. Section 23.1: Unsequenced expressions 144 .................................................................................................................. Section 23.2: Sequenced expressions 144 ..................................................................................................................... Section 23.3: Indeterminately sequenced expressions 145 ......................................................................................... Chapter 24: Function Pointers 146 ........................................................................................................................... Section 24.1: Introduction 146 .......................................................................................................................................... Section 24.2: Returning Function Pointers from a Function 146 ................................................................................. Section 24.3: Best Practices 147 ..................................................................................................................................... Section 24.4: Assigning a Function Pointer 149 ............................................................................................................. Section 24.5: Mnemonic for writing function pointers 149 ........................................................................................... Section 24.6: Basics 150 ................................................................................................................................................... Chapter 25: Function Parameters 152 .................................................................................................................... Section 25.1: Parameters are passed by value 152 ...................................................................................................... Section 25.2: Passing in Arrays to Functions 152 .......................................................................................................... Section 25.3: Order of function parameter execution 153 ........................................................................................... Section 25.4: Using pointer parameters to return multiple values 153 ...................................................................... Section 25.5: Example of function returning struct containing values with error codes 154 ................................... Chapter 26: Pass 2D-arrays to functions 156 ..................................................................................................... Section 26.1: Pass a 2D-array to a function 156 ........................................................................................................... Section 26.2: Using flat arrays as 2D arrays 162 .......................................................................................................... Chapter 27: Error handling 163 .................................................................................................................................. Section 27.1: errno 163 ..................................................................................................................................................... Section 27.2: strerror 163 ................................................................................................................................................. Section 27.3: perror 163 ................................................................................................................................................... Chapter 28: Undefined behavior 165 ...................................................................................................................... Section 28.1: Dereferencing a pointer to variable beyond its lifetime 165 ................................................................. Section 28.2: Copying overlapping memory 165 .......................................................................................................... Section 28.3: Signed integer overflow 166 ..................................................................................................................... Section 28.4: Use of an uninitialized variable 167 ......................................................................................................... Section 28.5: Data race 168 ............................................................................................................................................ Section 28.6: Read value of pointer that was freed 169 .............................................................................................. Section 28.7: Using incorrect format specifier in printf 170 ......................................................................................... Section 28.8: Modify string literal 170 ............................................................................................................................ Section 28.9: Passing a null pointer to printf %s conversion 170 ................................................................................ Section 28.10: Modifying any object more than once between two sequence points 171 ....................................... Section 28.11: Freeing memory twice 172 ...................................................................................................................... Section 28.12: Bit shifting using negative counts or beyond the width of the type 172 ............................................ Section 28.13: Returning from a function that's declared with `_Noreturn` or `noreturn` function specifier 173 ............................................................................................................................................................................. Section 28.14: Accessing memory beyond allocated chunk 174 ................................................................................. Section 28.15: Modifying a const variable using a pointer 174 .................................................................................... Section 28.16: Reading an uninitialized object that is not backed by memory 175 .................................................. Section 28.17: Addition or subtraction of pointer not properly bounded 175 ............................................................ Section 28.18: Dereferencing a null pointer 175 ............................................................................................................ Section 28.19: Using ush on an input stream 176 ...................................................................................................... Section 28.20: Inconsistent linkage of identifiers 176 ................................................................................................... Section 28.21: Missing return statement in value returning function 177 ...................................................................
📄 Page 6
Section 28.22: Division by zero 177 ................................................................................................................................ Section 28.23: Conversion between pointer types produces incorrectly aligned result 178 .................................... Section 28.24: Modifying the string returned by getenv, strerror, and setlocale functions 179 .............................. Chapter 29: Random Number Generation 180 ................................................................................................... Section 29.1: Basic Random Number Generation 180 .................................................................................................. Section 29.2: Permuted Congruential Generator 180 ................................................................................................... Section 29.3: Xorshift Generation 181 ............................................................................................................................ Section 29.4: Restrict generation to a given range 182 ............................................................................................... Chapter 30: Preprocessor and Macros 183 .......................................................................................................... Section 30.1: Header Include Guards 183 ....................................................................................................................... Section 30.2: #if 0 to block out code sections 186 ........................................................................................................ Section 30.3: Function-like macros 187 .......................................................................................................................... Section 30.4: Source file inclusion 188 ............................................................................................................................ Section 30.5: Conditional inclusion and conditional function signature modification 188 ....................................... Section 30.6: __cplusplus for using C externals in C++ code compiled with C++ - name mangling 190 ............... Section 30.7: Token pasting 191 ..................................................................................................................................... Section 30.8: Predefined Macros 192 ............................................................................................................................. Section 30.9: Variadic arguments macro 193 ............................................................................................................... Section 30.10: Macro Replacement 194 ......................................................................................................................... Section 30.11: Error directive 195 ..................................................................................................................................... Section 30.12: FOREACH implementation 196 ............................................................................................................... Chapter 31: Signal handling 199 ................................................................................................................................. Section 31.1: Signal Handling with “signal()” 199 ............................................................................................................ Chapter 32: Variable arguments 201 ...................................................................................................................... Section 32.1: Using an explicit count argument to determine the length of the va_list 201 .................................... Section 32.2: Using terminator values to determine the end of va_list 202 .............................................................. Section 32.3: Implementing functions with a `printf()`-like interface 202 ................................................................... Section 32.4: Using a format string 205 ......................................................................................................................... Chapter 33: Assertion 207 ............................................................................................................................................. Section 33.1: Simple Assertion 207 .................................................................................................................................. Section 33.2: Static Assertion 207 ................................................................................................................................... Section 33.3: Assert Error Messages 208 ....................................................................................................................... Section 33.4: Assertion of Unreachable Code 209 ........................................................................................................ Section 33.5: Precondition and Postcondition 209 ........................................................................................................ Chapter 34: Generic selection 211 ............................................................................................................................ Section 34.1: Check whether a variable is of a certain qualified type 211 .................................................................. Section 34.2: Generic selection based on multiple arguments 211 ............................................................................. Section 34.3: Type-generic printing macro 213 ............................................................................................................ Chapter 35: X-macros 214 ............................................................................................................................................ Section 35.1: Trivial use of X-macros for printfs 214 ..................................................................................................... Section 35.2: Extension: Give the X macro as an argument 214 ................................................................................. Section 35.3: Enum Value and Identifier 215 ................................................................................................................. Section 35.4: Code generation 215 ................................................................................................................................. Chapter 36: Aliasing and eective type 217 ....................................................................................................... Section 36.1: Eective type 217 ....................................................................................................................................... Section 36.2: restrict qualification 217 ............................................................................................................................ Section 36.3: Changing bytes 218 ................................................................................................................................... Section 36.4: Character types cannot be accessed through non-character types 219 ........................................... Section 36.5: Violating the strict aliasing rules 220 .......................................................................................................
📄 Page 7
Chapter 37: Compilation 221 ....................................................................................................................................... Section 37.1: The Compiler 221 ........................................................................................................................................ Section 37.2: File Types 222 ............................................................................................................................................ Section 37.3: The Linker 222 ............................................................................................................................................ Section 37.4: The Preprocessor 224 ............................................................................................................................... Section 37.5: The Translation Phases 225 ...................................................................................................................... Chapter 38: Inline assembly 227 ................................................................................................................................ Section 38.1: gcc Inline assembly in macros 227 ........................................................................................................... Section 38.2: gcc Basic asm support 227 ...................................................................................................................... Section 38.3: gcc Extended asm support 228 ................................................................................................................ Chapter 39: Identifier Scope 229 ............................................................................................................................... Section 39.1: Function Prototype Scope 229 .................................................................................................................. Section 39.2: Block Scope 230 ......................................................................................................................................... Section 39.3: File Scope 230 ............................................................................................................................................ Section 39.4: Function scope 231 .................................................................................................................................... Chapter 40: Implicit and Explicit Conversions 232 ........................................................................................... Section 40.1: Integer Conversions in Function Calls 232 ............................................................................................... Section 40.2: Pointer Conversions in Function Calls 233 .............................................................................................. Chapter 41: Type Qualifiers 235 ................................................................................................................................ Section 41.1: Volatile variables 235 .................................................................................................................................. Section 41.2: Unmodifiable (const) variables 236 ......................................................................................................... Chapter 42: Typedef 237 .............................................................................................................................................. Section 42.1: Typedef for Structures and Unions 237 ................................................................................................... Section 42.2: Typedef for Function Pointers 238 .......................................................................................................... Section 42.3: Simple Uses of Typedef 239 ..................................................................................................................... Chapter 43: Storage Classes 241 .............................................................................................................................. Section 43.1: auto 241 ....................................................................................................................................................... Section 43.2: register 241 ................................................................................................................................................ Section 43.3: static 242 ..................................................................................................................................................... Section 43.4: typedef 243 ................................................................................................................................................ Section 43.5: extern 243 ................................................................................................................................................... Section 43.6: _Thread_local 244 .................................................................................................................................... Chapter 44: Declarations 246 .................................................................................................................................... Section 44.1: Calling a function from another C file 246 ............................................................................................... Section 44.2: Using a Global Variable 247 ..................................................................................................................... Section 44.3: Introduction 247 ......................................................................................................................................... Section 44.4: Typedef 250 ............................................................................................................................................... Section 44.5: Using Global Constants 250 ..................................................................................................................... Section 44.6: Using the right-left or spiral rule to decipher C declaration 252 .......................................................... Chapter 45: Structure Padding and Packing 256 ............................................................................................. Section 45.1: Packing structures 256 .............................................................................................................................. Section 45.2: Structure padding 257 .............................................................................................................................. Chapter 46: Memory management 258 ................................................................................................................ Section 46.1: Allocating Memory 258 .............................................................................................................................. Section 46.2: Freeing Memory 259 ................................................................................................................................. Section 46.3: Reallocating Memory 261 ......................................................................................................................... Section 46.4: realloc(ptr, 0) is not equivalent to free(ptr) 262 ..................................................................................... Section 46.5: Multidimensional arrays of variable size 262 ......................................................................................... Section 46.6: alloca: allocate memory on stack 263 ....................................................................................................
📄 Page 8
Section 46.7: User-defined memory management 264 ............................................................................................... Chapter 47: Implementation-defined behaviour 266 ..................................................................................... Section 47.1: Right shift of a negative integer 266 ........................................................................................................ Section 47.2: Assigning an out-of-range value to an integer 266 ............................................................................... Section 47.3: Allocating zero bytes 266 .......................................................................................................................... Section 47.4: Representation of signed integers 266 ................................................................................................... Chapter 48: Atomics 267 ............................................................................................................................................... Section 48.1: atomics and operators 267 ....................................................................................................................... Chapter 49: Jump Statements 268 .......................................................................................................................... Section 49.1: Using return 268 ......................................................................................................................................... Section 49.2: Using goto to jump out of nested loops 268 .......................................................................................... Section 49.3: Using break and continue 269 ................................................................................................................. Chapter 50: Create and include header files 271 ............................................................................................. Section 50.1: Introduction 271 ......................................................................................................................................... Section 50.2: Self-containment 271 ................................................................................................................................ Section 50.3: Minimality 273 ............................................................................................................................................ Section 50.4: Notation and Miscellany 273 .................................................................................................................... Section 50.5: Idempotence 275 ....................................................................................................................................... Section 50.6: Include What You Use (IWYU) 275 ........................................................................................................... Chapter 51: <ctype.h> — character classification & conversion 277 ....................................................... Section 51.1: Introduction 277 .......................................................................................................................................... Section 51.2: Classifying characters read from a stream 278 ..................................................................................... Section 51.3: Classifying characters from a string 279 ................................................................................................. Chapter 52: Side Eects 280 ....................................................................................................................................... Section 52.1: Pre/Post Increment/Decrement operators 280 ..................................................................................... Chapter 53: Multi-Character Character Sequence 282 .................................................................................. Section 53.1: Trigraphs 282 .............................................................................................................................................. Section 53.2: Digraphs 282 .............................................................................................................................................. Chapter 54: Constraints 284 ........................................................................................................................................ Section 54.1: Duplicate variable names in the same scope 284 .................................................................................. Section 54.2: Unary arithmetic operators 284 .............................................................................................................. Chapter 55: Inlining 285 ................................................................................................................................................. Section 55.1: Inlining functions used in more than one source file 285 ....................................................................... Chapter 56: Unions 287 ................................................................................................................................................... Section 56.1: Using unions to reinterpret values 287 .................................................................................................... Section 56.2: Writing to one union member and reading from another 287 ............................................................. Section 56.3: Dierence between struct and union 288 ............................................................................................... Chapter 57: Threads (native) 289 ............................................................................................................................. Section 57.1: Inititialization by one thread 289 ............................................................................................................... Section 57.2: Start several threads 289 .......................................................................................................................... Chapter 58: Multithreading 291 ................................................................................................................................. Section 58.1: C11 Threads simple example 291 .............................................................................................................. Chapter 59: Interprocess Communication (IPC) 292 ........................................................................................ Section 59.1: Semaphores 292 ......................................................................................................................................... Chapter 60: Testing frameworks 297 ..................................................................................................................... Section 60.1: Unity Test Framework 297 ........................................................................................................................ Section 60.2: CMocka 297 ................................................................................................................................................ Section 60.3: CppUTest 298 .............................................................................................................................................
📄 Page 9
Chapter 61: Valgrind 300 ................................................................................................................................................ Section 61.1: Bytes lost -- Forgetting to free 300 ........................................................................................................... Section 61.2: Most common errors encountered while using Valgrind 300 ................................................................ Section 61.3: Running Valgrind 301 ................................................................................................................................. Section 61.4: Adding flags 301 ......................................................................................................................................... Chapter 62: Common C programming idioms and developer practices 302 ..................................... Section 62.1: Comparing literal and variable 302 .......................................................................................................... Section 62.2: Do not leave the parameter list of a function blank — use void 302 ................................................... Chapter 63: Common pitfalls 305 .............................................................................................................................. Section 63.1: Mixing signed and unsigned integers in arithmetic operations 305 ...................................................... Section 63.2: Macros are simple string replacements 305 .......................................................................................... Section 63.3: Forgetting to copy the return value of realloc into a temporary 307 .................................................. Section 63.4: Forgetting to allocate one extra byte for \0 308 ................................................................................... Section 63.5: Misunderstanding array decay 308 ......................................................................................................... Section 63.6: Forgetting to free memory (memory leaks) 310 ................................................................................... Section 63.7: Copying too much 311 .............................................................................................................................. Section 63.8: Mistakenly writing = instead of == when comparing 312 ....................................................................... Section 63.9: Newline character is not consumed in typical scanf() call 313 ............................................................ Section 63.10: Adding a semicolon to a #define 314 .................................................................................................... Section 63.11: Incautious use of semicolons 314 ............................................................................................................ Section 63.12: Undefined reference errors when linking 315 ....................................................................................... Section 63.13: Checking logical expression against 'true' 317 ...................................................................................... Section 63.14: Doing extra scaling in pointer arithmetic 318 ....................................................................................... Section 63.15: Multi-line comments cannot be nested 319 ........................................................................................... Section 63.16: Ignoring return values of library functions 321 ..................................................................................... Section 63.17: Comparing floating point numbers 321 ................................................................................................. Section 63.18: Floating point literals are of type double by default 323 ..................................................................... Section 63.19: Using character constants instead of string literals, and vice versa 323 ........................................... Section 63.20: Recursive function — missing out the base condition 324 .................................................................. Section 63.21: Overstepping array boundaries 325 ...................................................................................................... Section 63.22: Passing unadjacent arrays to functions expecting "real" multidimensional arrays 326 ................. Credits 328 ............................................................................................................................................................................ You may also like 333 ......................................................................................................................................................
📄 Page 10
GoalKicker.com – C Notes for Professionals 1 About Please feel free to share this PDF with anyone for free, latest version of this book can be downloaded from: https://goalkicker.com/CBook This C Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified This is an unofficial free book created for educational purposes and is not affiliated with official C group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk Please send feedback and corrections to web@petercv.com
📄 Page 11
GoalKicker.com – C Notes for Professionals 2 Chapter 1: Getting started with C Language Version Standard Publication Date K&R n/a 1978-02-22 C89 ANSI X3.159-1989 1989-12-14 C90 ISO/IEC 9899:1990 1990-12-20 C95 ISO/IEC 9899/AMD1:1995 1995-03-30 C99 ISO/IEC 9899:1999 1999-12-16 C11 ISO/IEC 9899:2011 2011-12-15 Section 1.1: Hello World To create a simple C program which prints "Hello, World" on the screen, use a text editor to create a new file (e.g. hello.c — the file extension must be .c) containing the following source code: hello.c #include <stdio.h> int main(void) { puts("Hello, World"); return 0; } Live demo on Coliru Let's look at this simple program line by line #include <stdio.h> This line tells the compiler to include the contents of the standard library header file stdio.h in the program. Headers are usually files containing function declarations, macros and data types, and you must include the header file before you use them. This line includes stdio.h so it can call the function puts(). See more about headers. int main(void) This line starts the definition of a function. It states the name of the function (main), the type and number of arguments it expects (void, meaning none), and the type of value that this function returns (int). Program execution starts in the main() function. { … } The curly braces are used in pairs to indicate where a block of code begins and ends. They can be used in a lot of ways, but in this case they indicate where the function begins and ends. puts("Hello, World"); This line calls the puts() function to output text to standard output (the screen, by default), followed by a newline.
📄 Page 12
GoalKicker.com – C Notes for Professionals 3 The string to be output is included within the parentheses. "Hello, World" is the string that will be written to the screen. In C, every string literal value must be inside the double quotes "…". See more about strings. In C programs, every statement needs to be terminated by a semi-colon (i.e. ;). return 0; When we defined main(), we declared it as a function returning an int, meaning it needs to return an integer. In this example, we are returning the integer value 0, which is used to indicate that the program exited successfully. After the return 0; statement, the execution process will terminate. Editing the program Simple text editors include vim or gedit on Linux, or Notepad on Windows. Cross-platform editors also include Visual Studio Code or Sublime Text. The editor must create plain text files, not RTF or other any other format. Compiling and running the program To run the program, this source file (hello.c) first needs to be compiled into an executable file (e.g. hello on Unix/Linux system or hello.exe on Windows). This is done using a compiler for the C language. See more about compiling Compile using GCC GCC (GNU Compiler Collection) is a widely used C compiler. To use it, open a terminal, use the command line to navigate to the source file's location and then run: gcc hello.c -o hello If no errors are found in the the source code (hello.c), the compiler will create a binary file, the name of which is given by the argument to the -o command line option (hello). This is the final executable file. We can also use the warning options -Wall -Wextra -Werror, that help to identify problems that can cause the program to fail or produce unexpected results. They are not necessary for this simple program but this is way of adding them: gcc -Wall -Wextra -Werror -o hello hello.c Using the clang compiler To compile the program using clang you can use: clang -Wall -Wextra -Werror -o hello hello.c By design, the clang command line options are similar to those of GCC. Using the Microsoft C compiler from the command line
📄 Page 13
GoalKicker.com – C Notes for Professionals 4 If using the Microsoft cl.exe compiler on a Windows system which supports Visual Studio and if all environment variables are set, this C example may be compiled using the following command which will produce an executable hello.exe within the directory the command is executed in (There are warning options such as /W3 for cl, roughly analogous to -Wall etc for GCC or clang). cl hello.c Executing the program Once compiled, the binary file may then be executed by typing ./hello in the terminal. Upon execution, the compiled program will print Hello, World, followed by a newline, to the command prompt. Section 1.2: Original "Hello, World!" in K&R C The following is the original "Hello, World!" program from the book The C Programming Language by Brian Kernighan and Dennis Ritchie (Ritchie was the original developer of the C programming language at Bell Labs), referred to as "K&R": Version = K&R #include <stdio.h> main() { printf("hello, world\n"); } Notice that the C programming language was not standardized at the time of writing the first edition of this book (1978), and that this program will probably not compile on most modern compilers unless they are instructed to accept C90 code. This very first example in the K&R book is now considered poor quality, in part because it lacks an explicit return type for main() and in part because it lacks a return statement. The 2nd edition of the book was written for the old C89 standard. In C89, the type of main would default to int, but the K&R example does not return a defined value to the environment. In C99 and later standards, the return type is required, but it is safe to leave out the return statement of main (and only main), because of a special case introduced with C99 5.1.2.2.3 — it is equivalent to returning 0, which indicates success. The recommended and most portable form of main for hosted systems is int main (void) when the program does not use any command line arguments, or int main(int argc, char **argv) when the program does use the command line arguments. C90 §5.1.2.2.3 Program termination A return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument. If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined. C90 §6.6.6.4 The return statement If a return statement without an expression is executed, and the value of the function call is used by the
📄 Page 14
GoalKicker.com – C Notes for Professionals 5 caller, the behavior is undefined. Reaching the } that terminates a function is equivalent to executing a return statement without an expression. C99 §5.1.2.2.3 Program termination If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
📄 Page 15
GoalKicker.com – C Notes for Professionals 6 Chapter 2: Comments Comments are used to indicate something to the person reading the code. Comments are treated like a blank by the compiler and do not change anything in the code's actual meaning. There are two syntaxes used for comments in C, the original /* */ and the slightly newer //. Some documentation systems use specially formatted comments to help produce the documentation for code. Section 2.1: Commenting using the preprocessor Large chunks of code can also be "commented out" using the preprocessor directives #if 0 and #endif. This is useful when the code contains multi-line comments that otherwise would not nest. #if 0 /* Starts the "comment", anything from here on is removed by preprocessor */ /* A large amount of code with multi-line comments */ int foo() { /* lots of code */ ... /* ... some comment describing the if statement ... */ if (someTest) { /* some more comments */ return 1; } return 0; } #endif /* 0 */ /* code from here on is "uncommented" (included in compiled executable) */ ... Section 2.2: /* */ delimited comments A comment starts with a forward slash followed immediately by an asterisk (/*), and ends as soon as an asterisk immediately followed by a forward slash (*/) is encountered. Everything in between these character combinations is a comment and is treated as a blank (basically ignored) by the compiler. /* this is a comment */ The comment above is a single line comment. Comments of this /* type can span multiple lines, like so: /* this is a multi-line comment */ Though it is not strictly necessary, a common style convention with multi-line comments is to put leading spaces and asterisks on the lines subsequent to the first, and the /* and */ on new lines, such that they all line up: /* * this is a * multi-line * comment
📄 Page 16
GoalKicker.com – C Notes for Professionals 7 */ The extra asterisks do not have any functional effect on the comment as none of them have a related forward slash. These /* type of comments can be used on their own line, at the end of a code line, or even within lines of code: /* this comment is on its own line */ if (x && y) { /*this comment is at the end of a line */ if ((complexCondition1) /* this comment is within a line of code */ && (complexCondition2)) { /* this comment is within an if, on its own line */ } } Comments cannot be nested. This is because any subsequent /* will be ignored (as part of the comment) and the first */ reached will be treated as ending the comment. The comment in the following example will not work: /* outer comment, means this is ignored => /* attempted inner comment */ <= ends the comment, not this one => */ To comment blocks of code that contain comments of this type, that would otherwise be nested, see the Commenting using the preprocessor example below Section 2.3: // delimited comments Version ≥ C99 C99 introduced the use of C++-style single-line comments. This type of comment starts with two forward slashes and runs to the end of a line: // this is a comment This type of comment does not allow multi-line comments, though it is possible to make a comment block by adding several single line comments one after the other: // each of these lines are a single-line comment // note how each must start with // the double forward-slash This type of comment may be used on its own line or at the end of a code line. However, because they run to the end of the line, they may not be used within a code line // this comment is on its own line if (x && y) { // this comment is at the end of a line // this comment is within an if, on its own line } Section 2.4: Possible pitfall due to trigraphs Version ≥ C99 While writing // delimited comments, it is possible to make a typographical error that affects their expected operation. If one types:
📄 Page 17
GoalKicker.com – C Notes for Professionals 8 int x = 20; // Why did I do this??/ The / at the end was a typo but now will get interpreted into \. This is because the ??/ forms a trigraph. The ??/ trigraph is actually a longhand notation for \, which is the line continuation symbol. This means that the compiler thinks the next line is a continuation of the current line, that is, a continuation of the comment, which may not be what is intended. int foo = 20; // Start at 20 ??/ int bar = 0; // The following will cause a compilation error (undeclared variable 'bar') // because 'int bar = 0;' is part of the comment on the preceding line bar += foo;
📄 Page 18
GoalKicker.com – C Notes for Professionals 9 Chapter 3: Data Types Section 3.1: Interpreting Declarations A distinctive syntactic peculiarity of C is that declarations mirror the use of the declared object as it would be in a normal expression. The following set of operators with identical precedence and associativity are reused in declarators, namely: the unary * "dereference" operator which denotes a pointer; the binary [] "array subscription" operator which denotes an array; the (1+n)-ary () "function call" operator which denotes a function; the () grouping parentheses which override the precedence and associativity of the rest of the listed operators. The above three operators have the following precedence and associativity: Operator Relative Precedence Associativity [] (array subscription) 1 Left-to-right () (function call) 1 Left-to-right * (dereference) 2 Right-to-left When interpreting declarations, one has to start from the identifier outwards and apply the adjacent operators in the correct order as per the above table. Each application of an operator can be substituted with the following English words: Expression Interpretation thing[X] an array of size X of... thing(t1, t2, t3) a function taking t1, t2, t3 and returning... *thing a pointer to... It follows that the beginning of the English interpretation will always start with the identifier and will end with the type that stands on the left-hand side of the declaration. Examples char *names[20]; [] takes precedence over *, so the interpretation is: names is an array of size 20 of a pointer to char. char (*place)[10]; In case of using parentheses to override the precedence, the * is applied first: place is a pointer to an array of size 10 of char. int fn(long, short); There is no precedence to worry about here: fn is a function taking long, short and returning int. int *fn(void); The () is applied first: fn is a function taking void and returning a pointer to int.
📄 Page 19
GoalKicker.com – C Notes for Professionals 10 int (*fp)(void); Overriding the precedence of (): fp is a pointer to a function taking void and returning int. int arr[5][8]; Multidimensional arrays are not an exception to the rule; the [] operators are applied in left-to-right order according to the associativity in the table: arr is an array of size 5 of an array of size 8 of int. int **ptr; The two dereference operators have equal precedence, so the associativity takes effect. The operators are applied in right-to-left order: ptr is a pointer to a pointer to an int. Multiple Declarations The comma can be used as a separator (*not* acting like the comma operator) in order to delimit multiple declarations within a single statement. The following statement contains five declarations: int fn(void), *ptr, (*fp)(int), arr[10][20], num; The declared objects in the above example are: fn: a function taking void and returning int; ptr: a pointer to an int; fp: a pointer to a function taking int and returning int; arr: an array of size 10 of an array of size 20 of int; num: int. Alternative Interpretation Because declarations mirror use, a declaration can also be interpreted in terms of the operators that could be applied over the object and the final resulting type of that expression. The type that stands on the left-hand side is the final result that is yielded after applying all operators. /* * Subscripting "arr" and dereferencing it yields a "char" result. * Particularly: *arr[5] is of type "char". */ char *arr[20]; /* * Calling "fn" yields an "int" result. * Particularly: fn('b') is of type "int". */ int fn(char); /* * Dereferencing "fp" and then calling it yields an "int" result. * Particularly: (*fp)() is of type "int". */ int (*fp)(void); /* * Subscripting "strings" twice and dereferencing it yields a "char" result. * Particularly: *strings[5][15] is of type "char" */ char *strings[10][20];
📄 Page 20
GoalKicker.com – C Notes for Professionals 11 Section 3.2: Fixed Width Integer Types (since C99) Version ≥ C99 The header <stdint.h> provides several fixed-width integer type definitions. These types are optional and only provided if the platform has an integer type of the corresponding width, and if the corresponding signed type has a two's complement representation of negative values. See the remarks section for usage hints of fixed width types. /* commonly used types include */ uint32_t u32 = 32; /* exactly 32-bits wide */ uint8_t u8 = 255; /* exactly 8-bits wide */ int64_t i64 = -65 /* exactly 64 bit in two's complement representation */ Section 3.3: Integer types and constants Signed integers can be of these types (the int after short, or long is optional): signed char c = 127; /* required to be 1 byte, see remarks for further information. */ signed short int si = 32767; /* required to be at least 16 bits. */ signed int i = 32767; /* required to be at least 16 bits */ signed long int li = 2147483647; /* required to be at least 32 bits. */ Version ≥ C99 signed long long int li = 2147483647; /* required to be at least 64 bits */ Each of these signed integer types has an unsigned version. unsigned int i = 65535; unsigned short = 2767; unsigned char = 255; For all types but char the signed version is assumed if the signed or unsigned part is omitted. The type char constitutes a third character type, different from signed char and unsigned char and the signedness (or not) depends on the platform. Different types of integer constants (called literals in C jargon) can be written in different bases, and different width, based on their prefix or suffix. /* the following variables are initialized to the same value: */ int d = 42; /* decimal constant (base10) */ int o = 052; /* octal constant (base8) */ int x = 0xaf; /* hexadecimal constants (base16) */ int X = 0XAf; /* (letters 'a' through 'f' (case insensitive) represent 10 through 15) */ Decimal constants are always signed. Hexadecimal constants start with 0x or 0X and octal constants start just with a 0. The latter two are signed or unsigned depending on whether the value fits into the signed type or not. /* suffixes to describe width and signedness : */ long int i = 0x32; /* no suffix represent int, or long int */ unsigned int ui = 65535u; /* u or U represent unsigned int, or long int */ long int li = 65536l; /* l or L represent long int */ Without a suffix the constant has the first type that fits its value, that is a decimal constant that is larger than
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List