Trainin Report-1
Trainin Report-1
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year
1972. It was mainly developed as a system programming language to write an operating system.
The main features of the C language include low-level memory access, a simple set of keywords,
and a clean style, these features make C language suitable for system programming like an
operating system or compiler development.
Many later languages have borrowed syntax/features directly or indirectly from the C language.
Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on the C
language. C++ is nearly a superset of C language (Few programs may compile in C, but not in
C++)
STRUCTURE OF C
when we begin with a new programming language, we are not aware about the basic structure of
a program. The sections of a program usually get shuffled and the chance of omission of error
rises. The structure of a language gives us a basic idea of the order of the sections in a program.
We get to know when and where to use a particular statement, variable, function, curly braces,
parentheses, etc. It also increases our interest in that programming language.Thus, the structure
helps us analyze the format to write a program for the least errors. It gives better clarity and the
concept
COMPILATION OF C
The compilation is a process of converting the source code into object code. It is done with the
help of the compiler. The compiler checks the source code for the syntactical or structural errors,
and if the source code is error-free, then it generates the object code.The compilation process can
be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.
Preprocessor
The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor
expands this code. After expanding the code, the expanded code is passed to the compiler.
Compiler
The code which is expanded by the preprocessor is passed to the compiler. The compiler
converts this code into assembly code. Or we can say that the C compiler converts the
pre-processed code into assembly code.
Assembler
The assembly code is converted into object code by using an assembler. The name of the object
file generated by the assembler is the same as the source file. The extension of the object file in
DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the
name of the object file would be 'hello.obj'.
Linker
The main working of the linker is to combine the object code of library files with the object code
of our program. Sometimes the situation arises when our program refers to the functions defined
in other files; then linker plays a very important role in this. It links the object code of these files
to our program. Therefore, we conclude that the job of the linker is to link the object code of our
program with the object code of the library files and other files.
character set
In the C programming language, the character set refers to a set of all the valid
characters that we can use in the source program for forming words, expressions, and
numbers.
The source character set contains all the characters that we want to use for the source
program text. On the other hand, the execution character set consists of the set of those
characters that we might use during the execution of any program. Thus, it is not a
prerequisite that the execution character set and the source character set will be the
same, or they will match altogether.
Types of Characters in C
The C programming language provides support for the following types of characters. In
other words, these are the valid characters that we can use in the C language:
● Digits
● Alphabets
● Main Characters
All of these serve a different set of purposes, and we use them in different contexts in
the C language -
Digits
The C programming language provides the support for all the digits that help in
constructing/ supporting the numeric values or expressions in a program. These range
from 0 to 9, and also help in defining an identifier. Thus, the C language supports a total
of 10 digits for constructing the numeric values or expressions in any program.
Type of Character Description Characters
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Alphabets
The C programming language provides support for all the alphabets that we use in the
English language. Thus, in simpler words, a C program would easily support a total of 52
different characters- 26 uppercase and 26 lowercase.
Lowercase a to z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,
Alphabets w, x, y, z
Uppercase A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T,
Alphabets U, V, W, X, Y, Z
Special Characters
We use some special characters in the C language for some special purposes, such as
logical operations, mathematical operations, checking of conditions, backspaces, white
spaces, etc.
We can also use these characters for defining the identifiers in a much better way. For
instance, we use underscores for constructing a longer name for a variable, etc.
The C programming language provides support for the following types of special
characters:
White Spaces
The white spaces in the C programming language contain the following:
● Blank Spaces
● Carriage Return
● Tab
● New Line
Summary of Characters in C
Here is a table that represents all the types of character sets that we can use in the C
language:
Lowercase a to z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,
Alphabets w, x, y, z
Uppercase A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T,
Alphabets U, V, W, X, Y, Z
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special – `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.
Characters ?
The character sets help in defining the valid characters that we can use in the source
program or can interpret during the running of the program. For the source text, we have
the source character set, while we have the execution character set that we use during
the execution of any program.
But we have various types of character sets. For instance, one of the character sets
follows the basis of the ASCII character definitions, while the other set consists of
various kanji characters .
The type of character set we use will have no impact on the compiler- but we must know
that every character has different, unique values. The C language treats every character
with different integer values. Let us know a bit more about the ASCII characters.
ASCII Values
All the character sets used in the C language have their equivalent ASCII value. The
ASCII value stands for American Standard Code for Information Interchange value. It
consists of less than 256 characters, and we can represent these in 8 bits or even less.
But we use a special type for accommodating and representing the larger sets of
characters. These are called the wide-character type or wchat_t.
Keywords in c
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler. Keywords are part of the syntax and they cannot be used as
an identifier. For example:
int money;
Here, int is a keyword that indicates money is a variable of type int (integer).
do if static while
All these keywords, their syntax, and application will be discussed in their respective
topics.
Identifiers in c
C identifiers represent the name in the C program, for example, variables, functions,
arrays, structures, unions, labels, etc. An identifier can be composed of letters such as
uppercase, lowercase letters, underscore, digits, but the starting letter should be either
an alphabet or an underscore. If the identifier is not used in the external linkage, then it
is called as an internal identifier. If the identifier is used in the external linkage, then it is
called as an external identifier.
● Identifiers should be written in such a way that it is meaningful, short, and easy to
read.
C - Operators
logical functions. C language is rich in built-in operators and provides the following
types of operators −
● Arithmetic Operators
● Relational Operators
● Logical Operators
● Bitwise Operators
● Assignment Operators
● Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language.
Relational Operators
The following table shows all the relational operators supported by C. Assume
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand. If yes, then
the condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand. If
yes, then the condition becomes true.
<= Checks if the value of left operand is less (A <= B) is true.
than or equal to the value of right operand. If
yes, then the condition becomes true.
Logical Operators
Following table shows all the logical operators supported by C language. Assume
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &,
|, and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A'
& Binary AND Operator copies a bit to the (A & B) = 12, i.e., 0000
result if it exists in both operands. 1100
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e., 0011
in one operand but not both. 0001
Assignment Operators
The following table lists the assignment operators supported by the C language-
Loops
You may encounter situations, when a block of code needs to be executed several
number of times. In general, statements are executed sequentially: The first statement
Programming languages provide various control structures that allow for more
times. Given below is the general form of a loop statement in most of the programming
languages −
C programming language provides the following types of loops to handle looping
requirements.
1 while loop
2 for loop
Executes a sequence of statements multiple times and abbreviates the code
3 do...while loop
It is more like a while statement, except that it tests the condition at the end
4 nested loops
You can use one or more loops inside any other while, for, or do..while loop.
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
1 break statement
Causes the loop to skip the remainder of its body and immediately retest its
3 goto statement
A loop becomes an infinite loop if a condition never becomes false. The for loop is
traditionally used for this purpose. Since none of the three expressions that form the
'for' loop are required, you can make an endless loop by leaving the conditional
expression empty.
#include <stdio.h>
int main () {
for( ; ; ) {
return 0;
}
When the conditional expression is absent, it is assumed to be true. You may have an
initialization and increment expression, but C programmers more commonly use the
Arrays in c
Arrays a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often
Instead of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1], and
accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the
greater than zero and type can be any valid C data type. For example, to declare a
double balance[10];
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows
The number of values between braces { } cannot be larger than the number of
If you omit the size of the array, an array just big enough to hold the initialization is
You will create exactly the same array as you did in the previous example. Following is
balance[4] = 50.0;
The above statement assigns the 5th element in the array with a value of 50.0. All
arrays have 0 as the index of their first element which is also called the base index and
the last index of an array will be total size of the array minus 1. Shown below is the
An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example −
The above statement will take the 10th element from the array and assign the value to
salary variable. The following example Shows how to use all the three above
#include <stdio.h>
int main () {
int i,j;
return 0;
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Arrays in Detail
Arrays are important to C and should need a lot more attention. The following
1 Multi-dimensional arrays
You can pass to the function a pointer to an array by specifying the array's
4 Pointer to an array
'\0'. Thus a null-terminated string contains the characters that comprise the string
followed by a null.
The following declaration and initialization create a string consisting of the word
"Hello". To hold the null character at the end of the array, the size of the character array
containing the string is one more than the number of characters in the word "Hello."
If you follow the rule of array initialization then you can write the above statement as
follows −
compiler automatically places the '\0' at the end of the string when it initializes the
array.