Learning GNU C

Ciaran O'Riordan

This file is a C programming tutorial using the GNU C compiler and GNU Libc.

Copyright © 2002 Ciaran O'Riordan.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

with the no Invariant Sections, with the Front-Cover Texts being "Richard Stallman Rules", and with the Back-Cover Texts being "This is a Free book, Free as in Freedom. Permission is granted to copy, distribute, and edit this book under the GNU Free Documentation License".


Table of Contents
Preface
1. Target Audience
2. Scope of this text
3. Why learn C?
4. Why use GNU?
5. Why Free Software?
6. References to persons
1. Introduction to C
1.1. What are Programming Languages?
1.2. What is C?
1.3. Programming Tools
1.4. Introducing GCC
1.5. Who defines Valid C?
1.5.1. "K&R" C
1.5.2. ISO C
1.5.3. C99
1.5.4. GNU C
1.5.5. Choosing a Dialect
1.5.6. Future Standards
1.6. Conclusion
2. Staring With Functions
2.1. What are functions?
2.2. A Line-by-Line Dissection
2.3. Comments
2.4. Making your own Functions
2.5. Multiple Files
2.6. Header Files
2.7. A Larger (non)Program
2.8. Another new Function
2.9. Primer Summary
3. Data and Expressions
3.1. Bicycle Dissection
3.2. Data Types
3.3. Another Example of Assignment
3.4. Quick Explanation of printf()
3.5. Simple arithmetic
3.6. Global Variables
3.7. Static Variables
3.8. Constant Variables
4. Flow Control
4.1. Branching
4.2. if ... else
4.3. Loops
4.4. while
4.5. for
4.6. do .. while
4.7. Switch
4.8. The Conditional Operator
4.9. break & continue
5. Pointers
5.1. The Basics
5.2. The Address of a Variable
5.3. Pointers as Function Arguments
5.4. Pointer Arithmetic
5.5. Generic Pointers
6. Structured Data Types
6.1. What is Structured data?
6.2. Arrays
6.3. Declaring and Accessing Arrays
6.4. Initialising Arrays
6.5. Multidimensional Arrays
6.6. Arrays of Characters (Text)
6.7. Defining data types
6.8. Structured Data
6.9. Unions
7. Run-time Memory Allocation
7.1. Why you need this
7.2. Dynamic Memory Functions
7.3. Run-time Memory Summary
8. Strings and File I/O
8.1. Introduction
9. Storage Classes
9.1. What are Storage Classes?
9.2. auto
9.3. static
9.4. extern
9.5. register
9.6. the restrict type qualifier
9.7. typedef
10. The C Preprocessor
10.1. What is the C Prepressor
10.2. What is it used for?
10.3. Some sample macros
10.4. Caveats for macros
10.5. Are macros necessary?
10.6. Replacing Simple Macros
10.7. Replacing Complex Macros
11. Variable Length Arguments
11.1. What are Variable Length Arguments?
12. Tricks with Functions
12.1. What are Virtual Functions?
12.2. Nesting functions
12.3. The Benefits of Nested Functions
12.4. Declaring and Defining Nested Functions
12.5. Scope
13. Taking Command Line Arguments
13.1. How does C handle command line arguments?
13.2. Argp
13.3. Using More of the Argp Functionality
13.4. Environment Variables
14. Using and Writing Libraries
14.1. What are Libraries?
14.2. Using Libraries
14.3. Stages of Compilation
14.4. Writing a library
14.5. Dynamic or Static
15. Writing Good Code
15.1. Readability
16. Speed
16.1. About Optimising
16.2. What are function attributes?
16.3. Function Attribute Syntax
16.4. What are pure and const?
A. GNU Free Documentation License
A.1. 0. PREAMBLE
A.2. 1. APPLICABILITY AND DEFINITIONS
A.3. 2. VERBATIM COPYING
A.4. 3. COPYING IN QUANTITY
A.5. 4. MODIFICATIONS
A.6. 5. COMBINING DOCUMENTS
A.7. 6. COLLECTIONS OF DOCUMENTS
A.8. 7. AGGREGATION WITH INDEPENDENT WORKS
A.9. 8. TRANSLATION
A.10. 9. TERMINATION
A.11. 10. FUTURE REVISIONS OF THIS LICENSE
A.12. ADDENDUM (How to use this License for your documents)
List of Examples
1-1. C vs. Assembly language
1-2. tiny.c
1-3. tiny2.c
2-1. hello.c
2-2. hello2.c
2-3. three_functions.c
2-4. main.c
2-5. display.c
2-6. display.h
2-7. prices.c
2-8. prices.h
3-1. bicycles.c
3-2. sizeof_types.c
3-3. displaying_variables.c
3-4. more_printf.c
3-5. wages.c
4-1. using_if.c
4-2. cows2.c
4-3. guess_my_number.c
4-4. for_ten.c
4-5. guess_my_number.c
4-6. apples.c
5-1. pointers_are_simple.c
5-2. swap_ints.c
5-3. generic_pointer.c
6-1. first_arrays.c
6-2. initialise_array.c
6-3. number_square.c
6-4. person_struct.c
9-1. list_squares.c
9-2. battleships.c
10-1. box_of_stars.c
10-2. max_macro.c
10-3. max_macro_problem.c
12-1. virtual_function.c
12-2. simple nested function
13-1. list_args.c
13-2. simple_argp.c
13-3. better_argp.c