C Fundamentals

Even the most complex C programs can be reduced to a small set of building blocks that make up the fundamental components of the C programming language.

Example Program

#include <stdio.h>

/*
 * This program will demonstrate
 * some basic features of the C
 * programming language
 */
int main()
{
    // string variable declaration
    char * my_string;
    
    // string variable assignment
    my_string = "This is a string";
    
    // function call
    puts(my_string);
    
    return 0;
}

If you compile and run the code, you should expect to see the following output:

This is a string

Directives

Sometimes it’s helpful to execute code before your program is compiled. Any such code is referred to as a directive, sometimes called a preprocessor directive. Any directive in C will begin with #, and will be handled by the compiler before the rest of the program.

#include <stdio.h>

The include directive, which you may recognize from the previous lesson, tells the compiler to look for stdio before compiling the rest of the program. Because this is done before compiling any code, your compiler is able to recognize information you reference from stdio. There are other directives available in C, but include is probably the most common, and for now is the only one you really need to know about.

Statements

After the preprocessor has finished, the compiler begins looking for commands that will be run during program execution. Any code that falls under this description is called a statement. Different statements can serve different purposes, but will always end with a semicolon.

char * my_string;

One type of statement is a declaration. A declaration will consist of a data type followed by a identifier. The purpose of a declaration is to introduce a variable to your program. A variable is basically a placeholder for data. In the code shown above the data type is char *, or a string, and the identifier is my_string. The identifier will allow you to reference the variable at other places in your program.

my_string = "This is a string";

Another type of statement is an assignment. An assignment is closely related to a declaration, as it takes a variable introduced in a declaration and stores data in it. In our example program, we assign a string value of “This is a string” to my_string. Note that this can be done on the same line as a declaration, as shown below:

char * my_string = "This is a string";

Either way is perfectly correct, but whether we declare and assign on the same line or separate lines depends on the situation. In most cases, it will boil down to style preferences, but in some cases there will clearly be a better option.

puts(my_string);

The last type of statement we will cover is a function call. A function call allows you to write a single line of code that will execute one or more commands located elsewhere. The main advantage of function calls is that they allow you to take long or repetitive processes and condense them into a single line you can use as many times as you like. A function call will consist of the function identifier, followed by parentheses containing data that we would like to pass to the function. In our code show above, we are passing my_string to the puts function, which tells the program that we would like to print my_string to the console. The process of printing my_string the console might in reality be hundreds of lines of code, but in our case we have a single function to cleanly do it for us. As you will learn later on, you can also create your own functions.

Comments

Sometimes you will want to add notes to your code for other people to read. This could be for any reason. Maybe because you want to explain your logic, narrate your steps, or even tell other people what work still needs to be done. These notes are called comments, and their only purpose in your program is for people to read them. They are ignored by the compiler and won’t affect the amount of memory your program uses. In C, there are two ways to leave comments.

/*
 * This program will demonstrate
 * some basic features of the C
 * programming language
 */

The above code from our example program is a multi-line comment. It is a block of text that can be as big or small as you like, as long as it begins with /* and ends with */. Note that the * at the beginning of each intermediate line is not required, but I usually put them there since they line the text up nicely. But in some cases, a multi-line comment is not required.

// string variable declaration

// string variable assignment

// function call

The above comments from our code are single-line comments, and they are useful for cases where we only want to leave a brief message. A single-line comment begins with // and ends at the last character on a given line. That means anything above or below this type of comment is not treated as a comment.

<< prev | next >>

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x