KEMBAR78
Chapter 1 - Why Learn C | PDF | Pointer (Computer Programming) | Computer Programming
0% found this document useful (0 votes)
57 views13 pages

Chapter 1 - Why Learn C

The document is a comprehensive guide to learning the C programming language, covering its history, usage, and fundamental concepts. It includes sections on using a compiler, variables, error handling, input/output, and advanced topics such as memory management and networking. Additionally, it discusses the significance of C as a foundational language for many modern programming languages and operating systems.

Uploaded by

D3ath L3g3nd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views13 pages

Chapter 1 - Why Learn C

The document is a comprehensive guide to learning the C programming language, covering its history, usage, and fundamental concepts. It includes sections on using a compiler, variables, error handling, input/output, and advanced topics such as memory management and networking. Additionally, it discusses the significance of C as a foundational language for many modern programming languages and operating systems.

Uploaded by

D3ath L3g3nd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C Programming

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

3 What you need before you can learn 9


3.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Footnotes ................................................................................................................ 12

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

9 Structure and style 31


9.1 C Structure and Style........................................................................................ 31
9.2 Introduction ............................................................................................................ 31
9.3 Line Breaks and Indentation ............................................................................ 32
9.4 Comments ............................................................................................................... 34
9.5 References .................................................................................................................... 37

10 Variables 39
10.1 Declaring, Initializing, and Assigning Variables .................................................. 39
10.2 Literals .................................................................................................................... 41
10.3 The Four Basic Data Types ................................................................................... 41

10.4 sizeof ................................................................................................................ 43


10.5 Data type modifiers ................................................................................................ 44
10.6 const qualifier ............................................................................................................. 45
10.7 Magic numbers ...................................................................................................... 45
10.8 Scope ............................................................................................................................ 46
10.9 Other Modifiers ...................................................................................................... 46
10.10 References .................................................................................................................... 50

11 Error handling 51
11.1 Preventing divide by zero errors ........................................................................... 52
11.2 Signals .......................................................................................................................... 52
11.3 setjmp ..................................................................................................................... 53

12 Simple Input and Output 55


12.1 Output using printf() ....................................................................................... 55
12.2 Other output methods ....................................................................................... 58
12.3 Input using scanf() .......................................................................................... 58
12.4 References .................................................................................................................... 59

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

16 Procedures and functions 93


16.1 More on functions .................................................................................................. 94
16.2 Writing functions in C ........................................................................................... 94

16.3 Using C functions .................................................................................................. 96


16.4 Functions from the C Standard Library ........................................................... 97
16.5 Variable-length argument lists ............................................................................... 99
16.6 References .................................................................................................................. 101
17 Preprocessor 103
17.1 Directives .................................................................................................................... 103
17.2 Useful Preprocessor Macros for Debugging ....................................................... 113

18 Libraries 119
18.1 What to put in header files ............................................................................. 121
18.2 References .................................................................................................................. 122

19 Standard libraries 123


19.1 History .................................................................................................................. 123
19.2 Design ......................................................................................................................... 124
19.3 ANSI Standard ..................................................................................................... 124
19.4 Common support libraries ................................................................................... 126
19.5 Compiler built-in functions .................................................................................. 126
19.6 POSIX standard library ........................................................................................ 126
19.7 References .................................................................................................................. 127

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

21 Beginning exercises 155


21.1 Variables ............................................................................................................... 155
21.2 Simple I/O ....................................................................................................... 156
21.3 Program Flow ....................................................................................................... 156
21.4 Functions .............................................................................................................. 157
21.5 Math...................................................................................................................... 157
21.6 Recursion .................................................................................................................... 157

22 In-depth C ideas 161

23 Arrays 163
23.1 Arrays ................................................................................................................... 163
23.2 Strings ................................................................................................................... 165

23.3 References .................................................................................................................. 166

24 Pointers and arrays 167


