Chapters 1-4 Notes – Practical C Programming, 3rd Edition By Steve Oualline

O Reilly - Practical C Programming, 3rd Edition.pdf

Chapter 1 gives a brief description of the C language and its use. This chapter includes some background on the history of the language.

There is only one way to learn how to program and that is to write programs. You'll

learn a lot more by writing and debugging programs than you ever will by reading

this book.

Chapter 2 explains the basic programming process and gives you enough

information to write a very simple program.

/*File:hello/hello.c*/ /*comments - prog source file name req*/

#include <stdio.h> /*comments – format for std input/output library for all progs*/

int main() /*comments – integer type null for “main” for all progs, so 0 ouput is no fault, 1 or other is fault*/

{ /*comments – required nest bracket1 for main*/

printf("Hello World\n"); /*comments – note new line char \n*/

return (0); /*comments – return no output on prog success */

} /*comments – required nest bracket2 for main*/

$ gcc -o hello hello.c

$ ls -l hello

-rwxr-xr-x 1 stevee stevee 8551 Jun 6 11:26 hello

$ ./hello

Hello World

man gcc


When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.

Why I don't use debugger or -Wall (all Warnings; gives about same as -g ) in Mint at this stage:

$ gcc -og hello hello.c

hello: In function `_fini':

(.fini+0x0): multiple definition of `_fini'

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 0 has invalid symbol index 7

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 1 has invalid symbol index 8

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 2 has invalid symbol index 9

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 0 has invalid symbol index 4

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 1 has invalid symbol index 4

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 2 has invalid symbol index 5

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 3 has invalid symbol index 5

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here

hello: In function `data_start':

(.data+0x0): multiple definition of `__data_start'

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here

hello: In function `data_start':

(.data+0x8): multiple definition of `__dso_handle'

/usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o:(.data+0x0): first defined here

hello:(.rodata+0x0): multiple definition of `_IO_stdin_used'

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here

hello: In function `_start':

(.text+0x0): multiple definition of `_start'

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:/build/eglibc-3GlaMS/eglibc-2.19/csu/../sysdeps/x86_64/start.S:118: first defined here

hello: In function `_init':

(.init+0x0): multiple definition of `_init'

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_line): relocation 0 has invalid symbol index 4

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_line): relocation 1 has invalid symbol index 5

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o:/build/eglibc-3GlaMS/eglibc-2.19/csu/../sysdeps/x86_64/crti.S:64: first defined here

/tmp/cc7gKOEV.o: In function `main':

