C Basic Concept
C Basic Concept
The UNIX operating system, the C compiler, and essentially all UNIX applications programs have
been written in C. The C has now become a widely used professional language for various
reasons.
Easy to learn
Structured language
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computer platforms.
C was initially used for system development work, in particular the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as code written in assembly language. Some examples of the use of C
might be:
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
Article IV. About C compilation Process.
Normally the C’s program building process involves four stages and utilizes different
‘tools’ such as a preprocessor, compiler, assembler, and linker.
At the end there should be a single executable file. Below are the stages that happen in
order regardless of the operating system/compiler and graphically illustrated in Figure
w.1.
1. Preprocessing is the first pass of any C compilation. It processes include-files,
conditional compilation instructions and macros.
2. Compilation is the second pass. It takes the output of the preprocessor, and the
source code, and generates assembler source code.
3. Assembly is the third stage of compilation. It takes the assembly source code
and produces an assembly listing with offsets. The assembler output is stored in
an object file.
4. Linking is the final stage of compilation. It takes one or more object files or
libraries as input and combines them to produce a single (usually executable)
file. In doing so, it resolves references to external symbols, assigns final
addresses to procedures/functions and variables, and revises code and data to
reflect new addresses (a process called relocation).
Bear in mind that if you use the IDE type compilers, these processes quite transparent.
Now we are going to examine more details about the process that happen before and
after the linking stage. For any given input file, the file name suffix (file extension)
determines what kind of compilation is done and the example for GCC is listed in Table
w.1.
In UNIX/Linux, the executable or binary file doesn’t have extension whereas in Windows
the executable for example may have .exe, .com and .dll.
The following Figure shows the steps involved in the process of building the C program starting
from the compilation until the loading of the executable image into the memory for
program running.
OBJECT FILES and EXECUTABLE
After the source code has been assembled, it will produce an Object files (e.g.
.o, .obj) and then linked, producing an executable files.
An object and executable come in several formats such as ELF (Executable and
Linking Format) and COFF (Common Object-File Format). For example, ELF is
used on Linux systems, while COFF is used on Windows systems.
Other object file formats are listed in the following Table.
When we examine the content of these object files there are areas called
sections. Sections can hold executable code, data, dynamic linking information,
debugging data, symbol tables, relocation information, comments, string tables,
and notes.
Some sections are loaded into the process image and some provide information
needed in the building of a process image while still others are used only in
linking object files.
There are several sections that are common to all executable formats (may be
named differently, depending on the compiler/linker) as listed below:
Article V. Basic Syntax of C.
Printf
(
"Hello, World! \n"
)
;
Semicolons ;
In C program, the semicolon is a statement terminator. That is, each individual statement
must be ended with a semicolon. It indicates the end of one logical entity.
For example, following are two different statements:
Comments
Comments are like helping text in your C program and they are ignored by the compiler.
They start with /* and terminates with the characters */ as shown below:
/* my first program in C */
Identifiers
A C identifier is a name used to identify a variable, function, or any other user-defined item.
An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more
letters, underscores, and digits (0 to 9). C does not allow punctuation characters such as @,
$, and % within identifiers. C is a case sensitive programming language. Thus, Manpower
and manpower are two different identifiers in C.
Keywords
The following list shows the reserved words in C. These reserved words may not be used as constant
or variable or any other identifier names.
Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and a
C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters and comments.
Whitespace separates one part of a statement from another and enables the compiler to
identify where one element in a statement, such as int, ends and the next element begins.
Therefore, in the following statement:
int age;
There must be at least one whitespace character (usually a space) between int and age for the
compiler to be able to distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // get the total fruit
No whitespace characters are necessary between fruit and =, or between = and apples, although you
are free to include some if you wish for readability purpose.
In the C programming language, data types refer to an extensive system used for declaring
variables or functions of different types. The type of a variable determines how much space it
occupies in storage and how the bit pattern stored is interpreted.
Integer Types:
Floating Types:
Void Types:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides the following types of
operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Ternary operators.
Arithmetic Operators:
Relational Operators:
Logical Operators:
Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^, ~ .
Ternary Operators:
The conditional operators ? and : are sometimes called ternary operators since they take three
arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the
value returned will be expression 2, otherwise the value returned will be expression 3”. Let us
understand this with the help of a few examples:
int x, y ;
scanf ( "%d", &x ) ;
y=(x>5?3:4);
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the
programming languages:
C programming language assumes any non-zero and non-null values as true, and if it is either
zero or null, then it is assumed as false value.
C programming language provides following types of decision making statements.
Article IX. Loops Structure in C.
There may be a situation, when you need to execute a block of code several number of times.
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages:
C programming language provides the following types of loop to handle looping requirements.