C Programming/Preliminaries

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Intro exercise Index Next: Compiling

Before learning C syntax and programming constructs, it is important to learn the meaning of a few key terms that are central in understanding C.

Block Structure, Statements, Whitespace, and Scope[edit]

Sentences delimited with '/*' and '*/' are comments, and the compiler ignores them. They are described in Programming Structure and Style

Now we are about to discuss the basic structure of a C program. If you're familiar with PASCAL, you may have heard it referred to as a block-structured language. C does not have complete block structure (and you'll find out why when you go over functions in detail) but it is still very important to understand what blocks are and how to use them.

So what is in a block? Generally, a block consists of executable statements.

Before blocks are explained, what is a statement? One way to put it is that statements are the text the compiler will attempt to turn into executable instructions, and the whitespace that surrounds them. An easier way to put it is that statements are bits of code that do things, like this:

int i = 6;

This declares a variable of type integer, initializes it to the value 6, which can be later accessed with the identifier 'i'. The various data types are introduced in the chapter Variables.

You might have noticed the semicolon at the end of the statement. Statements in C always end with a semicolon (;) character. Leaving off the semicolon is a common mistake that a lot of people make, beginners and experts alike! So until it becomes second nature, be sure to double check your statements!

Since C is a "free-format" language, several statements can share a single line in the source file, like so:

/* this declares the variables 'i', 'test', 'foo', and 'bar'
    note that ONLY 'bar' is set to six! */
int i, test, foo, bar = 6;

There are several kinds of statements, and you've seen some of them, such as the assignment (i = 6;). A substantial portion of this book deals with statement construction.

Now back to blocks. In C, blocks begin with an opening brace "{" and end with a closing brace "}". Blocks can contain other blocks which can contain their own blocks, and so on.

Let's show an example of blocks.

int main(void)
    /* this is a 'block' */
    int i = 5;

        /* this is also a 'block', nested inside the outer block */
        int i = 6;

    return 0;

Whitespace refers to the tab, space and newline characters that separate the text characters that make up the source code. Like many things in life, it's hard to appreciate whitespace until it's gone. To a C compiler, the source code

    printf("Hello world"); return 0;

is the same as

    printf("Hello world");
    return 0;

which is also the same as

    printf (
    "Hello world") ;

    return 0;

The compiler simply ignores whitespace. However, it is common practice to use spaces (or tabs) to organize source code for human readability. You can use blocks without preceding them with statements, such as the main function declaration (and the other statements to be taugh).

Most of the time we do not want other functions or other programmer's routines accessing data that we are currently manipulating. This is why it is important to understand the concept of scope.

Scopes describes the level at which a piece of data or a function is visible. There are two kinds of scopes in C, local and global. When we speak of something being global, we speak of something that can be seen or manipulated from anywhere in the program. When we speak of something being local, we speak of something that can be seen or manipulated only within the block it was declared.

Let's show some examples, to give a better picture of the idea of scopes.

int i = 5; /* this is a 'global' variable, it can be accessed from anywhere in the program */

/* this is a function, all variables inside of it
    are "local" to the function. */
int main(void)
    int i = 6; /* 'i' now equals 6 */
    printf("%d\n", i); /* prints a '6' to the screen, instead of the global variable of 'i', which is 5 */

    return 0;

That shows a decent example of local and global, but what about different scopes inside of functions? (you'll learn more about functions later, for now, just focus on the "main" part.)

/* the main function */
int main(void)
    /* this is the beginning of a 'block', you read about those above */

    int i = 6; /* this is the first variable of this 'block', 'i' */

        /* this is a new 'block', and because it's a different block, it has its own scope */

        /* this is also a variable called 'i', but in a different 'block',
            because it's in a different 'block' than the old 'i', it doesn't affect the old one! */
        int i = 5;
        printf("%d\n", i); /* prints a '5' onto the screen */
    /* now we're back into the old block */

    printf("%d\n", i); /* prints a '6' onto the screen */

    return 0;

Basics of Using Functions[edit]

Functions are a big part of programming. A function is a special kind of block that performs a well-defined task. If a function is well-designed, it can enable a programmer to perform a task without knowing anything about how the function works. The act of requesting a function to perform its task is called a function call. Many functions require a caller to hand it certain pieces of data needed to perform its task; these are called arguments. Many functions also return a value to the caller when they're finished; this is called a return value (the return value in the above program is 0).

The things you need to know before calling a function are:

  • What the function does
  • The data type (discussed later) of the arguments and what they mean
  • The data type of the return value and what it means

All code other than global data definitions and declarations needs to be a part of a function.

Usually, you're free to call a function whatever you wish to. The only restriction is that every executable program needs to have one, and only one, main function, which is where the program begins executing.

We will discuss functions in more detail in a later chapter, C Programming/Procedures and functions.

The Standard Library[edit]

In 1983, when C was in the process of becoming standardized, the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C known as "ANSI C". That standard specification created a basic set of functions common to each implementation of C, which is referred to as the Standard Library. The Standard Library provides functions for tasks such as input/output, string manipulation, mathematics, files, and memory allocation. The Standard Library does not provide functions that are dependent on specific hardware or operating systems, like graphics, sound, or networking. In the "Hello, World", program, a Standard Library function is used (printf) which outputs lines of text to the standard output stream.

Previous: Intro exercise Index Next: Compiling