hello.c:(.text+0x0): multiple definition of `main'

hello:(.text+0xed): first defined here

/usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'

hello:(.data+0x10): first defined here

/usr/bin/ld: error in hello(.eh_frame); no .eh_frame_hdr table will be created.

collect2: error: ld returned 1 exit status

Chapter 3 discusses programming style. Commenting a program is covered, as well

as writing clear and simple code.

Contrary to popular belief, programmers do not spend most of their time writing

programs. Far more time is spent maintaining, upgrading, and debugging existing

code than is ever spent on creating new wo rks. According to Datamation, the

amount of time spent on maintenance is skyrocketing. From 1980 to 1990, the

average number of lines in a typical application went from 23,000 to 1,200,000. The

average system age went from 4.75 to 9.4 years.

What's worse, 74% of the managers surveyed at the 1990 Annual Meeting and

Conference of the Software Maintenance Association reported that they "have

systems in their department, that have to be maintained by specific individuals

because no one else understands them."

Programs written only for the machine have two problems:

They are difficult to correct because sometimes even the author does not

understand them.

Modifications and upgrades are difficult to make because the maintenance

programmer must spend a considerable amount of time figuring out what

the program does from its code. Ideally, a program serves two purposes:

first, it presents the computer with a set of instructions, and second, it

provides the programmer with a clear, easy-to -read description of what the

program does…A working but uncommented program is a time bomb waiting to explode. Sooner or

later, someone will have to fix a bug in the program, modify it, or upgrade it, and the

lack of comments will make the job much more difficult. A well -commented, simple

program is a work of art. Learning how to comment is as important as learning how

to code properly.”


* hello -- program to print out "Hello World". *

* *

Not an especially earth-shattering program.


* Author:

Steve Oualline.


* Purpose:

Demonstration of a simple program.


* Usage:


Runs the program and the message appears.



#include <stdio.h>

int main()


/* Tell the world hello */

printf("Hello World\n");

return (0);


To set vim to use abbreviations for Comment Blocks:

vi .vimrc

:ab #b /********************************************************

:ab #e *********************************************************/

In a new doc, press i for insert text, then “#b” to add the comment stars.

A variable is a place in the computer's memory for storing a value. C identifies that

place by the variable name. e.g.

int account_number; /* Index for account table */

int balance_owed; /* Total owed us (in pennies)*/

(Programming tools like editors, cross -referencers, and searching tools such as

grep can also help you quickly find a variable's definition.)

In order to make programs easier to understand, most programmers indent their

programs. The general rule for a C program is to indent one level for each new block

or conditional.

while (! done)





if (total <= 0)


printf("You owe nothing\n");

total = 0;




printf("You owe %d dollars\n", total);

all_totals = all_totals + total;


Chapter 4 introduces you to simple C statements. Basic variables and the

assignment statement are covered in detail, along with arithmetic operators +, -, *,

/, and %.

The basic structure of a one-function program is:


* ...Heading comments...



...Data declarations...

int main()


...Executable statements...

return (0);


Operator Meaning

* Multiply

/ Divide

+ Add

- Subtract

% Modulus (return the remainder after division)

The general form of a variable declaration is:

type name; /* comment */

where type is one of the C variable types ( int, float, etc.) and name is any valid

variable name.

On most UNIX machines, integers are 32 bits (4 bytes), providing a range of

2147483647 (2 31 -1) to -2147483648. On the PC, most compilers use only 16 bits (2

bytes), so the range is 32767 (2 15 -1) to -32768. These sizes are typical. The standard header file limits.h defines constants for the various numerical limits.

#include <stdio.h> /*std input,output*/

#include <math.h> /*required for pow function*/

double answer; /* integer result of our expression via %f*/

int main() /* always required main function */


answer = pow(2 , 32) ; /*cant use ^ for power*/

printf("Answer is 2 ^ 32 or %.0f\n", answer ); /*to give no decimals*/

return (0);


$ ./maxbitnumber

Answer is 2 ^ 32 or 4294967296


#include <stdio.h>

int term;

/* term used in two expressions */

int main()


term = 3 * 5;

printf("Twice %d is %d\n", term, 2*term);

printf("Three times %d is %d\n", term, 3*term);

return (0);


$ ./twiceterm

Twice 15 is 30

Three times 15 is 45

The division operator is special. There is a vast difference between an integer divide

and a floating-point divide. In an integer divide, the result is truncated (any

fractional part is discarded). So the value of 19/10 is 1.

If either the d ivisor or the dividend is a floating-point number, a floating -point divide

is executed. So 19.0/10.0 is 1.9. (19/10.0 and 19.0/10 are also floating -point

divides; however, 19.0/10.0 is preferred for clarity.)

int integer; /* an integer */

float floating; /* a floating-point number */

int main()


floating = 1.0 / 2.0; /* assign "floating" 0.5 */

integer = 1 / 3; /* assign integer 0 */

floating = (1 / 2) + (1 / 2); /* assign floating 0.0 */

floating = 3.0 / 2.0; /* assign floating 1.5 */

integer = floating; /* assign integer 1 */

return (0);


Question 4 -3: Why does 2 + 2 = 5928? (Your results may vary. See Example 4 -5.)


#include <stdio.h>

/* Variable for computation results */

int answer;

int main()


answer = 2 + 2;

printf("The answer is %d\n");

return (0);


$ ./two

The answer is 1866226024

The printf function has no "answer" def to call for %d:

printf("The answer is %d\n" , answer);

$ ./two

The answer is 4

Chapter 5 covers arrays and more co mplex variables. Shorthand operators such as

++ and %= are also described.

Chapter 6 explains simple decision statements including if, else, and for. A

discussion of == versus = is presented.

Chapter 7 takes you through all the necessary steps to create a simple program

from specification through release. Structured programming, fast prototyping, and

debugging are also discussed.

Chapter 8 describes additional control statements. Included are while, break, and continue. The switch statement is discussed in detail.

Chapter 9 introduces local variables, functions, and parameters.

Chapter 10 describes the C preprocessor, which gives the prog rammer tremendous

flexibility in writing code. The chapter also provides the programmer with a

tremendous number of ways to mess up. Simple rules that help keep the

preprocessor from becoming a problem are described.

Chapter 11 discusses the logical C operators that work on bits.

Chapter 12 explains structures and other advanced types. The sizeof operator an d

the enum type are included.

Chapter 13 introduces C pointer variables and shows some of their uses.

Chapter 14 describes both buffered and unbuffered input/output. ASCII and binary

files are discussed, and you are shown how to construct a simple file.

Chapter 15 describes how to de bug a program, as well as how to use an interactive

debugger. You are shown not only how to debug a program, but also how to write a

program so that it is easy to debug. This chapter also describes many optimization

techniques for making your program run faster and more efficiently.

Chapter 16 uses a simple decimal floating-point format to introduce you to the

problems inherent in floating point, such as roundoff error, precision lo ss, overflow,

and underflow.

Chapter 17 describes advanced uses of pointers for constructing dynamic structures

such as linked lists and trees.

Chapter 18 shows how to split a program into several files and use modular

programming techniques. The make utility is explained in more detail.

Chapter 19 describes the old, pre -ANSI C language and associated compilers.

Although such compilers are rare today, a lot of code was written for them and there

are still a large number of programs out there that use the old syntax.

Chapter 20 describes the problems that can occur when you port a program (move

it from one machine to another).

Chapter 21 describes the do/while statement, the , operator, and the ? and :


Chapter 22 details the steps necessary to take a complex program from conception

to completion. Information -hiding and modular programming techniques are


Chapter 23 lists some programming adages that will help you construct good C


Comments are closed.

Post Navigation