Professional Documents
Culture Documents
UNIT-1
History of c language:
C was developed by Dennis Ritchie at Bell Labs between 1969 and 1973.
It evolved from an earlier language called B, which was developed by Ken Thompson in the late
1960s.
C language was developed by Dennis Ritchie in 1970s at AT& T Bell laboratories, Murray Hill in
Machine language is that low level language which the central processing unit executes.
Machine language(or low level language) is known as the first generation language.
Machine languages are hexadecimal numbers and these are very difficult to write and remember.
Then after Machine language scientists came up with an idea of replacing machine code
instructions with the English words and thus assembly language came.
Assembly language is called second generation language.
Its own set of instructions for handling various operations such as displaying information on
screen, getting input from keyboard and performing various other jobs.
1|Page LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
Assembly language was a boon but writing a large program with lots of procedures was still
difficult.
The assembly language is designed to represent various instructions only in symbolic code and is
more understandable than the low level language or machine language.
So Assembly language was a little bit difficult to understand, Because in the assembly language
there were a set of instructions for handling various operations.
They wanted to create one of the language which uses mathematical expressions ,simple English
like English statements, and operators.
This is the time of third generation programming language as we mentioned above C, BASIC,
Pascal, Fortran etc These are higher level languages and independent of processor architectures.
All above mention C,BASIC, Pascal, Fortran come with compiler which convert the source code
into an intermediate code known as object code.
Object codes are lower level code but not machine code.
Then we also know computer understand machine code to convert the object code into machine
code assembler is used.
C is middle level language.it has the simplicity of a high level language as well as the power of
low level language.
The C makes it suitable for writing for both System programs and application programs.
Most of the application such as mathematical, scientific, business and system software
applications developed by C Language.
The C language is extensible since it allows the users to add their own library functions to the
library
1. Portable
The high Level languages are designing to keep in mind the features of portability.
Portable languages are Machine independent.
These languages are not tied with any hardware.
So we can say it is hardware independent or platform independent
2. Extensible
In c language features The C language is Extensible.
Because it can adopt easily new features we can also add new libraries in c Language
3. Pointer
C is the Very powerful language and Real Power of C lies in pointers using pointers
Accessing dynamically allocated memory and using pointer and we can directly interact with
memory using pointers implementing Data structure like linked lists,trees and graphs
4. Faster
C is faster then other languages because of C has less in build functions its more closed with hardware.
5.Memory Management
The process of allocation memory at the time of execution is called Dynamic memory
allocation<
6.Rich Library
7.Structure Language
1.Top-down Design
2.Button up design
structure of a C program
Sometimes, 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.
Thus, the structure helps us analyze the format to write a program for the least errors. It gives better clarity
and the concept of a program.
Here, we will discuss the sections of a C program, some practical examples with explanations, steps
to compile and execute a C program.
Let's start.
1. Documentation section.
2. Preprocessor section or Link Section.
3. Definition section.
4. Global declaration.
5. Main function.
6. User defined functions.
Documentation section
It includes the statement specified at the beginning of a program, such as a program's name, date,
description, and title.
It is represented as:
Or
/*
*/
Anything written inside will be considered a part of the documentation section and
will not interfere with the specified code.
It informs the system to link the header files to the system libraries.
It is given by:
#include<stdio.h>
#include<conio.h>
The #include statement includes the specific file as a part of a function at the time of the
compilation.
Thus, the contents of the included file are compiled along with the function being compiled.
The #include<stdio.h> consists of the contents of the standard input output files, which contains
the definition of stdin, stdout, and stderr.
Whenever the definitions stdin, stdout, and stderr are used in a function, the statement
#include<stdio.h> need to be used.
There are various header files available for different purposes. For example, # include <math.h>. It is
used for mathematic functions in a program.etc
Define section:
The define section comprises of different constants declared using the define keyword.
It is given by:
#define a = 2
Global declaration
The global section comprises of all the global declarations in the program.
It is given by:
char = 1 byte
float = 4 bytes
int = 2 bytes
We can also declare user defined functions in the global variable section.
Main function
main()
int main()
Or
void main()
Local declarations
The variable that is declared inside a given function or block refers to as local declarations.
7|Page LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
main()
{
Int i = 2;
i++;
}
Statements
The statements refers to if, else, while, do, for, etc. used in a program within the main function.
Expressions
An expression is a type of formula where operands are linked with each other by the use of operators.
It is given by:
a - b;
a +b;
The user defined functions specified the functions specified as per the requirements of the user.
For example, color(), sum(), division(), etc.
The program (basic or advance) follows the same sections as listed above.
return;
or
return expression ;
For example,
return 0;
Examples
It is given by:
Output
/* Sum of the two It is the comment section. Any statement described in it is not considered
numbers */ as a code. It is a part of the description section in a code.
The comment line is optional. It can be in a separate line or part of an
executable line.
int main() main() is the first function to be executed in every program. We have used
int with the main() in order to return an integer value.
{………………..} The curly braces mark the beginning and end of a function. It is
mandatory in all the functions.
printf() The printf() prints text on the screen. It is a function for displaying
constant or variables data. Here, 'Enter two numbers to be added' is the
parameter passed to it.
scanf() It reads data from the standard input stream and writes the result into the
specified arguments.
sum = a + b The addition of the specified two numbers will be passed to the sum
parameter in the output.
return 0 A program can also run without a return 0 function. It simply states that a
program is free from error and can be successfully exited.
Letters : A to Z and a to z
Digits : 0 to 9
Special characters : *, @, # ......etc.
10 | P a g e LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
White space characters: Enter key(\n), tab space key(\t), space key ( −), back
space key (‘\b’)....etc.
Keywords
Keywords are the words whose meaning has already been explained to the C compiler.
The keywords cannot be used as Identifiers because if we do so we are trying to assign a new
meaning to the keyword, which is not allowed by the computer.
There are only 32 keywords available in C.
do if static while
Variables
A variable is an identifier or a name which is used to refer a value.
A variable is a name of the memory location where data is stored.
Each variable in C has a specific type, which determines the size of the variable's memory; the
range of values that can be stored within that memory; and the set of operations that can be applied
to the variable.
Variable Declaration in C
Syntax
datatype variable_name;
Example:
int id;
float bassal;
Syntax
Example:
int id = 5;
float bassal = 18000;
Constant in c
Numeric Constant in C
The Numeric constant consist of numerical digits.
They may be without decimal point or may have decimal point(.).
These are number of rules for defining numeric constants-
1. Numeric constant should have at least one digit.
2. There should be no comma or space is allow within the numeric constant for defining.
3. Numeric constant may be positive or negative but default sign of numeric constant is always be
positive.
Integers Constant in C
There are three types of integer constant based on numbers systems such (decimal, octal,
hexadecimal) so here all are integer constants or integer types constants but based on number
system
Decimal constants- 0,1,2,3,4,5,6,7,8,9 (base 10).
Octal constants- 0,1,2,3,4,5,6,7 (base 8)
Hexadecimal constants 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,a,b,c,d,e,f (base 16)
Invalid Remarks
Here are some invalid decimal integers constants are in the below example-
let us see some valid octal integers constants and take example, first digit must be 0.
let us see and take example of some valid Hexadecimal integers constant, first two characters should be
0X or 0x. some examples are
Character Constant in C
The character constant is the single character that is enclosed with in the single quotes. here are
some valid character constants are-
Note: Every character has unique integer value. so every associated value or integer value is
machine code such as ASCII(American standard code for information interchange). its depend on
machine if machine using ASCII then A character represent by 65
; ASCI value(59)
String Constant in C
A string Constant is a group of characters enclosed within double quotes(“”). At the end of string
the complier automatically placed null character (‘\0’). Here are some examples of String constants-
“Saraswat”
“Nitish”
“897”
“5”
”“
“,”
16 | P a g e LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
Symbolic Constant in C
#define PI 3.141567
#define name “Nitish”
#define CH ‘c’
Data types in c
Data types in c programming, A data types defines a domain of allowed values and the what type
of values will be store and what types of operations performs.
Storage represents every different data types with different memory blocks.
In C languages there some data types predefined, Derived, Enumeration, void
Integer type
Integers are used to store whole numbers.
Size and range of Integer type on 16-bit machine:
Character type
So int is use to stored integer type value, char is used to store any single character, float is use for
storing single precision floating point number and the last is double are use for
storing double precision floating point number. These basic data types also divides into two types–
1. Size qualifiers short, long
2. Sign qualifiers signed, unsigned
Talk about the qualifiers signed and unsigned are apply to char and integers types.
Data types in c programming, when the qualifier signed is use store number may positive or
negative, and when the qualifier is unsigned is used the number will always positive.
If there is explicitly sign qualifier is not mention in integers, then the default signed qualifier is
assume and for the char if sign qualifier is not mention explicitly.
Talk about range the range of values of the signed data types are less than that of unsigned type.
The qualifiers long and short we can be apply to int type to get types short int and long int.
And the qualifier long we can applied to double to get type of long double.
And the sizes and range of different different data types on a 16-bit machine is given in this table.
The size and range may be vary on machine by machine
Scanf in C
The Input Data can entered into the memory from a standard input device(Keyboard).
There is scanf() library function are use for entering input data in c.
This Scanf() can take All data types of values like(Numerical, character, string) as input. The
scanf() function read the data input data from console.
The scanf() function can written as–
scanf("control string",address1,address2,......);
Scanf function in c programming or reading input data in c, in This scanf() function should have at
least two parameters.
The first parameter is a control string which is know conversion specification character.
It should be in double quotes.
The conversion specification characters may one or more than one.
It depend on user want to numbers of variable inputs and the others parameters are addressed of
variables.
In the scanf() function at least one address should inside.
The address of the variable is represent by preceding the variable name and follow by an
ampersand (&) sign.
This sign is know as address operator and its gives the starting address of the variable in memory.
Example–
int main()
int age=18;
scanf("%d",&age);
In this example. in the first parameter control string contains only one conversion specification
character %d, which indicates that only one integer value should be enter as input
int main()
char grade='A';
scanf("%c",&grade);
In this example in the first parameter contain conversion specification character %c.
which means the single character should be enter as input.
This value stored in the grade variable. for example-
In the
float salary=300.60; above
scanf("%f",&salary);
example first parameter control string contains the conversion specification character %f, which means that
floating point any number input as user input this entered value will be stored in salary variable
char str[30]
scanf("%s",&str);
In the above example first parameter conversion specifier %s indicates the String should be input. and its
stored in the str variable which is string type. we can use ampersand (&) or without ampersand (&) before
the variable More then one value we can stored using scanf() function there is some examples —
#include<stdio.h>
void main()
int salary,age;
scanf("%d%d",&salary,&age);
void main()
scanf("%c%d",&grade,&salary);
void main()
float average=89.67;
scanf("%c%d%f",&grade,&salary,&average);
#include<stdio.h>
int main()
int fnum;
scanf("%d ",&fnum);
return (0);
Printf in c
Output data can be written from user input or from memory to the console and output
device(monitor) using printf() library function.
With printf() function all types of values such as(numerical,character or string)can be written as
output the printf() function can be written as–
printf("control strings",variable1,variable2,variable3......);
In C printf() function | writing output in c, in this printf() function, the first control string contains
conversion specification characters and text.
The first control string should be enclosed with in double quotes.
In the second part the variables not followed by an ampersand(&) sign.
If the control string does not contain any conversion specification, the variables name not
specified. Here is some Examples–
int main()
{
printf(″C language has powerful tools″);
}
In this example. In the printf() function the control string contains only text and no conversion
specification characters, so the output will be only text that written in the printf()
int main()
int marks;
scanf("%d",&marks);
In this above example the printf() function does not contain any conversion specification character.
its contains only text to print so its used to display message. And tell to user to enter marks –
float salary=300.60;
printf("%f",salary);
In the above example first parameter control string contains the conversion specification character
%f, which means that floating point value will be display. The variable salary has floating value
to display as output
char str='A';
printf("%c",str);
In the above example first parameter conversion specifier character %c. That means a single character will
be display and variable str has that character value
#include<stdio.h>
void main()
char name[10]="Nitish";
printf("%s",name);
Output
Nitish
In the above example first parameter conversion specifier character %s indicates that string will be display
and variable name is the type of character array that stored the string which will be display.
#include<stdio.h>
void main()
char name[10]="Nitish";
float average=90.7;
int roll;
printf("%s %f %d",name,average,roll);
Output
Identifiers in c
C identifiers , All the words we will use in the C language will be either keywords or identifiers.
The keywords are predefined words that cannot changed by the users and C Identifiers are user
defined words and are use to give names to entities like variables, functions, array, structures,
union etc. Rules for Naming identifiers giving—-
C identifiers represent the name in the C language, for example, functions, arrays, structures
variables, labels unions, etc.
It is a composed of letters such as lowercase, uppercase letters, digits and underscore, but the
starting letter should be either an alphabet or an underscore not the any digit.
If identifiers not used in the external linkage, then it is known as an internal identifier.
And whenever the identifier is used in the external linkage, then it is known as an external
identifier.
(1) The name should consist of only uppercase. lowercase, digits and underscore(_).
(2) The name should not be a keyword(reserve word)
(3) The first character should be an alphabet or underscore.
(4) Commas or blank spaces cannot be specified within an identifier.
(5) It should not start with any numerical digit.
(6) The length of the identifier should not be more than 31 characters.
(7) The C is a case sensitive. so the lowercase and uppercase considering different so for example program
and PROGRAM are different.
Identifiers Keywords
Identifiers are the user defined words Keywords are the pre-defined words. That
have a special meaning
It can contain underscore, number, It does not contain the underscore character
characters and numbers
Operators:
For perform any task the variables, constants can joined by various operators to form
an expression.
There are operands.
Operands is a data item on which an operators acts.
There are some operators required two operands and some operators required only
one operand.
The c language support larger number of operators that are mentioned below with
several different categories operators in c programming
The Arithmetic operators are used for numerical calculations such as addition, subtraction,
multiplication, division etc
The unary operators are required only one operand. See in the below example
The binary operators require two operands So there are five binary operators in c. show in the below
1. + Addition
2. – Subtraction
3. * Multiplication
4. / Division
5. % Modular
% Is modular operator which gives the remainder of the integer. Modular operator can not apply with floating
point operands. Here is one thing note that unary minus and plus operators are different from the addition and
subtraction operators. When both operands are integers, then the resulting value always will be integer let us
take example there are two operands x is 19 and y is 8, then the result of the following operations are-
Expression Result
x+y 27
x-y 11
x*y 152
In the above example in the division operation the decimal part will be truncated then the result will be only
integer part of quotient.after modular operation in the result the remainder part of integer division. In the
modular operation the second operand must be non zero for modular and division operations.
void main()
{
int x=19,y=8;
printf(“subtraction is=%d”,x-y);
printf(“\n addition is =%d”,x+y);
printf(“\n multiplication is =%d”,x*y);
printf(“\n division is =%d”,x/y);
printf(“\n remainder is =”,x%y);
}
Assignment Operators In C:
The values stored in any variable or in any operand using assignment operator.
The operand on the left hand side should be variable where value will be assigned,
while on the right hand side the operand can any variable, constant or expression.
So the value of right hand side will assign to the left hand side operand.
Here in the below some examples of assignment expressions –
a=b=c=50
Here in the above example all the three variables a,b,c will be assign 50. and the value of
the complete expression will be 50
The (++) increment and (–) decrement both operators are unary operators because of
both operators operate on a single operand.
The increment operator increments the value of any variable by 1 and decrement
operator decrements the any variable value by 1.
Here in the below there are some examples –
is Equivalent to a=a+1
++a
–a is Equivalent to a=a-1
These opearators can not be used with constant and expression here in the below are
prefix increment/decrement
In the prefix increment or decrement operator is written before the operand(e.g. ++x
or –x) here first the value of variable is increment/decrement then the new value is
used in any expression or operation.
b=b+1;
a=b;
The statement a=- -b; means first decrement the value of b by one then assign the value to a This statement is
equivalent to these two statements
b=b-1;
a=b;
{
int a=7;
printf(“a=%d\t”,a);
printf(“a=%d\t”,++a); /*prefix increment*/
printf(“a=%d\t”,a);
printf(“a=%d\t”,–a); /*prefix decrement*/
printf(“a=%d\t”,a);
}
Postfix Increment/Decrement
Here first the value of variable is used in the operation and then increment or
decrement will be perform.
The statement y=x++; statement means the first the value of x is assign to y then
value of x is increase by one.
y=x;
x=x+1;
As the same the statement y=x–; means first the value of x is assigned to variable y
then value of x will decrease by one.
33 | P a g e LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
y=x;
x=x-1;
Relational operator in c
Operators Meaning
== equal to
!= Not equal to
Lets us take example with applying relational operators let us take two variables x=10 and
y=8 and form simple relational expressions with them–
Expression Relation Value of Expression
x<=y false 0
x<=y False 0
x==y False 0
x!=y True 1
x>y True 1
x>=y True 1
x==0 False 0
y!=0 True 1
X>1 True 1
10<20 True 1
Generally the relation operators are used with in the if else statement and loops so
now we will used relational operators with if else statement in the next program to
understand.
If the control statement evaluates any expression and that expression is true or (non
zero) then the next statement is evaluated or executed .
Otherwise the next statement is skipped or not show so in the below discussed
program with relation operator
#include<stdio.h>
void main()
int x,y;
scanf(“%d%d”,&x,&y);
if(x>y)
if(x>=y)
if(x==y)
if(x!=y)
Logical operators in c
When an Expression that combines two or more expressions is know as logical
expression.
To combining these expressions we use logical operators.
This logical operator return 0 for false and 1 for true.
The operands may be constant, expressions or variables .
In c language there are three logical operator –
Operator Name
&& AND
|| OR
! NOT
Here in this example NOT is a unary operator while the others two OR, AND are
binary operators.
Before using These operators let us understand the concept of these operators of true
and false.
In c language any non zero value regarded as true and the zero is regarded as false.
This operators gives the net result true if both condition will be true, Otherwise
result will be false.
Let us see Example
Boolean Table
OR (||) Operator
This OR operator gives the result false. if both conditions have the value false.
Otherwise the result is true
38 | P a g e LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
Boolean Table
Let us understand the concept of OR operator. so take three variables a=20, b=15, c=0;
This is a Unary operator and its negates the values of any condition.
So if the value of the condition is false then it gives the result true.
39 | P a g e LOYOLA INSTITUTE OF TECHNOLOGY & MANAGEMENT
DIPLOMA-CME PROGRAMMING IN C
And the value of condition is true then it gives the result false.
So now understand the concept of NOT operator.
Boolean Table
Condition Result
True false
false true
!a !true false 0
!c !false true 1
Conditional Operator In C:
Conditional operator is a ternary operator (? and ::) which is required three
expression as operand let us take example its written as –
x>y is evaluated if the value of this condition expression is true then the value of variable x
will be value of conditional expression otherwise the value of y becomes the value of
conditional expression
let us suppose x=6 and y=10 and now we are using above conditional operator.
Result=x>y ? x : y;
Here in the example the first expression x>y is evaluated so here in the x>y condition it is
false so the value of y variable becomes to the condition expression and finally its assign to
the result variable
scanf(“%d”,&y);
larger=x>y ? x : y;
printf(“The larger number is %d”,larger);
return 0;
}
Output :
Comma operator in c:
a=8;
b=5;
c=9;
d=10;
result=a+b+c+d;
Output:
result = 32
C control statements
Control Statements
In c programming, statements are executed sequentially or step by step in the order in which they
are write in the program.
Sometimes i want executes some parts of program on the basis of conditions.
Also some times many situations arise where we want to use any statement or some statements
several times.in all these conditions the control statements are use.
Goto statements in C
Simple if Statement
Simple if statements are carried out to perform some operation when the condition is
only true.
If the condition of the if statement is true then the statements under the if block is
executed else the control is transferred to the statements outside the if block.
If (Condition)
{
Statement-1;
}
Flow Chart:
https://www.simplilearn.com/tutorials/c-tutorial/conditional-control-statements-in-c