0% found this document useful (0 votes)
11 views34 pages

Trainin Report-1

C is a procedural programming language developed in 1972, primarily for system programming, with features that allow low-level memory access and a simple syntax. The document outlines the structure of C programs, the compilation process, character sets, keywords, identifiers, and various operators used in C programming. It emphasizes the importance of understanding these elements for effective programming in C.

Uploaded by

pullkiiit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views34 pages

Trainin Report-1

C is a procedural programming language developed in 1972, primarily for system programming, with features that allow low-level memory access and a simple syntax. The document outlines the structure of C programs, the compilation process, character sets, keywords, identifiers, and various operators used in C programming. It emphasizes the importance of understanding these elements for effective programming in C.

Uploaded by

pullkiiit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

INTRODUCTION TO C

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.

Use of Character Set in C


Just like we use a set of various words, numbers, statements, etc., in any language for
communication, the C programming language also consists of a set of various different
types of characters. These are known as the characters in C. They include digits,
alphabets, special symbols, etc. The C language provides support for about 256
characters.

These are the set of characters offered by the C language -

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.

Type of Description Characters


Character

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:

Type of Character Examples


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:

Type of Description Characters


Character

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 ?

White Spaces – Blank Spaces, Carriage Return, Tab, New Line


Purpose of Character Set in C

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.

However, a majority of the ANSI-compatible compilers in C accept these ASCII


characters for both the character sets- the source and the execution. Every ASCII
character will correspond to a specific numeric value.

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).

As C is a case sensitive language, all keywords must be written in lowercase. Here is a


list of all keywords allowed in ANSI C.
auto double int struct

break else long switch

case enum register typedef

char extern return union

continue for signed void

do if static while

default goto sizeof volatile

const float short unsigned

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.

We can say that an identifier is a collection of alphanumeric characters that begins


either with an alphabetical character or an underscore, which are used to represent
various programming elements such as variables, functions, arrays, structures, unions,
labels, etc. There are 52 alphabetical characters (uppercase and lowercase), underscore
character, and ten numerical digits (0-9) that represent the identifiers. There is a total of
63 alphanumerical characters that represent the identifiers.

Rules for constructing C identifiers

● The first character of an identifier should be either an alphabet or an underscore,


and then it can be followed by any of the character, digit, or underscore.

● It should not begin with any numerical digit.

● In identifiers, both uppercase and lowercase letters are distinct. Therefore, we


can say that identifiers are case sensitive.

● Commas or blank spaces cannot be specified within an identifier.

● Keywords cannot be represented as an identifier.

● The length of the identifiers should not be more than 31 characters.

● Identifiers should be written in such a way that it is meaningful, short, and easy to
read.

C - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or

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.

Assume variable A holds 10 and variable B holds 20 then −

Operato Description Example


r

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200


/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an B%A=0


integer division.

++ Increment operator increases the integer A++ = 11


value by one.

-- Decrement operator decreases the integer A-- = 9


value by one.

Relational Operators

The following table shows all the relational operators supported by C. Assume

variable A holds 10 and variable B holds 20 then −

Operato Description Example


r
== Checks if the values of two operands are (A == B) is not true.
equal or not. If yes, then the condition
becomes true.

!= Checks if the values of two operands are (A != B) is true.


equal or not. If the values are not equal, then
the condition becomes true.

> 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 less (A < B) is 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

variable A holds 1 and variable B holds 0, then −

Operato Description Example


r

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then the condition
becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition
becomes true.

! Called Logical NOT Operator. It is used to !(A && B) is true.


reverse the logical state of its operand. If a
condition is true, then Logical NOT operator
will make it false.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &,

|, and ^ is as follows −

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

A = 0011 1100

B = 0000 1101

-----------------
A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table lists the bitwise operators supported by C. Assume variable 'A'

holds 60 and variable 'B' holds 13, then -

Operato Description Example


r

& Binary AND Operator copies a bit to the (A & B) = 12, i.e., 0000
result if it exists in both operands. 1100

| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e., 0011


either operand. 1101

^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e., 0011
in one operand but not both. 0001

~ Binary One's Complement Operator is unary (~A ) = ~(60), i.e,.


and has the effect of 'flipping' bits. -0111101
<< Binary Left Shift Operator. The left operands
A << 2 = 240 i.e., 1111
value is moved left by the number of bits
0000
specified by the right operand.

>> Binary Right Shift Operator. The left


A >> 2 = 15 i.e., 0000
operands value is moved right by the number
1111
of bits specified by the right operand.

