C Programming Language
C Programming Language
There are four fundamental stages, or processes, in the creation of any C program:
• Editing
• Compiling
• Linking
• Executing
You’ll soon get familiar with all these processes, since you’ll be carrying them out so often. First, we’ll
explain what each process is and how it contributes to the development of your C program.
1. Editing
Editing is the process of creating and modifying C source code – It is the name given to the program
instructions you write –.
Some C compilers come with a specific editor program that provides a lot of assistance in managing
your programs. In fact, an editor often provides a complete environment for writing, managing,
developing, and testing your programs. This is sometimes called an integrated development
environment (IDE). You can also use a general-purpose text editor -like block note on Windows OS-
to create your source files, but the editor must store the code as plain text without any extra formatting
data embedded in it; that is why word processor programs such as Microsoft Word aren’t suitable for
producing program code because of the extra formatting information they store along with the text. In
general, if you have a compiler system with an editor included, it will provide a lot of features that make
it easier to write and organize your source programs. There will usually be automatic facilities for
laying out the program text appropriately and color highlighting for important language elements, which
not only makes your code more readable but also provides a clear indicator when you make errors when
keying in such words.
2. Compiling
The compiler converts your source code into machine language and detects and reports errors in the
compilation process. The input to this stage is the file you produce during your editing, which is usually
referred to as a source file.
The compiler can detect a wide range of errors that are due to invalid or unrecognized program
code, as well as structural errors where, for example, part of a program can never be executed. The
output from the compiler is known as object code and it is stored in files called object files, which
usually have names with the extension .obj in the Microsoft Windows environment, or .o in the
Linux/UNIX environment.
The compiler can detect several different kinds of errors during the translation process, and most
of these will prevent the object file from being created. The result of a successful compilation is a file
with the same name as that used for the source file, but with the .o or .obj extension.
3. Linking
The linker combines the object modules generated by the compiler from source code files, adds required
code modules from the standard library supplied as part of C, and welds everything into an executable
whole. The linker also detects and reports errors; for example, if part of your program is missing or a
nonexistent library component is referenced.
In practice, a program of any significant size will consist of several source code files, from
which the compiler generates object files that need to be linked. A large program may be difficult to
write in one working session, and it may be impossible to work with as a single file. By breaking it up
into a number of smaller source files that each provide a coherent part of what the complete program
does, you can make the development of the program a lot easier. The source files can be compiled
separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole
program can usually be developed incrementally. The set of source files that make up the program will
usually be integrated under a project name, which is used to refer to the whole program.
Program libraries support and extend the C language by providing routines to carry out
operations that aren’t part of the language. For example, libraries contain routines that support
operations such as performing input and output, calculating a square root, comparing two character
strings, or obtaining date and time information.
A failure during the linking phase means that once again you have to go back and edit your
source code. Success, on the other hand, will produce an executable file, but this does not necessarily
mean that your program works correctly. In a Microsoft Windows environment, the executable file
will have an .exe extension; in UNIX, there will be no such extension, but the file will be of an
executable type. Many IDEs have a build option, which will compile and link your program in a single
operation.
4. Executing
The execution stage is where you run your program, having completed all the previous processes
successfully. Unfortunately, this stage can also generate a wide variety of error conditions that can
include producing the wrong output, just sitting there and doing nothing, or perhaps crashing your
computer for good measure. In all cases, it’s back to the editing process to check your source code.
Now for the good news: this is also the stage where if your program works, you get to see your
computer doing exactly what you told it to do! In UNIX and Linux you can just enter the name of the
file that has been compiled and linked to execute the program. In most IDEs, you’ll find an appropriate
menu command that allows you to run or execute your compiled program. This Run or Execute option
may have a menu of its own, or you may find it under the Compile menu option. In Windows, you can
run the .exe file for your program as you would any other executable.
The following picture summarize the process of program editing a program in C programming
language.
Note: in this course we use the CodeBlock IDE that is free and open source. You are free to use any
other one like Visual Studio code if you are familiar with.
Figure 1: C program editing steps
} Preprocessing Directives
// instruction to the compiler.
int main()
}
{
// instructions // signature of the main function
return value; // Beginning of the main function
}
// Instructions of the program
the main function
// returning an integer value to the
// end of the main function
1. The preprocesing directives
a preprocesing directives is not strictly part of the executable program; they give instruction to the
compiler to perform some action. They use the symbol # to indicate it is a preprocessing directive,
which is an instruction to your compiler to do something before compiling the source code. The
compiler handles these directives during an initial preprocessing phase before the compilation process
starts. There are quite a few preprocessing directives, and there are usually some at the beginning of the
program source file, but they can be anywhere.
Example:
#include <stdio.h>
#include <stdlib.h>
3. Keywords
In C as in most common programming languages , a keyword is a word with special significance, so
you must not use keywords for any other purpose in your program -for example you cannot use any as
the name of a variable-. For this reason, keywords are also referred to as reserved words. In the
preceding example, int is a keyword and void and return are also keywords. C has several keywords,
and you’ll become familiar with more of them as you learn more of the language.
A variable corresponds to an area of memory which can store a value of the given type. Making
a drawing is an excellent way to think about the variables in a program. Draw each variable as
box with the current value inside the box. Variables, such as num, do not have their memory
cleared or set in any way when they are allocated at run time. Variables start with random
values, and it is up to the program to set them to something sensible before depending on their
values. Names in C are case sensitive so "x" and "X" refer to different variables. Names can
contain digits and underscores (_), but may not begin with a digit. Multiple variables can be
declared after the type by separating them with commas.
Example:
will execute until the variable i takes on the value 10 at which time the expression (i - 10) will become
false (i.e. 0). (we'll see the while() statement a bit later)
C includes the usual binary and unary arithmetic operators. The operators are sensitive to
the type of the operands. So division (/) with two integer arguments will do integer division.
If either argument is a float, it does floating point division. So (6/4) evaluates to 1 while
(6/4.0) evaluates to 1.5 -- the 6 is promoted to 6.0 before the division.
+ Addition
- Subtraction
/ Division
* Multiplication
% Remainder (mod)
2.6 Unary Increment Operators: ++ --
The unary ++ and -- operators increment or decrement the value in a variable. There are
"pre" and "post" variants for both operators which do slightly different things (explained
below)
var++ increment "post" variant
++var increment "pre" variant
var-- decrement "post" variant
--var decrement "pre" variant
Example
int i = 42;
i++; // increment on i
// i is now 43
i--; // decrement on i
// i is now 42
2.7 Relational Operators
These operate on integer or floating point values and return a 0 or 1 boolean value.
!= Not Equal
> Greater Than
< Less Than
>= Greater or Equal
<= Less or Equal
To see if x equals three, write something like:
if (x == 3) ...
2.8 Logical Operators
The value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the
truth or falsity of the expression can be deduced. (Such operators are called "short circuiting") In ANSI
C, these are furthermore guaranteed to use 1 to represent true, and not just some random non-zero bit
pattern. However, there are many C programs out there which use values other than 1 for true (non-zero
pointers for example), so when programming, do not assume that a true boolean is necessarily 1 exactly.
! Boolean not (unary)
&& Boolean and
|| Boolean or