24.1 Declaring pointers ................................................................................................ 168
24.2 Assigning values to pointers ............................................................................... 168
24.3 Pointer dereferencing ........................................................................................... 169
24.4 Pointers and Arrays ........................................................................................ 170
24.5 Pointers in Function Arguments .......................................................................... 173
24.6 Pointers and Text Strings ............................................................................... 173
24.7 Pointers to Functions ...................................................................................... 174
24.8 Practical use of function pointers in C ............................................................... 175
24.9 Examples of pointer constructs ........................................................................... 177
24.10 sizeof .......................................................................................................................... 178
24.11 External Links ...................................................................................................... 180

25 Memory management 181


25.1 The malloc function ........................................................................................... 181
25.2 The calloc function............................................................................................. 183
25.3 The realloc function ........................................................................................... 183
25.4 The free function ................................................................................................ 183
25.5 References .................................................................................................................. 185

26 Strings 187
26.1 Syntax ................................................................................................................... 187
26.2 The <string.h> Standard Header ...................................................................... 188
26.3 Examples .............................................................................................................. 201
26.4 References .................................................................................................................. 201

27 Complex types 203


27.1 Data structures ................................................................................................ 203
27.2 Enumerations ....................................................................................................... 204

28 Networking in UNIX 207


28.1 A simple client ..................................................................................................... 207
28.2 A simple server .................................................................................................... 209
28.3 Useful network functions..................................................................................... 210
28.4 FAQs .................................................................................................................... 210

29 Common practices 211


29.1 Dynamic multidimensional arrays ....................................................................... 211
29.2 Constructors and destructors .......................................................................... 213
29.3 Nulling freed pointers .......................................................................................... 214
29.4 Macro conventions ............................................................................................... 214
29.5 Further reading ..................................................................................................... 215

30 C and beyond 217

31 Particularities of C 219
31.1 References .................................................................................................................. 221

32 Language Overloading and Extensions 223


32.1 External links ....................................................................................................... 223

33 Combining Languages 225


33.1 Assembler ................................................................................................................... 225
33.2 Cg ............................................................................................................................... 225
33.3 Java.................................................................................................................. 226
33.4 Perl........................................................................................................................ 226
33.5 Python ............................................................................................................. 226
33.6 For further reading ............................................................................................... 227
33.7 References .................................................................................................................. 227

34 Object Oriented Programming: The GObject System 229


34.1 Object-Creation .................................................................................................... 229
34.2 Inheritance ............................................................................................................ 231
34.3 Further reading ..................................................................................................... 232

35 Computer Programming 233

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

37 C Reference Tables 239

38 C Language Reference 241


38.1 Table of keywords ............................................................................................... 241
38.2 Table of operators ................................................................................................ 242
38.3 Table of data types ............................................................................................... 244
38.4 Character sets .................................................................................................. 249
38.5 References .................................................................................................................. 249

39 POSIX Reference 251


39.1 Header files .......................................................................................................... 251
39.2 References .................................................................................................................. 253
39.3 Bibliography ............................................................................................................... 253

40 C Compilers Reference List 255


40.1 Free (or with a free version) ................................................................................ 255
40.2 Paid.................................................................................................................. 256

41 Contributors 257

List of Figures 277

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?

w:C (programming language)1


C2 is the most commonly used programming language for writing operating systems 3. The
first operating system written in C is Unix4. Later operating systems like GNU/Linux 5
were all written in C. Not only is C the language of operating systems, it is the precursor
and inspiration for almost all of the most popular high-level languages available today. In
fact, Perl6, PHP7, Python8 and Ruby9 are all written in C.
By way of analogy, let's say that you were going to be learning Spanish, Italian, French, or
Romanian. Do you think knowing Latin would be helpful? Just as Latin was the basis of
all of those languages, knowing C will enable you to understand and appreciate an entire
family of programming languages built upon the traditions of C. Knowledge of C enables
freedom.

1.0.1 Why C, and not assembly language?

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...

1.0.2 Why C, and not another language?

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.

You might also like