Assignment Operators

The following table lists the assignment operators supported by the C language-

Operato Description Example


r

= Simple assignment operator. Assigns values C = A + B will assign the


from right side operands to left side operand value of A + B to C

+= Add AND assignment operator. It adds the


C += A is equivalent to C
right operand to the left operand and assign
=C+A
the result to the left operand.
-= Subtract AND assignment operator. It
subtracts the right operand from the left C -= A is equivalent to C
operand and assigns the result to the left =C-A
operand.

*= Multiply AND assignment operator. It


multiplies the right operand with the left C *= A is equivalent to C
operand and assigns the result to the left =C*A
operand.

/= Divide AND assignment operator. It divides


C /= A is equivalent to C
the left operand with the right operand and
=C/A
assigns the result to the left operand.

%= Modulus AND assignment operator. It takes


C %= A is equivalent to C
modulus using two operands and assigns the
=C%A
result to the left operand.

<<= Left shift AND assignment operator. C <<= 2 is same as C = C


<< 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C
>> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C


&2

^= Bitwise exclusive OR and assignment C ^= 2 is same as C = C ^


operator. 2

|= Bitwise inclusive OR and assignment C |= 2 is same as C = C |


operator. 2

Misc Operators ↦ sizeof & ternary

Operato Description Example


r

sizeof() sizeof(a), where a is integer, will


Returns the size of a variable.
return 4.
& &a; returns the actual address of the
Returns the address of a variable.
variable.

* Pointer to a variable. *a;

?: If Condition is true ? then value X :


Conditional Expression.
otherwise value Y

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

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. 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.

Sr.No Loop Type & Description


.

1 while loop

Repeats a statement or group of statements while a given condition is true.

It tests the condition before executing the loop body.

2 for loop
Executes a sequence of statements multiple times and abbreviates the code

that manages the loop variable.

3 do...while loop

It is more like a while statement, except that it tests the condition at the end

of the loop body.

4 nested loops

You can use one or more loops inside any other while, for, or do..while loop.

Loop Control Statements

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.

C supports the following control statements.

Sr.No Control Statement & Description


.

1 break statement

Terminates the loop or switch statement and transfers execution to the

statement immediately following the loop or switch.


2 continue statement

Causes the loop to skip the remainder of its body and immediately retest its

condition prior to reiterating.

3 goto statement

Transfers control to the labeled statement.

The Infinite Loop

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( ; ; ) {

printf("This loop will run forever.\n");

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

for(;;) construct to signify an infinite loop.

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

more useful to think of an array as a collection of variables of the same type.

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

..., numbers[99] to represent individual variables. A specific element in an array is

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

number of elements required by an array as follows −

type arrayName [ arraySize ];


This is called a single-dimensional array. The arraySize must be an integer constant

greater than zero and type can be any valid C data type. For example, to declare a

10-element array called balance of type double, use this statement −

double balance[10];

Here balance is a variable array which is sufficient to hold up to 10 double numbers.

Initializing Arrays

You can initialize an array in C either one by one or using a single statement as follows

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } cannot be larger than the number of

elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is

created. Therefore, if you write −

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create exactly the same array as you did in the previous example. Following is

an example to assign a single element of the array −

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

pictorial representation of the array we discussed above −

Accessing Array Elements

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 −

double salary = balance[9];

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

mentioned concepts viz. declaration, assignment, and accessing arrays-

#include <stdio.h>

int main () {

int n[ 10 ]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n to 0 */

for ( i = 0; i < 10; i++ ) {


n[ i ] = i + 100; /* set element at location i to i + 100 */

/* output each array element's value */

for (j = 0; j < 10; j++ ) {

printf("Element[%d] = %d\n", j, n[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

important concepts related to array should be clear to a C programmer −

Sr.No Concept & Description


.

1 Multi-dimensional arrays

C supports multidimensional arrays. The simplest form of the

multidimensional array is the two-dimensional array.

2 Passing arrays to functions

You can pass to the function a pointer to an array by specifying the array's

name without an index.

3 Return array from a function

C allows a function to return an array.

4 Pointer to an array

You can generate a pointer to the first element of an array by simply

specifying the array name, without any index.


Strings in c

Strings are actually one-dimensional array of characters terminated by a null character

'\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."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as

follows −

char greeting[] = "Hello";

Following is the memory presentation of the above defined string in C/C++ −


Actually, you do not place the null character at the end of a string constant. The C

compiler automatically places the '\0' at the end of the string when it initializes the

array.

You might also like