top of page

Create a Simple C Program


The following program, which will be called number (but which could be named number.exe, program2, or anything else desired), is still trivial, but slightly more interesting in that it introduces some additional concepts and is capable of responding to user input rather than just displaying static output. Its source code is:

#include <stdio.h> main() { int count; puts("Please enter a number: "); scanf("%d", &count); printf("The number is %d \n", count); }

When launched, this program first writes the phrase Please enter a number on the display screen. After the user responds by typing in one or more digits and pressing the ENTER key, the program writes the phrase The number is followed by a single space and then the number that the user typed in.

A Closer Look

The first line is the preprocessor directive. It causes the file named stdio.h to be copied into the source code of number.c. That file resides in glibc (i.e., the GNU project's implementation of the standard C library) and provides the basic input and output facilities for C. The preprocessor directive is followed by a blank line, as is good style (but not absolutely necessary).

The main() function, which begins on the third line, is where the program starts execution (i.e., running). It performs some operation(s) and returns a single value to the program. In this case, main() contains four statements within its curly brackets, each on a separate line. A statement is an instruction to a program; each statement always ends with a semicolon.

The first statement, int count;, declares the variable named count as the data type called int (i.e., integer, which is any whole number). A variable can be thought of as a container for a certain type of value. In C, each variable must be declared before it can be used. This is accomplished by stating its type and then giving its name.

A data type (often called a datatype or just a type) is a category of values on which certain types of operations can be performed. Having data types can seem like a nuisance at first, but they provide several important benefits1 and thus they are explicitly or implicitly supported by virtually all programming languages. The int data type in C is any number up to four bytes in length (i.e., between -2,147,483,648 and +2,147,483,647). There are several other data types in C, including float (floating point numbers) and char (characters).

Any number of variables of the same data type can be declared simultaneously with a single statement, thereby helping make the code more compact. For example, the following statement declares three int variables a, b and c:

int a, b, c;

The puts() function writes the string (i.e., a sequence of characters) enclosed in its parenthesis to standard output, which by default is the display screen, and then automatically starts a new line. In this example, it thus displays the phrase Please enter a number: on the screen and then moves the cursor down to the next line for the user to type in a number.

The scanf() function reads formatted data from standard input, which by default is text typed in at the keyboard. The %d inside of its parenthesis is a placeholder that reads the integers typed in at the keyboard and places their value in the variable count. This function could be changed to read other data types, for example with %f for the float and %c for the char data types.

The ampersand attached to the beginning of the count inside of the parenthesis is referred to as the address operator. It tells the program to store the result of its scanning operation in the variable that follows it, and it is necessary for proper operation of the program.

The printf() function then writes what is enclosed in quotation marks in its parenthesis to standard output. The %d is a placeholder with essentially the opposite function of that in scanf(); that is, it is replaced by the value in count when printf() is executed. Unlike puts(), printf() does not automatically start a new line, and thus it is necessary to add the newline character \n.

Compiling and Running

After copying the source code to a new file with a text editor such as gedit or vi and saving it as number.c, the program is ready to be compiled with the GCC (GNU Compiler Collection) using the following command:

gcc -o number number.c

The -o option tells the compiler to use the string that follows it as the name for the executable (i.e., runnable) file that it produces, which in this example is number. If this option is omitted, the compiler will, by default, give the name a.out to the executable file.

Compilation for number.c should take only about a second or two on a reasonably fast personal computer because the program is so simple. The program can then be run on most systems by typing a dot, a forward slash and the program name and then pressing the ENTER key, i.e.,

./number

If the program will not compile, or it if compiles but will not run correctly (or at all), the problem is most likely that the source code has been copied incorrectly. The most common typing errors include omitting a semicolon at the the end of a statement, forgetting one or more of the curly brackets and forgetting an ampersand. Another typical source of problems is forgetting the .c extension at the end of the name of the source code file(s). If the program still will not run, the problem might be that the above command is being used in a directory other than that in which the program resides or that the dot and slash were omitted or reversed.

Experiments

When the program is running correctly, it can be seen that it will correctly repeat any number of up to nine or ten digits. If more digits are typed in, the program returns the number 2147483647, which is the maximum value for the int data type. If there are any spaces in the number, only those digits occurring before the first space are recognized. If any letters or other non-numeric characters are used, then the program fails and some number apparently unrelated to the characters is returned.

This demonstrates that the coding for even a program designed to perform a very simple task must be made more complex that might initially seem necessary in order to be robust (i.e., operate correctly under unusual circumstances). There are several programming approaches that could be used to make this program more robust, particularly (1) restricting the ability of users to enter characters other than digits and (2) developing code to cope with larger input values.

It can also be interesting and instructive to try making some changes to the program itself, including (1) changing the name assigned to the int variable (e.g., calling it a or banana), (2) modifying the wording within the puts() and printf() functions, (3) changing the data type (such as using char or float) and also making a corresponding change in the %d, (4) omitting the ampersand that is inside of scanf(), (5) replacing the \n with a \t (which is the tab character) and (6) eliminating the double quotes in puts(), scanf() and printf() or replacing them with single quotes.

________ 1These benefits include safety (i.e., preventing attempts to code some operations which are not valid in certain contexts), optimization (e.g., providing useful information to the compiler), abstraction (i.e., allowing programmers to visualize programs in higher level terms), modularity (which facilitates creating consistent interfaces between subsystems) and documentation (which facilitates the reading of complex code that others have written). C has fewer data types than other popular programming languages because of its extreme simplicity.

Recent Posts 
Serach By Tags
No tags yet.
bottom of page