Site Home

Please see http://caniuse.com/#feat=mathml if your browser supports MathML because certain sections of this book rely on MathML. If your browser does not support MathML please install Firefox from Mozilla because AFAIK Firefox supports MathML.

Informal C Programming with GNU/Linux

Shiv Dayal

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 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”.


Dedication

Dedicated to my family and free software community

Table of Contents

Preface
1. Acknowledgements
1. Introduction
1.1. Why C?
1.2. History
1.3. Comparison with other Languages
1.4. How to Learn Programming?
1.5. What is a Computer Program?
1.6. Attributes of a Program
1.7. Tools of Trade
1.8. Bits and Bytes
1.9. Notes on Number System
1.10. Binary Number System
1.10.1. Conversion of Unsigned Decimals and Binaries
1.10.2. 2's Complement and 1's Complement
1.11. Compiling and Executing
2. Basics of C
2.1. The C Character Set
2.2. Keywords
2.3. Identifers
2.4. Programming
2.5. Data Types
2.5.1. Integers
2.5.2. Characters
2.5.3. Floating Types
2.5.3.1. Representation of Floating-Point Numbers
2.6. New Data Type of C99
2.6.1. Boolean Type
2.6.2. Complex Types
2.7. void and enum Types
2.8. Constants
2.9. Escape Sequences
3. Console I/O
3.1. C's Memory Model
3.2. printf
3.3. scanf
3.4. Character String I/O Functions
3.5. Single Character I/O
4. Operators and Expressions
4.1. Scope of an Identifier
4.2. Linkages of an Identifier
4.3. Storage Duration of Objects
4.4. Usual Arithmetic Conversions
4.5. Arithmetic Operators
4.6. Relational Operators
4.7. Equality Operators
4.8. Increment and Decrement Operators
4.9. Logical Operators
4.10. Bitwise Operators
4.11. Bitwise Shift Operators
4.12. Assignment Operators
4.13. Conditional Operators
4.14. Comma Operator
4.15. sizeof Operator
4.16. Unary Arithmetic Operators
4.17. Grouping parentheses
5. Control Flow
5.1. if else Statement
5.1.1. Dangling else Problem
5.2. switch Statement
5.3. while Loop
5.4. do-while Loop
5.5. for Loop
5.6. break and continue Statements
5.7. typedef and return Statements
5.8. goto statement
5.9. Examples
5.9.1. Implementing a loop using goto statement
5.9.2. Printing Various Patterns
5.9.3. Printing Pascal's triangle
5.9.4. Converting decimal numbers to binary numbers
6. Arrays and Pointers
6.1. Single-Dimensional Array
6.1.1. Notes on allocation of array
6.2. Multi-Dimensional Array
6.3. Pointers
6.4. Address and Indirection Operators
6.5. Arrays of Pointers
6.6. Pointers of Pointers
6.7. realloc() Function
6.8. free() Function
6.9. Constness
7. Functions
7.1. Pass by Value
7.2. Pass by Address
7.3. Passing arrays
7.4. Passing pointers
7.5. Recursion
7.6. Function like Macros
7.7. Inline Functions
7.8. Function Pointers
7.9. Passing and Receiving Function Pointers
7.10. Type Generic Functions
7.10.1. Another Example
7.10.2. Type Compatibilty in an Expression
8. Structures and Unions
8.1. Pointer members of a Structure
8.2. Usage of Structures and Unions
8.3. Structures and Arrays
8.4. Passing Structures to Functions
8.5. Bit Fields
8.6. Offset of Elements
9. Strings
9.1. Useful Functions
9.1.1. strlen and strnlen Functions
9.1.2. strcpy and strncpy Functions
9.1.3. strcat and strncat Functions
9.1.4. strcmp and strncmp Functions
9.1.5. strstr, strchr and strrchr Functions
9.1.6. strerror Function
10. File Input/Output
10.1. Text and Binary Files
10.2. Functions Related to Files
10.2.1. fopen and fclose Functions
10.2.2. fputc and fgetc Function
10.3. File Positioning Functions
10.3.1. fgetpos Function
10.3.2. fseek Function
10.3.3. fsetpos Function
10.3.4. ftell Function
10.3.5. rewind Function
10.4. Stream Buffering
10.4.1. fflush Function
10.5. Controlling Buffering
10.6. Peeking Ahead ungetc Function
10.7. Operation on Files
11. Preprocessing Directives
11.1. Conditional Inclusion
11.2. Source File Inclusion
11.3. Macro Replacement
11.3.1. Argument Substitution
11.3.2. The # Operator
11.3.3. The ## Operator
11.3.4. Rescanning and Further Replacement
11.3.5. Scope of Macro Definitions
11.4. Line Control
11.5. Error Directive
11.6. Pragma Directive
11.7. Null Directive
11.8. Predefined Macro Names
11.9. Pragma Operator
11.10. Usage
11.10.1. #include
11.10.2. Why we need headers?
11.10.3. #define
11.10.4. #undef
11.10.5. # and ##
11.10.6. #error
11.10.7. #pragma
11.10.8. Miscellaneous
12. Bit manipulation
12.1. Basics
12.2. Shifts and division
12.3. 2's Complement
12.4. Average of two numbers
12.5. Swapping two numbers without using a temporary
12.6. Finding next even or odd number
12.7. Operations on bits
12.7.1. Testing a bit
12.7.2. Setting a bit
12.7.3. Clearing a bit
12.7.4. Toggling a bit
12.7.5. Copying bits
12.8. Swapping even and odd bits of a number
13. Multi-threaded Programming
14. Tools for C Programming(gdb and gcc and others)
14.1. Most used gcc options
14.2. Multi-file programs, static and dynamic libraries
14.2.1. Creating static and dynamic libraries
14.3. Introduction to gdb
14.3.1. Invoking gdb
14.3.2. Getting help
15. The C Standard Library
15.1. Introduction
15.1.1. Definition of Terms
15.1.2. Standard Headers
15.1.3. Reserved Identifiers
15.1.4. Use of Library Functions
16. Diagnostics <assert.h>
16.1. Program Diagnostics
16.1.1. The assert Macro
17. Complex arithmetic <complex.h>
17.1. Introduction
17.2. Conventions
17.3. Branch Cuts
17.4. The CX_LIMITED_RANGE Pragma
17.5. Trigonometric functions
17.5.1. The cacos functions
17.5.2. The casin functions
17.5.3. The catan functions
17.5.4. The ccos functions
17.5.5. The csin functions
17.5.6. The ctan functions
17.6. Hyperbolic functions
17.6.1. The cacosh functions
17.6.2. The casinh function
17.6.3. The catanh functions
17.6.4. The ccosh functions
17.6.5. The csinh functions
17.6.6. The ctanh functions
17.7. Exponential and logarithmic functions
17.7.1. The cexp functions
17.7.2. The clog functions
17.8. Power and absolute-value functions
17.8.1. The cabs functions
17.8.2. The cpow functions
17.8.3. The csqrt functions
17.9. Manipulation functions
17.9.1. The carg functions
17.9.2. The cimag functions
17.9.3. The conj functions
17.9.4. The cproj functions
17.9.5. The creal function
18. Character Handling <ctype.h>
18.1. Character classification functions
18.1.1. The isalnum function
18.1.2. The isalpha function
18.1.3. The isblank function
18.1.4. The iscntrl function
18.1.5. The isdigit function
18.1.6. The isgraph function
18.1.7. The islower function
18.1.8. The isprint function
18.1.9. The ispunct function
18.1.10. The isspace function
18.1.11. The isupper function
18.1.12. The isxdigit function
18.2. Character case mapping functions
18.2.1. The tolower function
18.2.2. The toupper function
19. Errors <errno.h>
20. Floating-point environment <fenv.h>
20.1. The FENV_ACCESS pragma
20.2. Floating-point exceptions
20.2.1. The feclearexcept function
20.2.2. The fegetexceptflag function
20.2.3. The feraiseexcept function
20.2.4. The fesetexceptflag function
20.2.5. The fetestexcept function
20.3. Rounding
20.3.1. The fegetround function
20.3.2. The fesetround function
20.4. Environment
20.4.1. The fegetenv function
20.4.2. The feholdexcept function
20.4.3. The fesetenv function
20.4.4. The feupdateenv function
21. Characterictics of floating types <float.h>
22. Format conversion of integer types <inttypes.h>
22.1. Macros for format specifiers
22.2. Functions for greatest-width integer types
22.2.1. The imaxabs function
22.2.2. The imaxdiv function
22.2.3. The strtoimax and strtoumax functions
22.2.4. The wcstoimax and wcstoumax functions
23. Alternative spellings <iso646.h>
24. Sizes of integer types <limits.h>
25. Localization <locale.h>
25.1. Locale Control
25.1.1. The setlocale function
25.2. Numeric formatting convention inquiry
25.2.1. The localeconv function
26. Mathematics <math.h>
26.1. Treatment of error conditions
26.2. The FP_CONTRACT pragma
26.3. Classification macros
26.3.1. The fpclassify macro
26.3.2. The isfinite macro
26.3.3. The isinf macro
26.3.4. The isnan macro
26.3.5. The isnormal macro
26.3.6. The signbit macro
26.4. Trigonometric functions
26.4.1. The acos functions
26.4.2. The asin functions
26.4.3. The atan functions
26.4.4. The atan2 functions
26.4.5. The cos functions
26.4.6. The sin functions
26.4.7. The tan functions
26.5. Hyperbolic functions
26.5.1. The acosh functions
26.5.2. The asinh functions
26.5.3. The atanh functions
26.5.4. The cosh functions
26.5.5. The sinh functions
26.5.6. The tanh functions
26.6. Exponential and logrithmic functions
26.6.1. The exp functions
26.6.2. The exp2 functions
26.6.3. The expml functions
26.6.4. The frexp functions
26.6.5. The ilogb functions
26.6.6. The ldexp functions
26.6.7. The log functions
26.6.8. The log10 functions
26.6.9. The log1p functions
26.6.10. The log2 functions
26.6.11. The logb functions
26.6.12. The modf functions
26.6.13. The scalbn and scalbln functions
26.7. Power and absolute-value functions
26.7.1. The cbrt functions
26.7.2. The fabs function
26.7.3. The hypot functions
26.7.4. The pow functions
26.7.5. The sqrt functions
26.8. Error and gamma functions
26.8.1. The erf functions
26.8.2. The erfc functions
26.8.3. The lgamma functions
26.8.4. The tgamma functions
26.9. Nearest integer functions
26.9.1. The ceil functions
26.9.2. The floor functions
26.9.3. The nearbyint functions
26.9.4. The rint functions
26.9.5. The lrint and llrint functions
26.9.6. The round functions
26.9.7. The lround and llround functions
26.9.8. The trunc functions
26.10. Remainder functions
26.10.1. The fmod functions
26.10.2. The remainder functions
26.10.3. The remquo functions
26.11. Manipulation functions
26.11.1. The copysign function
26.11.2. The nan functions
26.11.3. The nextafter functions
26.11.4. The nexttoward functions
26.12. Maximum, minimum and positive difference functions
26.12.1. The fdim funcitons
26.12.2. The fmax function
26.12.3. The fmin functions
26.13. Floating multiply-add
26.13.1. The fma functions
26.14. Comparison macros
26.14.1. The isgreater macro
26.14.2. The isgreaterequal macro
26.14.3. The isless macro
26.14.4. The islessequal macro
27. Nonlocal jumps <setjmp.h>
27.1. Save calling environment
27.1.1. The setjmp macro
27.2. Restore calling environment
27.2.1. The longjmp function
28. Signal handling <signal.h>
28.1. Specify signal handling
28.1.1. The signal function
28.2. Send signal
28.2.1. The raise function
29. Alignment <stdalign.h>
30. Variable arguments <stdarg.h>
30.1. Variable argument list access macros
30.1.1. The va_arg macro
30.1.2. The va_copy macro
30.1.3. The va_end macro
30.1.4. The va_start macro
31. Atomics <stdatomic.h>
31.1. Introduction
31.2. Initialization
31.2.1. The ATOMIC_VAR_INIT macro
31.2.2. The atomic_init generic function
31.3. Order and consistency
31.3.1. The kill_dependency macro
31.4. Fences
31.4.1. The atomic_thread_fence function
31.4.2. The atomic_signal_fence function
31.5. Lock-free Property
31.5.1. The atomic_is_lock_free generic function
31.6. Atomic integer types
31.7. Operations on atomic types
31.7.1. The atomic_store generic functions
31.7.2. The atomic_load generic functions
31.7.3. The atomic_exchange generic functions
31.7.4. The atomic_compare_exchange generic functions
31.7.5. The atomic_fetch and modify generic functions
31.8. Atomic flag type and operations
31.8.1. The atomic_flag_test_and_set functions
31.8.2. The atomic_flag_clear functions
32. Boolean types and values <stdbool.h>
33. Common definitions <stddef.h>
34. Integer types <stdint.h>
34.1. Integer types
34.1.1. Exact-width integer types
34.1.2. Minimum-width integer types
34.1.3. Fastest minimum-width integer types
34.1.4. Integer types capable of holding object pointers
34.1.5. Greatest-width integer types
34.2. Limits of specific-width integer types
34.2.1. Limits of exact-width integer types
34.2.2. Limits of minimum-width integer types
34.2.3. Limits of fastest minimum-width integer types
34.2.4. Limits of integer types capable of holding object pointers
34.2.5. Limits of greatest-width integer types
34.3. Limits of other integer types
34.4. Macros for integer constants
34.4.1. Macros for minimum-width integer constants
34.4.2. Macros for greatest-width integer constants
35. Input/Output <stdio.h>
35.1. Introduction
35.2. Streams
35.3. Files
35.4. Operations on files
35.4.1. The remove function
35.4.2. The rename function
35.4.3. The tmpfile function
35.4.4. The tmpnam function
35.5. File acces functions
35.5.1. The fclose functions
35.5.2. The fflush function
35.5.3. The fopen function
35.5.4. The freopen function
35.5.5. The setbuf function
35.5.6. The setvbuf function
35.6. Formatted input/output function
35.6.1. The fprintf function
35.6.2. The fscanf function
35.6.3. The printf function
35.6.4. The scanf function
35.6.5. The snprintf function
35.6.6. The sprintf function
35.6.7. The scanf function
35.6.8. The vfprintf function
35.6.9. The vfscanf function
35.6.10. The vprintf function
35.6.11. The vscanf function
35.6.12. The vsnprintf function
35.6.13. The vsprintf function
35.6.14. The vsscanf function
35.7. Character input/output functions
35.7.1. The fgetc function
35.7.2. The fgets function
35.7.3. The fputc function
35.7.4. The fputs function
35.7.5. The getc function
35.7.6. The getchar function
35.7.7. The putc function
35.7.8. The putchar function
35.7.9. The puts function
35.7.10. The ungetc function
35.8. Direct input/output functions
35.8.1. The fread function
35.8.2. The fwrite function
35.9. File positioning functions
35.9.1. The fgetpos function
35.9.2. The fseek function
35.9.3. The fsetpos function
35.9.4. The ftell function
35.9.5. The rewind function
35.10. Error-handling functions
35.10.1. The clearerr function
35.10.2. The feof function
35.10.3. The ferror function
35.10.4. The perror function
36. General utilities <stdlib.h>
36.1. Numeric conversion functions
36.1.1. The atof function
36.1.2. The atoi, atol and atoll functions
36.1.3. The strtod, strtof and strtold functions
36.1.4. The strtol, strtoll, strtoul and strtoull functions
36.2. Pseudo-random sequence generation functions
36.2.1. The rand function
36.2.2. The srand function
36.3. Memory management functions
36.3.1. The aligned_alloc function
36.3.2. The calloc function
36.3.3. The free function
36.3.4. The malloc function
36.3.5. The realloc function
36.4. Communication with environment
36.4.1. The abort function
36.4.2. The atexit function
36.4.3. The at_quick_exit function
36.4.4. The exit function
36.4.5. The _Exit function
36.4.6. The getenv function
36.4.7. The quick_exit function
36.4.8. The system function
36.5. Searching and sorting utilities
36.5.1. The bsearch function
36.5.2. The qsort function
36.6. Integer arithmetic functions
36.6.1. The abs, labs and llabs functions
36.6.2. The div, ldiv and lldiv functions
36.7. Multibyte/wide character conversion functions
36.7.1. The mblen function
36.7.2. The mbtowc function
36.7.3. The wctomb function
36.8. Multibyte/wide string conversion functions
36.8.1. The mbstowcs function
36.8.2. The wcstombs function
37. _Noreturn <stdnoreturn.h>
38. String handling <string.h>
38.1. String function conventions
38.2. Copying functions
38.2.1. The memcpy function
38.2.2. The memmove function
38.2.3. The strcpy function
38.2.4. The strncpy function
38.3. Concatenation functions
38.3.1. The strcat function
38.3.2. The strncat function
38.4. Comparison functions
38.4.1. The memcmp function
38.4.2. The strcmp function
38.4.3. The strcoll function
38.4.4. The strncmp function
38.4.5. The strxfrm function
38.5. Search functions
38.5.1. The memchr function
38.5.2. The strchr function
38.5.3. The strcspn function
38.5.4. The strpbrk function
38.5.5. The strchr function
38.5.6. The strspn function
38.5.7. The strstr function
38.5.8. The strtok function
38.6. Miscellaneous functions
38.6.1. The memset function
38.6.2. The strerror function
38.6.3. The strlen function
39. Type-genetic math <tgmath.h>
40. Threads <threads.h>
40.1. Initialization functions
40.1.1. The call_once function
40.2. Condition variable functions
40.2.1. The cnd_broadcast function
40.2.2. The cnd_destroy function
40.2.3. The cnd_init function
40.2.4. The cnd_signal function
40.2.5. The cnd_timedwait function
40.2.6. The cnd_wait function
40.3. Mutes functions
40.3.1. The mtx_destroy function
40.3.2. The mtx_init function
40.3.3. The mtx_lock function
40.3.4. The mtx_timedlock function
40.3.5. The mtx_trylock function
40.3.6. The mtx_unlock function
40.4. Thread functions
40.4.1. The thrd_create function
40.4.2. The thrd_current function
40.4.3. The thrd_detach function
40.4.4. The thrd_equal function
40.4.5. The thrd_exit function
40.4.6. The thrd_join function
40.4.7. The thrd_sleep function
40.4.8. The thrd_yield function
40.5. Thread-specific storage functions
40.5.1. The tss_create function
40.5.2. The tss_delete function
40.5.3. The tss_get function
40.5.4. The tss_set function
41. Date and time <time.h>
41.1. Components of time
41.2. Time manipulation function
41.2.1. The clock function
41.2.2. The difftime function
41.2.3. The mktime function
41.2.4. The time function
41.2.5. The timespec_get function
41.3. Time conversion functions
41.3.1. The asctime function
41.3.2. The ctime function
41.3.3. The gmtime function
41.3.4. The localtime function
41.3.5. The strftime function
42. Unicode utilities <uchar.h>
42.1. Restartable multibyte/wide character conversion functions
42.1.1. The mbrtoc16 function
42.1.2. The c16rtomb function
42.1.3. The mbrtoc32 function
43. Extended multibyte and wide character utilities <wchar.h>
43.1. Formatted wide character input/output functions
43.1.1. The fwprintf function
43.1.2. The fwscanf function
43.1.3. The swprintf function
43.1.4. The swscanf function
43.1.5. The vfwprintf function
43.1.6. The vfwscanf function
43.1.7. The vswprintf function
43.1.8. The vswscanf function
43.1.9. The vwprintf function
43.1.10. The vwscanf function
43.1.11. The wprintf function
43.1.12. The wscanf function
43.2. Wide character input/output functions
43.2.1. The fgetwc function
43.2.2. The fgetws function
43.2.3. The fputwc function
43.2.4. The fputws function
43.2.5. The fwide function
43.2.6. The getwc function
43.2.7. The getwchar function
43.2.8. The putwc function
43.2.9. The putwchar function
43.2.10. The ungetwc function
43.3. General wide string utilities
43.3.1. Wide string numeric conversion functions
43.3.1.1. The wcstod, wcstof and wcstold functions
43.3.1.2. The wcstol, wcstoll, wcstoul and wcstoull functions
43.3.2. Wide string copying function
43.3.2.1. The wcspy function
43.3.2.2. The wcsncpy function
43.3.2.3. The wmemcpy function
43.3.2.4. The wmemmove function
43.3.3. Wide string concatenation function
43.3.3.1. The wcscat function
43.3.3.2. The wcsncat function
43.3.4. Wide string comparison functions
43.3.4.1. The wcscmp function
43.3.4.2. The wcscoll function
43.3.4.3. The wcsncmp function
43.3.4.4. The wcsxfrm function
43.3.4.5. The wmemcmp function
43.3.5. Wide string search function
43.3.5.1. The wcschr function
43.3.5.2. The wcsspn function
43.3.5.3. The wcspbrk function
43.3.5.4. The wcsrchr function
43.3.5.5. The wcsspn function
43.3.5.6. The wcsstr function
43.3.5.7. The wcstok function
43.3.5.8. The wmemchr function
43.3.6. Miscellaneous function
43.3.6.1. The wcslen function
43.3.6.2. The wmemset function
43.3.7. Wide character time conversion functions
43.3.7.1. The wcsftime function
43.4. Extended multibyte/wide character conversion utilities
43.4.1. Single-byte/wide character conversion function
43.4.1.1. The btowc function
43.4.1.2. The wctob function
43.4.2. Conversion state functions
43.4.2.1. The mbsinit function
43.4.3. Restartable multibyte/wide character conversion functions
43.4.3.1. The mbrlen function
43.4.3.2. The mbrtowc function
43.4.3.3. The wcrtomb function
43.4.4. Restartable multibyte/wide string conversion functions
43.4.4.1. The mbsrtowcs function
43.4.4.2. The wcsrtombs function
44. Wide character classification and mapping utilities <wctype.h>
44.1. Introduction
44.2. Wide character classification utilities
44.2.1. Wide character classification functions
44.2.1.1. The iswalnum function
44.2.1.2. The iswalpha function
44.2.1.3. The iswblank function
44.2.1.4. The iswcntrl function
44.2.1.5. The iswdigit function
44.2.1.6. The iswgraph function
44.2.1.7. The iswlower function
44.2.1.8. The iswprint function
44.2.1.9. The iswpunct function
44.2.1.10. The iswspace function
44.2.1.11. The iswupper function
44.2.1.12. The iswxdigit function
44.2.2. Extensible wide character classification functions
44.2.2.1. The iswctype function
44.2.2.2. The wctype function
44.3. Wide character case mapping utilities
44.3.1. Wide character case mapping functions
44.3.1.1. The towlower function
44.3.1.2. The towupper function
44.3.2. Extensible wide character case mapping functions
44.3.2.1. The towctrans function
44.3.2.2. The wctrans function
A. GNU Free Documentation License
Index

© 2010, 2016 Shiv S. Dayal. 10hash.com. GNU FDL license v1.3 or later is applicable where not stated.