BlueSmoke - Guide : Fundamental C Programming (Rev 1.0)

 Date  : Feb 17th, 2001
 Category  : Programming
 Manufacturer   : N/A
 Author  : Jin-Wei Tioh
You can think of these first few articles as a language class. As with any new language, we must first be aware of the syntax of the language. C uses notations that may appear strange to people who have not programmed before. Let us begin by writing a simple program which simply prints a line of text. For reasons of sentimentality, it will be based on the program written by Kernighan and Ritchie.

Code :

/* My first C program */

#include

main()
{

printf("Hello World!\n");
return 0;

}


Output :

Hey, this is simpler than you thought right? Although it doesn't teach very much programming, it gives you a chance to learn the mechanics of typing and compiling code. The goal of this program is not to learn how to print words to the terminal. It's to learn how to type, save and compile a program. This is often a non-trivial procedure, and there are a lot of things that can go wrong even if your source code is correct. This simple program highlights several important features of the C language. The line :

/* My first C program */

begins with /* and ends with */, indicating that the line is a comment. Comments are remarks in a program's source code intended for human readers, acting as internal documentation. They are ignored by the C compiler and do not cause any additional code to be generated, ie. if you put a whole essay (not a good idea, btw) as a comment, the resulting compiled program would be no different. As programs become more and more complicated, well written comments can greatly ease the maintenance/modification of the code. However, be extremely wary of unterminated comments. It is very easy, even for veteran programmers, to forget to end a comment with a */, or to accidentally type it as * /, or /*. In these cases, the compiler will treat parts of the program as comments until if finds a correct comment terminator. This can cause weird program behavior, or result in weird compilation errors, potentially wasting lots of time and effort. Next, the line :

#include

is a preprocessor directive. Lines beginning with # are processed by the preprocessor before the program is compiled. This specific line in our first program tells the preprocessor to include the contents of the standard input/output header file (stdio.h) in the program. While you can get by without including header files, it is best done. Header file store the details and declarations used when compiling functions, in this case, the standard input/output function printf. This helps the compiler to determine if calls to library functions have been written correctly. For now, don't worry about header files. I'll get back to them later.

Next, the line :

main()

is part of every C program. The parentheses after the word main indicates that main is a function. C programs can either can a single function (like our program above), or multiple functions, one of which must be main. When a C program executes, the function main is executed first. If there are multiple functions, functions besides main are never implicitly/automatically executed unless they are somehow called either directly or indirectly from main.

The body of every function must start with a left brace, {. The end of every function must have a corresponding right brace, }. The portion of the program between the braces is also termed a block, an important unit in the C language. The next line :

printf("Hello World!\n");

basically instructs the computer to perform an action. In our case, it is to print the message Hello World! to the screen. This message is a string, which is simply just a sequence of characters. The entire line is what we will call a statement, which must end with a semicolon ( ; ). This particular statement consists of printf and its arguments within the parentheses.

Alright, you now know that the string to be printed is Hello World!\n. But hey, where is the \n? The backslash ( \ ) is an escape character, and it indicates that printf is supposed to do something unordinary. When printf encounters a backslash, it looks ahead at the next character, combining it with the backslash to form an escape sequence. The \n sequence is the newline, which makes the cursor advance to a fresh line. Here's a listing of the escape sequences in C :

Escape Sequences Details
\n Newline escape sequence. Advances the cursor to the beginning of a new line.
\r Carriage return escape sequence. Positions the cursor at the start of the current line.
\t Tab escape sequence. Advance the cursor to the next tab stop.
\a Alert escape sequence. Makes the PC speaker beep.
\" Double quote escape sequence. Prints a double quote when used in a printf statement.
\\ Backslash escape sequence. Prints a backslash when used in a printf statement.

The line :

return 0;

passes the value 0 (zero) back to the operating system. This indicates that the program has executed successfully and passes control back to the operating system. While it is not absolutely necessary, it is generally good programming practice to write this line in all C programs.

Finally, the right brace, }, indicates the end of the main function.

I'm sure you noticed that the two lines between the { and } were indented. Indenting the entire body of each function by one level emphasizes the structure of the program, while also making it much easier to read. Hence, it is another good programming practice.

