C Programming Examples, Basics and OutlinesBy Youssef Edward
January 12, 2019
Here is provided a description of some basic features of the C Programming language. We will base our discussion on the programs in the example below.
A Programming example contains a C program and a sample execution of that program. The program asks for a nickname and the current year and then displays a personalized message to the program user.
The program line starting with char identifies the names of three memory cells (letter_1, letter_2, letter_3) that will be used to store each letter of the nickname. The program instruction
scanf(’%c%c%c, &letter_1, &letter_2, &letter_3);
copies the three letters Bob (typed by the program user) into the three memory cells, one letter per cell. After prompting the user to enter a year and storing this value in the memory cell year, the program displays the welcoming message shown as the last line of the sample execution. The final program line
is a statement that completes the execution of function main by returning a value that indicates that the function run normally.
One of the nicest things about C is that it lets us write programs that resemble English. At this point, you probably can read and understand the two sample programs, even though you do not know how to write your own pro grams. In the following sections, you’ll learn more details about the two C pro grams we’ve looked at so far.
* Displays the user’s nickname and the current year
* in a welcoming message.
=include <stdio.h> 1* printf, scanf definitions */
char letter1, letter 2, letter 3; /* three letters */
int year; /* current year */
printf(’Enter a 3-letter nickname and press return> “); scanf(’%c%c%c’, &letter_i, &letter_2, &letter3);
printf(”Enter the current year and press return> “);
scanf( “%d’, &year)
printf(”Welcome, %c%c%c. %d is a great year to study C!\n’, letter_i, letter_2, letter 3, year);
Outline of a Simple C Program
Preprocessor directives main function prototype
C Program Outline
All preprocessor directives use commands that have a number-sign symbol (#) as the first nonblank character on the line. These directives give instructions to the C preprocessor, whose job it is to modify the text of a C program before it is compiled. The two most commonly used directives are #include and #define.
The C language explicitly defines only a small number of operations:
Many actions that are necessary in a computer program are not defined directly by C. Instead, every C implementation contains collections of useful functions and symbols called libraries. The ANSI standard for C requires that certain standard libraries be provided in every ANSI C implementation. A C system may expand the number of operations available by supplying additional libraries; an individual programmer can also create libraries of functions.
It is the #include directive that gives a program access to one of these libraries. This directive causes the preprocessor to insert definitions from a library file into a program before compilation. The sample programs we have seen must include definitions from the standard I/O library <stdio . h> so the compiler will know the meanings of printf and scanf.
The other preprocessor directive was #define, which was used to create the constant macrot P1 with the meaning 3. 14 15 9. This directive instructs the preprocessor to replace the name defined by its meaning every time the name appears in your program. For example, the pre processor would respond to by replacing the symbol P1 by the value 3. 14 throughout the text of the program. As a result, the line
area = P1 * radius * radius;
area = 3.14159 * radius * radius;
By the time it was sent to the C compiler. Only data values that never change or change very rarely should be given names using #define, because an executing C program cannot change the value of a name defined as a constant macro.
MAIN FUNCTION PROTOTYPE
One valid prototype (header) for the main function of a simple C program is
This prototype marks the function where program execution begins.
Main Function Body
The remaining parts of the simple C program outline form the body of the main function. The curly braces mark the beginning and end of this function body. The declarations identify the memory cells used by the function, and the statements list instructions to manipulate data according to the program’s purpose.
Now that we have an idea of the overall structure of a simple C program, let’s look in detail at the components of the body of the main function.
Reserved Words and Identifiers
Each line of the programs satisfies the syntax rules for the C language. Each line contains a number of different elements, such as reserved words, identifiers from standard libraries, special symbols, and names for memory cells. Let’s look at the first two elements. All the reserved words appear in lowercase; they have special meaning in C and cannot be used for other purposes. A complete list of ANSI C reserved words is found in Appendix G. The reserved words are
mt void, double, char, return
Two identifiers in these programs are defined by the standard libraries that we access by using the #include directive. Although these names can be used by the programmer for other purposes, we don’t recommend this practice. The standard identifiers appearing are
What is the difference between reserved words and standard identifiers? Although you must not choose a reserved word as the name of a memory cell used by your program, you may choose a standard identifier. However, once you use a standard identifier to name a memory cell, the C compiler no longer associates the standard library definition with that identifier. For example, you could decide to use printf as the name of a memory cell, but then you would not be able to use printf to display program output, its usual purpose. Because standard identifiers already serve a valuable purpose, we don’t recom mend using them to name memory cells. (Other identifiers appearing in the programs in
The preprocessor directives and the main function prototype introduced in this section are summarized in the following displays. Each display describes the syntax of the statement and provides examples and an interpretation of the statement.
#include Directive for defining identifiers from Standard Libraries
INTERPRETATION: #include directives tell the preprocessor where to find the meanings of standard identifiers used in the program. These meanings are collected in files called standard header tiles. The header file studio.h contains standard input and output functions such as scanf and printf .