C Programming/Preliminaries

Previous: Intro exercise Index Next: Compiling

Basic ConceptsEdit

Before one gets too deep into learning C syntax and programming constructs, it is beneficial to learn the meaning of a few key terms that are central to a thorough understanding of C.

Block Structure, Statements, Whitespace, and ScopeEdit

Now we 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 we say what a block is, what's a statement? One way to put it is that statements are 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 'i', and sets it to equal 6 */

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. Assignment (i = 6;), conditional and flow-control. 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,' separate from the last one */
        int i = 6;
    }
 
    return 0;
}

Blocks come in handy with readability and scope. You'll learn a little more about scope in a second.

Whitespace refers to the tab, space and newline/EOL (End Of Line) characters that separate the text characters that make up source code lines. 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 the same as

  printf (
  "Hello world") ; 


return 0;

The compiler simply skips over whitespace. However, it is common practice to use spaces (and tabs) to organize source code for human readability. You can use blocks without a conditional, loop, or other statement to organize your code.

In C, 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.

Scope describes the level at which a piece of data or a function is visible. There are two kinds of scope 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 scope.

int i = 5; /* this is a 'global' variable, anywhere in the program can access it */
 
/* 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 FunctionsEdit

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 LibraryEdit

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
Last modified on 9 November 2013, at 05:48