There are several different ways that you can use to print the Hello World! message. For example,

Code :

/* My first C program - variation 1 */

#include <stdio.h>

main() {

printf("He");
printf("llo ");
printf("World!\n");
return 0;

}

Output :

Another way would be to spread the message across multiple lines :

Code :

/* My first C program - variation 2 */

#include <stdio.h>

main() {

printf("Hello\nWorld!\n");
return 0;

}

Output :

Feel free to experiment with the printf function whilst familiarizing yourself with the basic syntax of C. Next, the scanf function will be introduced, which will enable you obtain input typed by a user on the keyboard.

 

The standard C library provides quite a few functions for doing input. The scanf function is the most commonly used; others include getchar and gets. Using scanf, let's write a program that will read in two integers (whole numbers), calculate the sum of the values and print the result to the display using printf.

Code :

/* Integer addition program */

#include <stdio.h>

main() {

/* Variable declaration */
int number1, number2, sum;

printf("First integer\t: ");
scanf("%d", &number1);
printf("Second integer\t: ");
scanf("%d", &number2);

sum = number1 + number2;
printf("The sum of %d and %d is %d\n", number1, number2, sum);
return 0;

}

Output :

Since this program involves both input and output, it would definitely look messier than the previous example, which involved only output. Not to worry, let's step through the program line by line. Again, we have comments, the preprocessor directive, main(), { and }. The line :

int number1, number2, sum;

is called a declaration. number1, number2 and sum are the names of variables. A variable is a location in memory where data can be stored for use by a program. To use a variable, it must be declared first. This tells the compiler a few important things : what type of variables we want; what names the variables will have. The declaration in our program tells the program to create three variables, number1, number2 and sum, of type int, which means that these variables will hold integer values, ie. whole numbers. In C, variables must be declared immediately after the left brace that begins the body of main. Most compilers will give a compilation error if you attempt to do otherwise. Several variables of the same type can be declared in one declaration, but we could have also written :

int number1;
int number2;
int sum;

Three separate declarations, one for each variable. However, this isn't necessarily a bright idea, since the single declaration is clearer and more concise. Of course, C programs can deal with other data types as well, such as float (real numbers) and char (characters). We'll only use these in subsequent articles.

A variable name in C is any valid identifier. They may include letters, digits and underscores, so long as they do not start with a digit. According to the ANSI C standard, only the first 31 characters of an identifier are required to be recognized by compilers. Thus :

ali_baba_and_the_fourty_thieves_part_one
ali_baba_and_the_fourty_thieves_part_two

would be exactly the same to the compiler, since part_one and part_two are ignored. C identifiers are also case sensitive - uppercase and lowercase letters are different, so number1 and Number1 would be different identifiers.

Diverging a bit from the program step-through, there are also keywords in C. Not all identifiers may be used freely because some have special meanings. If we used these as identifiers, the compiler would misinterpret our program. The keywords are :

Keywords
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

The lines (not in order) :

printf("First integer\t: ");
printf("Second integer\t: ");

print the strings First integer : and Second integer : on the display and make use of the horizontal tab escape sequence to evenly align the colons. They are called prompts because they well, prompt the user to take a specific action. In this case, we want the user to enter integers. However, the action is on the lines (not in order) :

scanf("%d", &number1);
scanf("%d", &number2);

These lines use scanf to obtain a value from the user. Think of scanf as a pizza delivery service. It takes input from the standard input (the keyboard, not the pizza parlor), and "delivers" the data to the computer's memory. But, just like the poor chap in real life who has to know where you live to deliver your pizza, scanf must know where in the computer's memory must it store data. This is where scanf's two arguments come in, ie. the "%d" and &number1. "%d" is the format control string, which specifies the type of data that the user inputs. The %d conversion specifier specifies that the data should be an integer. The % is treated as an escape character in both scanf and printf, just like \. Thus, %d forms an escape sequence (like \n).

The second argument begins with an ampersand (& ) - the address operator - followed by our variable name. &number1 tells scanf the location in memory where the variable number1 resides. So after reading in the data, scanf stores the value for number1 at that location.

