The C programming language, chpt1, an intro

This chapter provides a quick intro to all the important components of the C language such as array, function and control flows. It also introduced some otherwise easily looked-over features that can simply our code or save us from bugs. I will document some important things I ran into.


Where does C come from

C is closely associated with UNIX since most of the system and programs on UNIX is written by it. That’s why C is called a system programming language. Many C’s idea comes from BCPL and also B. But they are typeless language. C, on the other hand, has types. The fundamental types are char, int, float of several sizes. The derived classes are created with pointers, array, struct and unions.

Battery not included

C doesn’t have operations deal directly with composite objects such as char strings and array. There are no operations that manipulate entire string or array. It also doesn’t define any storage allocation facility other than static definition and stack of local variable. It also doesn’t have garbage collection.

These functions must be provided by function falls. But thank god they are available through library.

This lack of battery is because we want to keep the language small.

Some miscellaneous facts about C

Since this is a quick intro, I will just collects some fun facts about C

Integer division truncates(discard fraction parts)


Printf is a output formatting function. Its first argument is a string of chars with each % indicating where one of the other arguments(second, third and so on) will be substituted and what format will be printed.

printf("%d, %d ", fahr, celsius);

The first % is matched with the second argument. They type must match.

BTW printf is not part of the C language. C doesn’t have input or output function. printf is in STL.

For or While?

We us for loop when the initialization and increment are single statements( such as ++) and are logically related.(such as iterating through a range in an array.)

Use symbolic constants instead of magic numbers

define name replacement_txt

Any occurrence of name will be replaced by replacement_txt

In convention, they are often in ALL CAP. And notice there is no semi-colon at the end.

Char input and output

Text stream

In C, text input or output, no matter where they are from, are considered as text stream. A text stream is a sequence of chars divided into lines

Reading file

#include <stdio.h>
    int c;
    c = getchar();
    while(c != EOF){
        c = getchar();

This function will read in from input and until it becomes EOF. It will keep reading.


EOF is end of file, a special int in <stdio.h>.

Why int instead of char

Because we want c to be big enough to hold EOF.

Assignment has value

In C, any assignment is an expression and has value, which is the value of the left hand after the assignment. We can put the assignment of c inside of the test part of a while loop and get:

#include <stdio.h>
    int c;
    while ((c = getchar())!= EOF){

Precedence of operators != > =

So c = getchar() != EOF is c = (getchar() != EOF) and c will be either 0 or 1.

Count chars

#include <stdio.h>
int main(){
    long nc;
    char c;
    for(nc = 0;( c = getchar() ) != EOF; nc ++){
    print("%d", nc);

Null statement in for

For statement needs a body so we put ; in it

Advantage of for and while for empty input over do while

If getchar() get EOF at the very beginning, then the loop body won’t be executed. Intelligently behavior when given zero-length input

Consecutive assignments

nl = nw = nc = 0;

Since each assignment is an expression with a value and assignments associate from right to left. so the above code is equivalent to nl = (nw = (nc = 0))

And and OR

They are evaluated from left to right and allows short circuit.

Char represent of digits

if (c >= '0' && c <= '9')

Determines if c is a digit. If it is, then the **numerical digit is ** c – ‘0’. This works as long as ‘0’…’9′ has consecutive representation. It is true for ASCII.

All functions are called by value

The arguments are given in temporary variables rather than the originals. But it is possible to have a function to modify an argument variable. We pass the address and access the variable indrectly to it.

For array, we only pass the first element’s address

No copying array elements!

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see