### Debug C Program with GDB Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Compile-Example.md Starts the GNU Debugger (GDB) to debug the compiled C program 'fib1'. ```Shell gdb fib1 ``` -------------------------------- ### C Program Examples Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/The-First-Example.md Examples of C source code, including recursive and iterative Fibonacci implementations, presented in a fixed-width typeface for clarity. ```C // Example: Recursive Fibonacci // (Details not provided in the input text) ``` ```C // Example: Iterative Fibonacci // (Details not provided in the input text) ``` -------------------------------- ### Mixed Comment Types Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Comments.md Provides examples of how traditional block comments and C++-style line comments can be placed within each other, showing valid combinations. ```C /* traditional comment // contains line comment more traditional comment */ text here is not a comment // line comment /* contains traditional comment */ ``` -------------------------------- ### C Macro Expansion Example: Stringification and Concatenation Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Argument-Prescan.md Demonstrates how macro arguments are expanded before stringification or concatenation, using nested macro calls to achieve the desired expansion. The example shows the effect of prescan on `AFTERX(BUFSIZE)` and `XAFTERX(BUFSIZE)`. ```C #define AFTERX(x) X_ ## x #define XAFTERX(x) AFTERX(x) #define TABLESIZE 1024 #define BUFSIZE TABLESIZE ``` -------------------------------- ### C Function Call Example with printf and fib Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Function-Calls.md An example illustrating a function call in C, specifically using the `printf` function to display output and calling another function `fib` to calculate a value. ```C printf ("Fibonacci series item %d is %d\n", 19, fib (19)); ``` -------------------------------- ### Run Compiled C Program Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Compile-Example.md Executes the compiled C program named 'fib1' from the current directory. ```Shell ./fib1 ``` -------------------------------- ### C: Local Variable Declaration with Initializer Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Iterative-Fibonacci.md Demonstrates how to declare a local integer variable in C and initialize it with a value. ```C int last = 1; ``` -------------------------------- ### C: For Loop Statement Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Iterative-Fibonacci.md Provides the structure of a C for loop, including initialization, condition, and increment/update. ```C for (i = 1; i < n; ++i) body ``` -------------------------------- ### C Function Header Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Function-Header.md Demonstrates the structure of a C function header, specifying the return type, function name, and argument types. This example shows a function named 'fib' that takes an integer 'n' and returns an integer. ```C int fib (int n) ``` -------------------------------- ### C `for` Loop Example: Fibonacci Sequence Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Example-of-for.md Demonstrates the `for` loop structure in C using an iterative approach to calculate Fibonacci numbers. The example highlights initialization, condition checking, and incrementing within the loop, along with variable management inside the loop body. ```C int i; for (i = 1; i < n; ++i) /* If n is 1 or less, the loop runs zero times, */ /* since i < n is false the first time. */ { /* Now last is fib (i) and prev is fib (i - 1). */ /* Compute fib (i + 1). */ int next = prev + last; /* Shift the values down. */ prev = last; last = next; /* Now last is fib (i + 1) and prev is fib (i). But that won’t stay true for long, because we are about to increment i. */ } ``` -------------------------------- ### C: Local Variable Declaration Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Iterative-Fibonacci.md Illustrates the basic syntax for declaring a local integer variable in C. ```C int i; ``` -------------------------------- ### C: Assignment Expression Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Iterative-Fibonacci.md Shows the syntax for assigning a value to a variable in C. ```C variable = value ``` -------------------------------- ### C goto for do-while Loop Equivalent Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/goto-Statement.md An example illustrating how to use the 'goto' statement to create a loop structure equivalent to a 'do-while' loop in C. ```C { loop_restart: body if (condition) goto loop_restart; } ``` -------------------------------- ### C Variadic Function Usage Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Variable-Number-of-Arguments.md Demonstrates how to call a variadic function in C, passing the count of additional arguments followed by the arguments themselves. ```C sum = add_multiple_values (3, 12, 34, 190); /* Value is 12+34+190. */ ``` -------------------------------- ### C Decimal Floating-Point Constants Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Floating-Constants.md Examples of decimal floating-point constants in C, demonstrating the use of decimal points and exponents. ```C 1.0 1000. 3.14159 .05 .0005 ``` -------------------------------- ### C Integer Division Examples Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Division-and-Remainder.md Demonstrates how integer division in C rounds results towards zero for positive and negative numbers. ```C 16 / 3 ⇒ 5 -16 / 3 ⇒ -5 16 / -3 ⇒ -5 -16 / -3 ⇒ 5 ``` -------------------------------- ### C Identifier Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Identifiers.md Demonstrates a valid C identifier used as a variable name. Identifiers in C are sequences of letters, digits, and underscores, not starting with a digit, and are case-sensitive. ```C int anti_dis_establishment_arian_ism; ``` -------------------------------- ### C Structure Initialization Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Structure-Constructors.md Demonstrates initializing a C structure using a constructor with field labels and direct value assignment. It shows how to specify values for individual fields or rely on the order. ```C struct foo { int a; char b[2]; } structure; // Direct initialization ((struct foo) {x + y, 'a', 0}) // Initialization with nested array ((struct foo) {x + y, {'a', 0}}) // Initialization using field labels (struct foo) {.a = x + y, .b = {'a', 0}} ``` ```C // Equivalent statement expression ({ struct foo temp = {x + y, 'a', 0}; temp; }) ``` -------------------------------- ### C Address-of Operator Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Address-of-Data.md Demonstrates the use of the '&' operator to get the memory address of an integer variable 'i' and an element of a double array 'a'. It highlights that '&' operates on lvalues. ```C int i; double a[5]; // &i gives the address of the variable i // &a[3] gives the address of the element 3 of a ``` -------------------------------- ### C Type Designator Example: int Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Type-Designators.md Demonstrates how to derive the type designator 'int' for full-word integers by starting with a variable declaration and removing the variable name and semicolon. ```C int foo; ``` -------------------------------- ### C Macro Expansion Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Macro-Arguments.md Demonstrates the step-by-step expansion of a nested macro call `min(min(a, b), c)`. It shows how arguments are substituted and rescanned for further macro expansion, resulting in a complex expression. ```C min (min (a, b), c) is first expanded to min (((a) < (b) ? (a) : (b)), (c)) and then to ((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))) : (c)) ``` -------------------------------- ### Conditional Includes with Macros Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Computed-Includes.md Demonstrates selecting different header files based on system configurations using conditional compilation and computed includes with macros. ```C #if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 /* … */ #endif ``` ```C #define SYSTEM_H "system_1.h" /* … */ #include SYSTEM_H ``` ```C #define HEADER "a\"b" #include HEADER ``` -------------------------------- ### C Array Pointer Arithmetic Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Passing-Array-Args.md Shows how passing a pointer to an element other than the start of an array affects which element is modified. By passing `data+1`, the function `clobber4` modifies the element at `data[5]` instead of `data[4]`. ```C clobber4 (data+1); ``` -------------------------------- ### C Designated Initializers for Arrays Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Designated-Inits.md Demonstrates initializing specific elements of a C array using designated initializers. It shows how to set individual elements and how non-designated elements are handled. ```C int foo[10] = { [3] = 42, [7] = 58 }; ``` ```C int foo[10] = { 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 }; ``` ```C int foo[10] = { [3] = 42, 43, 44, [7] = 58 }; ``` ```C int foo[10] = { 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 }; ``` -------------------------------- ### C Preprocessing Directives Examples Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Directives.md Illustrates the basic syntax of C preprocessing directives, including defining macros, undefining macros, and triggering preprocessor errors. ```C #define LIMIT 51 # undef LIMIT # error You screwed up! ``` -------------------------------- ### C Union Example: Number Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Unions.md An example of a C union named 'number' that can hold either a long integer or a double-precision floating-point number in the same memory space. ```C union number { long int integer; double float; } ``` -------------------------------- ### Macro Expansion Example - C Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Misnesting.md Demonstrates how a macro call can be constructed by combining a macro body with its arguments, leading to a nested expansion. This highlights the process of macro substitution in C. ```C #define twice(x) (2*(x)) #define call_with_1(x) x(1) call_with_1 (twice) → twice(1) → (2*(1)) ``` -------------------------------- ### UTF-8 String Constant Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/UTF_002d8-String-Constants.md Demonstrates the syntax for defining a UTF-8 encoded string constant in C using the 'u8' prefix. This example shows how non-ASCII characters are represented as multi-byte sequences. ```C u8"A cónstàñt" ``` -------------------------------- ### C: restrict Pointer Optimization Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/restrict-Pointer-Example.md Demonstrates how 'restrict' assures the compiler that the 'out' array does not overlap with the 'in' array, enabling optimization. The function processes data by calculating `out[i] = in[i] + in[i + 1]`. ```C void process_data (const char *in, char * restrict out, size_t size) { for (i = 0; i < size; i++) out[i] = in[i] + in[i + 1]; } ``` -------------------------------- ### C Constants Overview Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Concept-Index.md A comprehensive look at constants in C, covering various forms and applications. ```c Constants.md ``` -------------------------------- ### C: Call Function with Array Argument Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Array-Example-Call.md Example of calling the `avg_of_double` function with a pre-filled array of doubles. It shows array declaration, element assignment, and passing the array and its size to the function. ```C { /* The array of values to average. */ double nums_to_average[5]; /* The average, once we compute it. */ double average; /* Fill in elements of nums_to_average. */ nums_to_average[0] = 58.7; nums_to_average[1] = 5.1; nums_to_average[2] = 7.7; nums_to_average[3] = 105.2; nums_to_average[4] = -3.14159; average = avg_of_double (5, nums_to_average); /* …now make use of average… */ } ``` -------------------------------- ### Variable Declarations in C Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Concept-Index.md A guide to declaring variables in C, including syntax, scope, and initialization. This section covers both standard and combined declaration patterns. ```APIDOC Variable Declarations: - Details the syntax for declaring variables in C. - Covers scope, type specifiers, and initialization. - Includes information on combining multiple variable declarations. - References Variable-Declarations.md and Combining-Variable-Declarations.md. ``` -------------------------------- ### Pointer Equality Comparison Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Pointer-Comparison.md Demonstrates the use of equality (==) and inequality (!=) operators to compare two pointers that point to the same memory location. The example shows that if two pointers point to the same object, the equality comparison evaluates to true. ```C { int i; int *p, *q; p = &i; q = &i; if (p == q) printf ("This will be printed.\n"); if (p != q) printf ("This won't be printed.\n"); } ``` -------------------------------- ### C Preprocessor Directives Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Symbol-Index.md Documentation for C preprocessor directives including include, line control, undefining macros, and diagnostics. ```C #include #line #undef #warning ``` -------------------------------- ### C Variable Declaration Syntax Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Variable-Declarations.md Illustrates the general syntax for declaring variables in C, including keywords, base type, decorated variable, and optional initialization. ```C keywords basetype decorated-variable [= init]; ``` -------------------------------- ### C Const Field Assignment Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/const-Fields.md Demonstrates attempting to assign a value to a `const` field within a structure. The example shows that assigning to `q->datum` (where `q` points to a `const` structure) results in an error, while assigning to `p->datum` (where `p` points to a non-`const` structure) is valid. ```C /* p has type struct intlistlink *. Convert it to struct intlistlink_ro *. */ struct intlistlink_ro *q = (struct intlistlink_ro *) p; q->datum = 5; /* Error! */ p->datum = 5; /* Valid since *p is not a struct intlistlink_ro. */ ``` -------------------------------- ### Compile C Program with GCC Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Compile-Example.md Compiles a C source file named 'fib1.c' into an executable file named 'fib1'. The -g flag includes debugging information, and the -O flag enables basic optimization. ```C gcc -g -O -o fib1 fib1.c ``` -------------------------------- ### C Hexadecimal Floating-Point Constants Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Floating-Constants.md Examples of hexadecimal floating-point constants in C, using 'p' or 'P' for exponents. ```C 0xAp2 // 40 in decimal 0xAp-1 // 5 in decimal 0x2.0Bp4 // 32.6875 decimal 0xE.2p3 // 113 decimal 0x123.ABCp0 // 291.6708984375 in decimal 0x123.ABCp4 // 4666.734375 in decimal 0x100p-8 // 1 0x10p-4 // 1 0x1p+4 // 16 0x1p+8 // 256 ``` -------------------------------- ### C Function Call Syntax Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Function-Calls.md Demonstrates the basic syntax for calling a function in C, which involves the function name followed by parentheses containing arguments. ```C function (arguments…) ``` -------------------------------- ### C: Declare a Pointer Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Declaring-Arrays-and-Pointers.md Declares a variable as a pointer to a specific type. The example shows a pointer to a structure. ```C struct list_elt *foo; ``` -------------------------------- ### C Simple Integer Declaration Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Variable-Declarations.md A basic example of declaring an integer variable named 'foo' in C. ```C int foo; ``` -------------------------------- ### C Structure Layout Example 1 Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Structure-Layout.md Demonstrates the memory layout of a C struct with fields declared as char, char, int. Shows the resulting size, alignment, and padding due to alignment requirements. ```C struct foo { char a, b; int c; }; /* This structure occupies 8 bytes, with an alignment of 4. `a` is at offset 0, `b` is at offset 1, and `c` is at offset 4. There is a gap of 2 bytes before `c`. */ ``` -------------------------------- ### C Shadowing in Subblock Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Scope.md Demonstrates that a declaration with block scope can be shadowed by another declaration with the same name in a subblock. ```C void foo (void) { char *x = "foo"; { int x = 42; ... exit (x / 6); } } ``` -------------------------------- ### C #include Directive Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/include-Operation.md Demonstrates the functionality of the #include directive in C. It shows how including a header file with a function declaration enables its use in the main program. ```C header.h: char *test (void); ``` ```C program.c: int x; #include "header.h" int main (void) { puts (test ()); } ``` ```C Equivalent program.c: int x; char *test (void); int main (void) { puts (test ()); } ``` -------------------------------- ### C Structure Assignment Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Structure-Assignment.md Demonstrates structure assignment in C, including memory allocation and copying structure fields. It shows how to copy a struct point to a newly allocated memory location. ```C #include /* Defines NULL. */ #include /* Declares malloc. */ struct point { double x, y; }; struct point * copy_point (struct point point) { struct point *p = (struct point *) malloc (sizeof (struct point)); if (p == NULL) fatal ("Out of memory"); *p = point; return p; } ``` -------------------------------- ### C Once-Only Headers Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Concept-Index.md Guidance on using include guards or `#pragma once` for header files to prevent multiple inclusions in C. ```c Once_002dOnly-Headers.md#index-including-just-once ``` -------------------------------- ### C: Declare an Array Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Declaring-Arrays-and-Pointers.md Declares a variable as an array of a specified length. The example shows an array of 5 integers. ```C int foo[5]; ``` -------------------------------- ### C Remainder Operator Examples Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Division-and-Remainder.md Illustrates the behavior of the '%' remainder operator in C with positive and negative operands. ```C 16 % 3 ⇒ 1 -16 % 3 ⇒ -1 16 % -3 ⇒ 1 -16 % -3 ⇒ -1 ``` -------------------------------- ### C Bit Field Packing Example Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Bit-Fields.md Demonstrates how two bit fields, an unsigned 4-bit 'opcode' and a signed 4-bit 'small', can be packed into a single 'char' type. ```C unsigned char opcode: 4; signed char small: 4; ``` -------------------------------- ### C Structure Variable Declaration Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Variable-Declarations.md An example of declaring a variable named 'foo' with a structure type 'struct tree_node' in C. ```C struct tree_node foo; ``` -------------------------------- ### GNU C Language Manual Index Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Concept-Index.md This section lists various topics covered in the GNU C Language Manual, providing links to detailed explanations for each. It covers fundamental C concepts like storage, strings, and structures. ```APIDOC Storage Organization: Storage.html#index-storage-organization Strings: Strings.md#index-string String Constants: String-Constants.md#index-string-constants Stringification: Stringification.md#index-stringification Structure Assignment: Structure-Assignment.html#index-structure-assignment Structure Constructors: Structure-Constructors.md#index-structure-constructors Structure Field Offset: Field-Offset.md#index-structure-field-offset Structure Fields, Constant: const-Fields.md#index-structure-fields,-constant Structure Fields, Referencing: Referencing-Fields.html#index-structure-fields,-referencing Structure Layout: Structure-Layout.md#index-structure-layout ``` -------------------------------- ### C Declaration Order Example (Invalid) Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Scope.md Illustrates an invalid declaration where an identifier is used before its declaration. The code attempts to use 'y' before it is declared. ```C int x = y + 10; int y = 5; ``` -------------------------------- ### C Structure Initialization with Arrays Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Initializers.md Shows how to initialize a structure containing an array, or an array of structures, using nested initializers. This example initializes an array of 'point' structures. ```C struct point { double x, y; }; struct point series[] = { {0, 0}, {1.5, 2.8}, {99, 100.0004} }; ``` -------------------------------- ### Postincrement Example - C Source: https://github.com/vernongrant/gnu-c-language-manual/blob/main/markdown/Postincrement_002fPostdecrement.md Demonstrates the behavior of the postincrement operator in C. It shows how `i++` evaluates to the current value of `i` before `i` is incremented. ```C #include /* Declares printf. */ int main (void) { int i = 5; printf ("%d\n", i); printf ("%d\n", i++); printf ("%d\n", i); return 0; } ```