Chapter 1 - Why Learn C
Chapter 1 - Why Learn C
en.wikibooks.org
December 15, 2017
On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia
projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A
URI to this license is given in the list of figures on page 277. If this document is a derived work
from the contents of one of these projects and the content was still licensed by the project under
this license at the time of derivation this document has to be licensed under the same, a similar or a
compatible license, as stated in section 4b of the license. The list of contributors is included in chapter
Contributors on page 257. The licenses GPL, LGPL and GFDL are included in chapter Licenses on
page 281, since this book and/or parts of it may or may not be licensed under one or more of these
licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of
figures on page 277. This PDF was generated by the LATEX typesetting software. The LATEX source
code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from
the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www.
pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save
the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z.
To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX
source itself was generated by a program written by Dirk Hünniger, which is freely available under
an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf.
Contents
1 Why learn C? 3
2 History 7
4 Using a Compiler 13
5 Beginning C 17
6 Intro exercise 19
6.1 The ”Hello, World!”Program ................................................................................ 19
6.2 References .................................................................................................................... 21
7 Preliminaries 23
7.1 Block Structure, Statements, Whitespace, and Scope .......................................... 23
7.2 Basics of Using Functions ..................................................................................... 25
7.3 The Standard Library ............................................................................................. 26
7.4 References .................................................................................................................... 26
8 Compiling 27
8.1 Preprocessor ........................................................................................................... 27
8.2 Syntax Checking .................................................................................................... 28
8.3 Object Code ............................................................................................................ 28
8.4 Linking ......................................................................................................................... 28
8.5 Automation ............................................................................................................. 29
8.6 References .................................................................................................................... 30
10 Variables 39
10.1 Declaring, Initializing, and Assigning Variables .................................................. 39
10.2 Literals .................................................................................................................... 41
10.3 The Four Basic Data Types ................................................................................... 41
11 Error handling 51
11.1 Preventing divide by zero errors ........................................................................... 52
11.2 Signals .......................................................................................................................... 52
11.3 setjmp ..................................................................................................................... 53
13 Simple math 61
13.1 Arithmetic Operators ............................................................................................. 61
13.2 Assignment Operators ............................................................................................ 62
13.3 Logical Operators .................................................................................................. 62
13.4 Relational and Equality Operators ........................................................................ 63
13.5 Type Casting .......................................................................................................... 64
13.6 The Shift Operators (which may be used to rotate bits) ................................. 64
13.7 Bitwise Operators .................................................................................................. 66
13.8 Comma Operator .............................................................................................. 66
13.9 References .................................................................................................................... 67
14 Further math 69
14.1 Trigonometric functions ........................................................................................ 69
14.2 Hyperbolic functions .............................................................................................. 70
14.3 Exponential and logarithmic functions ................................................................. 71
14.4 Power functions ..................................................................................................... 73
14.5 Nearest integer, absolute value, and remainder functions .................................... 74
14.6 Error and gamma functions ................................................................................... 76
14.7 References .................................................................................................................... 77
15 Control 79
15.1 Conditionals ........................................................................................................... 79
15.2 Loops ............................................................................................................................ 86
15.3 One last thing: goto ............................................................................................... 90
15.4 Examples ................................................................................................................ 91
15.5 References .................................................................................................................... 92
18 Libraries 119
18.1 What to put in header files ............................................................................. 121
18.2 References .................................................................................................................. 122
20 File IO 129
20.1 Introduction .......................................................................................................... 129
20.2 Streams ................................................................................................................. 129
20.3 Standard Streams ............................................................................................ 130
20.4 FILE pointers ............................................................................................................. 130
20.5 Opening and Closing Files .................................................................................. 131
20.6 Other file access functions ................................................................................... 132
20.7 Functions that Modify the File Position Indicator ......................................... 133
20.8 Error Handling Functions .................................................................................... 135
20.9 Other Operations on Files .................................................................................... 136
20.10 Reading from Files ............................................................................................... 137
20.11 Writing to Files ............................................................................................... 145
20.12 References .................................................................................................................. 153
23 Arrays 163
23.1 Arrays ................................................................................................................... 163
23.2 Strings ................................................................................................................... 165
26 Strings 187
26.1 Syntax ................................................................................................................... 187
26.2 The <string.h> Standard Header ...................................................................... 188
26.3 Examples .............................................................................................................. 201
26.4 References .................................................................................................................. 201
31 Particularities of C 219
31.1 References .................................................................................................................. 221
36 Statements 235
36.1 Labeled Statements .............................................................................................. 235
36.2 Compound Statements ......................................................................................... 235
36.3 Expression Statements ......................................................................................... 236
36.4 Selection Statements ............................................................................................ 236
36.5 Iteration Statements ........................................................................................ 236
36.6 Jump Statements ............................................................................................. 237
41 Contributors 257
Contents
42 Licenses 281
42.1 GNU GENERAL PUBLIC LICENSE ................................................................ 281
42.2 GNU Free Documentation License ..................................................................... 282
42.3 GNU Lesser General Public License .................................................................. 283
1 Why learn C?
While assembly language10 can provide speed and maximum control of the program, C
provides portability.
Different processors are programmed using different Assembly languages and having to
choose and learn only one of them is too arbitrary. In fact, one of the main strengths of C
is that it combines universality and portability across various computer architectures while
retaining most of the control of the hardware provided by assembly language.
For example, C programs can be compiled and run on the HP 50g calculator (ARM11
processor), the TI-89 calculator (6800012 processor), Palm OS Cobalt smartphones (ARM
processor), the original iMac (PowerPC13), the Arduino (Atmel AVR14), and the Intel iMac
1 https://en.wikipedia.org/wiki/C%20%28programming%20language%29
2 https://en.wikipedia.org/wiki/C%20%28programming%20language%29
3 https://en.wikipedia.org/wiki/operating%20systems
4 https://en.wikipedia.org/wiki/Unix
5 https://en.wikipedia.org/wiki/Linux
6 https://en.wikipedia.org/wiki/Perl
7 https://en.wikipedia.org/wiki/PHP
8 https://en.wikipedia.org/wiki/Python%20%28programming%20language%29
9 https://en.wikipedia.org/wiki/Ruby_%28programming_language%29
10 https://en.wikipedia.org/wiki/Assembly%20language
11 https://en.wikipedia.org/wiki/ARM%20architecture
12 https://en.wikipedia.org/wiki/Motorola%2068000
13 https://en.wikipedia.org/wiki/PowerPC
14 https://en.wikipedia.org/wiki/Atmel%20AVR
Why learn C?
(Intel15 Core 2 Duo). Each of these devices has its own assembly language that is completely
incompatible with the assembly language of any other.
Assembly, while extremely powerful, is simply too difficult to program large applications
and hard to read or interpret in a logical way. C is a compiled language, which creates fast
and efficient executable files. It is also a small “what you see is all you get” language: a
C statement corresponds to at most a handful of assembly statements, everything else is
provided by library functions.
So is it any wonder that C is such a popular language?
Like toppling dominoes, the next generation of programs follows the trend of its ancestors.
Operating systems designed in C always have system libraries designed in C. Those system
libraries are in turn used to create higher-level libraries (like OpenGL16, or GTK17), and
the designers of those libraries often decide to use the language the system libraries used.
Application developers use the higher-level libraries to design word processors, games, media
players and the like. Many of them will choose to program in the language that the higher-
level library uses. And the pattern continues on and on and on...
The primary design of C is to produce portable code while maintaining performance and
minimizing footprint (CPU time18, memory19 usage, disk I/O, etc.). This is useful for
operating systems20, embedded systems21 or other programs where performance matters a
lot (“high-level” interface would affect performance). With C it’s relatively easy to keep
a mental picture of what a given line really does, because most of the things are written
explicitly in the code. C has a big codebase for low level applications. It is the “native”
language of UNIX22, which makes it flexible and portable. It is a stable and mature language
which is unlikely to disappear for a long time and has been ported to most, if not all,
platforms.
One powerful reason is memory allocation. Unlike most programming languages, C allows
the programmer to write directly to memory. Key constructs in C such as structs, pointers
and arrays are designed to structure and manipulate memory in an efficient, machine-
independent fashion. In particular, C gives control over the memory layout of data struc-
tures. Moreover dynamic memory allocation is under the control of the programmer (which
also means that memory deallocation has to be done by the programmer). Languages like
Java23 and Perl shield the programmer from having to manage most details of memory
allocation and pointers (except for memory leaks24 and some other forms of excess memory
15 https://en.wikipedia.org/wiki/x86
16 https://en.wikipedia.org/wiki/OpenGL
17 https://en.wikipedia.org/wiki/GTK
18 https://en.wikipedia.org/wiki/CPU%20time
19 https://en.wikipedia.org/wiki/Computer%20memory
20 https://en.wikipedia.org/wiki/Operating%20systems
21 https://en.wikipedia.org/wiki/Embedded%20systems
22 https://en.wikipedia.org/wiki/Unix
23 https://en.wikipedia.org/wiki/Java%20%28programming%20language%29
24 https://en.wikipedia.org/wiki/Memory%20leak
Contents
usage). This can be useful since dealing with memory allocation when building a high-level
program is a highly error-prone process. However, when dealing with low-level code such
as the part of the OS that controls a device, C provides a uniform, clean interface. These
capabilities just do not exist in most other languages.
While Perl, PHP, Python and Ruby may be powerful and support many features not pro-
vided by default in C, they are not normally implemented in their own language. Rather,
most such languages initially relied on being written in C (or another high-performance pro-
gramming language), and would require their implementation be ported to a new platform
before they can be used.
As with all programming languages, whether you want to choose C over another high-level
language is a matter of opinion and both technical and business requirements could dictate
which language is required.