When the computer executes the scanf statement, it sits and waits for the user to enter a value. The user responds by typing an integer, then pressing the enter or return key. As described earlier, the computer assigns this number to our variable. The next statement is also known as an assignment statement :

sum = number1 + number2;

It is named as such because it contains the C assignment operator, =. The value of number1 + number2 is calculated and assigned to the variable sum. Correctly, the statement reads "sum gets the value of number1 + number2", and not "sum equals to the value of number1 + number2". In C, the equality operator is ==, but I'll cover this further only in subsequent articles. Finally, the statement :

printf("The sum of %d and %d is %d\n", number1, number2, sum);

uses the printf function to print the literal The sum of and is, with the values of our three variables (number1, number2, sum) in between. In this statement, printf has four arguments, "The sum of %d and %d is %d\n", number1, number2 and sum, separated by commas. The first argument is the format control string, which contains some literal characters as well as three %d conversion specifiers, indicating that three integers will be printed. The second, third and fourth arguments specify the values to be printed. They can either be all variable names, all values or a mix of both. We could have a statement that would print Napster is 2 years old :

printf("Napster is %d years old\n", 2);

Pay attention to two things though. One, the conversion specifier for an integer is the same in both printf and scanf. This is the case for most data types in C. Two, the values/variables are printed out in the sequence that they are specified, ie. if we had written :

printf("The sum of %d and %d is %d\n", sum, number1, number2)

we would have gotten The sum of 11 and 3 is 8.

Calculations can also be performed within printf statements. We can replace these two lines :

sum = number1 + number2;
printf("The sum of %d and %d is %d\n", number1, number2, sum);

with

printf("The sum of %d and %d is %d\n", number1, number2, number1+number2);

in which case, we wouldn't need the third variable sum. You can do things either way, since it is mainly a matter of personal preference.Let's move on to the various arithmetic operators in C.

 

In our earlier program, we used the addition operator ( + ) to add two integers. There are other arithmetic operators in C, which are listed below :

Operation Operator C Expression Algebraic Expression
Addition + a + b a + b
Subtraction - a - b a - b
Multiplication * a * b ab
Division / a / b a / b
Modulus % a % b a mod b

One important note though, integer division yields an integer result. Eg. 2/3 yields 0, instead of 0.666666667. Similarly, 100/3 would yield 33 instead of 33.33333333. This is because we are storing the results of the division into a variable of type int, and only the integer portion of the result (ie. the part before the decimal point) is stored. The rest is simply ignored. This is what is called truncation.

So how would we write a program to read in two numbers (be they integers of real numbers), perform division on them and print the result to the display? Simple, just modify our earlier program.

Code :

/* Number division program */

#include <stdio.h>

main() {

/* Variable declaration */
float number1, number2, sum;

printf("First integer\t: ");
scanf("%f", &number1);
printf("Second integer\t: ");
scanf("%f", &number2);

sum = number1 / number2;
printf("The sum of %.2f and %.2f is %.2f\n", number1, number2, sum);
return 0;

}

Output :

Only a few things have changed. First, in our declaration statement, we changed the type of variables declared to float. floats are allocated much more space than ints, allowing them to store real numbers such as 1.23456789, which is exactly what we need in our program.

Secondly, the %d conversion specifiers have been changed to %f. This specifies that the data should be a real number. We have also changed the addition operator ( + ) to the division operator ( /) to divide number1 by number2 and store the results in sum.

Lastly, the conversion specifier in the last printf statement have been changed to %.2f. Why %.2f and not %f? The .2 indicates the digits of precision that we want. If our program divided 10 by 3 (ie. 10/3), the result would be printed out as 3.33 (2 digit precision) instead of 3.3333333 (arbitrary digits of precision).

Well, that concludes Part 2 of the programming guide. A lot of terminologies and syntax have been introduced, so take your time to familiarize yourself with them. Also, try writing variations or completely different programs to practice. Stay tuned for the next article. Until then, have fun...



Copyright © 2000-2005 BlueSmoke. All rights reserved. Terms, Conditions and Privacy Information.
Site Design by Jin-Wei Tioh

Sitemap