Notes Comp. Fundamental
Notes Comp. Fundamental
(Overview of C)
HISTORY OF C LANGUAGE: -
Dennis Ritchie is known as the founder of the C Language. C has also greatly influenced
many other popular languages especially to C++ which was originally designed as an
enhancement to C.
It is the most commonly used programming language for writing system software though it
is also widely used for writing applications.
C is structured programming language that is machine independent and used to write
various applications like windows and many other complex programs.
C is one of the high-level programming languages develop by Denis Ritche
C was originally developed for UNIX operating system to beat the issues of previous
language such as B, BCPL etc.
The Unix operating system development started in the year 1967 and its code was rewritten
in C in the year 1972.
Now a days C is exclusively used for building Os, application packages and customized
software because of its power and efficiency.
C is increasingly used by system programmers’ application developers and researches for a
spread of programming tasks.
It is considered a high-level language because it allows the programmer to concentrate on
the problem at hand and not worry about the machine that the program will be using.
IMPORTANCE OF C LANGUAGE: -
C is robust language and has rich set of built in functions, data types and operators which
can be used to write any complex program.
C has the capabilities of an assembly language with the feature of high-level language so it is
well suited for writing both system software and application software.
C is highly portable language i.e. code writing in one machine can be moved to other which
is very important and powerful feature.
C is a structured programming language which allow a complex program to be broken into
simple program called functions. It allows free movement of data across these function.
C language is case sensitive which means lowercase and uppercase letters are treated
differently.
C is a general-purpose programming language can efficiently work on enterprise applications
games, graphics etc.
C programs are efficient and fast.
C is an extensible programing language; this means that users can add their own function to
its library set.
C language available various version Lite Turbo C, Borland C, ANSI C.
C has a rich set of operators and provide compact representation to expressions.
C language contain 32 keywords.
ELEMENTS OF C: -
Character set = As every language contains a set of character used to construct word, Statements
etc. C language also has a set of character which include alphabets, digits and special symbols. C
language support a total of 256 character.
Evay C program contains statements These statements are constructed using words and
these words are constructed using character from C character set C language character set
contained the following set of characters
1. Alphabets
2. Digits
3. Special Symbol
1) Alphabets: - C language support all the alphabets from English language Lower and
uppercase together support 52 alphabets. (A to Z and a to z).
2) Digits: - C language support 10 digits which are used to construct numerical values in C
Language (0 to 9).
3) Special symbols: - C language support a rich set of special symbols that include symbol to
perform mathematical operations to check conditions, which spaces, backspaces and other
special symbols like ~ @ # % ^ & * () _ - {} etc.
Rule of identifier: -
1) The first character of an identifies should either an alphabet or an underscore; and then it
can be followed by any of the character, digits or underscore.
2) Must Consist only of alphabetic character, digit or underscore No extra symbols and allowed
other than letters, digits and "_".
They are always written in lowercase.
DATA TYPE: -
A data type specifies the type of data that a variable can store such as integer; floating character etc
i.e. C has different data types for different type of data and can be broadly classified as: -
C Data Types
PRIMARY
Character
Integer
Float
Double
void
SECONDARY
Array
Pointers
Structures
Union
Enum etc.
1. Integer: - Integer are whole numbers that can have both zero, positive and negative values
but no decimal values we can use “int” for declaring and integer.
The size of integer is usually 2. bytes-and its range limited to 12768 to +32767
Example: - int = datatype
Sum; = variable name
Syntax: - int < variable >;
Here int is a keyword which is used to define integer number
2. Floating data type: - The float data type is und to store fractional numbers (real numbers
with 6 digits of precision. Floating point numbers are denoted by the keyword “float” The
size of float is 4 bytes and range Limited to 3-4e-38 to 3-4е+38 A flout value consist of an
exponential and a fractional portion. Syntax float <variable names>
3. Character: - character type variable can hold a single character. we can use "char" for
declaring on character.
The size of character is 1 byte and its range limited to -128 to 127
The char data type is used for storing character such as alphabets and special symbols e.g. *,
$, # Computer use a numerical code called ASCII (America standard code for information
interchange) to represent this character.
(A-Z) = 65-90
(a-z) = 97-122
4. Void: - Void is an empty data type. The void type has no values therefore we Cannot declare
it as variable The void data type is usually used with functions to specify its return type
VARIABLE: - A variable is a name of the memory location. It is used to store data. Its
value can be changed and it can be reused many times.
It is the way to represent memory location through symbol so that it can be easily identified.
int a;
float;
char;
Here a, b, c are variable and int, float, char are data types.
Example: - a=10;
sum = 20;
“=” is the assignment operator and sum, a are the name of variable.
KEYWORDS: - Keywords are the reserved words which have predefined meaning in C
language. Since they are reserved for specific purpose that's why also known as reserved
words.
They are 32 keywords in C
These have predefined fixed meaning and uses cannot be changed for any other
purpose in a C program, hence cannot be used as variable names, function names
etc.
They are basic building block of the program.
A complete list of C keywords as follows: -
do if static while
SYMBOLIC CONSTANT: -
A symbolic constant is name given to some numeric constant, or character constant String constant
or any other constant Symbolic constant names are also known as constant identifiers. Pre-
processor directive # define is used for defining symbolic constants.
Syntax: -
Example: -
# define PI 3.141592
STRUCTURE OF C PROGRAM: -
A C program is divided into different section There are six main sections to basic C program.
The six sections are: -
1. Documentation section
2. Link section
3. Definition section
4. Global Declaration Section
5- Main function
6- Subprogram section
Example: - a+b
Here a and b are operands and + is operator the data item on which operators act upon are
called operands.
2. Relational operator: - Relational operates are symbols that are used to compare the value of
operands. They are used to find the relationship b/w two operands and called relational
operators.
Operators Names
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
The operands bring out a decision result in form of logical value i.e. true or false
Example: - 5>13 false
6>=6 true
12==13 false
3. Logical operators: - Logical operators are used to Combine two or more relational
expressions. After evaluation, expression consisting of logical operators results is either true
or false such type of expression are called logical expression Logical operators are commonly
used in decision making
4. Assignment operator: - An assignment operator assigns the value or result of the right-hand
side expression to the left-hand side value or variable. ''=” is known as assignment operator
which is uses to copy the result of an expression into a memory location identified by a
variable name.
Syntax: - variable name = expression;
Example: - a = 10;
B = 2+5;
5. Shorthand assignment operator: - C support a short variant of assignment operator called
shorthand assignment operator. shorthand assignment operator combines one of the
arithmetic operators with assignment operator.
Syntax: - variable operators=exp. a+= 2; a = a+2;
Example: - int a = 5
a = a+2;
a+=2;
Operator Name
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise X OR operator
~ Bitwise complement operator
<< Shift Left operator
>> Shift Right operator
Bitwise AND operator: - The output of bitwise AND 1 if the corresponding bits of
two operands is 1. If either bit of an operand is O, the result of corresponding bit is
evaluated to 0.
A=0011 1100
B=0000 1101
ARB=0000 1100
Bitwise OR operator: - The output of bitwise OR is 1 if at least one corresponding bit
of two operand is 1.
A=0011 1100
B=0000 1101
A|B=0011 1101
Bitwise X OR operator: - The result of bitwise XOR operator is 1 if the corresponding
bits of two operands are opposite.
A=0011 1100
B=0000 1101
A^B=0011 0001
Bitwise complement operator: - Bitwise complement operator is an unary operator.
It changes 1to0 and 0to1.
A=0011 1100
~A=1100 0011
Shift Right operator: - Right shift operator, shifts all bits towards right by certain no.
of specified bits. It is denoted by ».
A=||0||0||
A>>2
A=00||0||0
Shift Left operator: - shifts all bits towards left by certain no. of specified bits. It is
denoted by <<.
A=0|0| ||0|
A<<3
A=|||0|000
Unary operators: - Unary operators are operators that act upon a single operand to
produce a new value.
Example: - Increment (++)
decrement (--)
Address of operator (&)
Size of ()
Not (!)
ARITHMETIC EXPRESSIONS: -
An expression is combination of variable, constant and operators written according to the
syntax of C language i.e. an expression is a combination of arithmetic operators and
operand.
Arithmetic expressions are used to perform meaning computation.
Example: - 2/5÷2+5-6
Priority Operators
st
1 */%
2nd +-
rd
3 =
Rules for evaluation of expression: -
1. First parenthesis expression from left to Right are evaluated.
2. If parenthesis is nested then innermost expression evaluated first.
3. Arithmetic expressions are evaluated from left to right using the rules of precedence.
i =2*3/4+4/4+8-2+5/8
i = 6/4+4/4+8-2+5/8
i = 1+4/4+8-2+5/8
i = 1+1+8-2+0
i = 1+1+8-2+0
i = 2+8-2+0
i = 10-2+0
i = 8+0
8
i = 3/2*4+3/8+3
i = 1*4+3/8+3
i = 4+3/8+3
i = 4+0+3
i = 4+3
i=7
OPERATOR ASSOCIATIVITY: - The operator of the Same precedence are evaluated either
from left to Right or from right to left this is known as operator associativity.
1 / % → Left to right
= → Right to left
+ - → left to Right
OPERATOR HIERARCHY: - The higher the position of an operator in the table, higher its
priority.
! Logical NOT
*/% Arithmetic operator
+- Arithmetic
<><=>= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment
TYPE CASTING AND TYPE CONVERSION: - Type Casting is a way to convert a variable from
one data type to another data type.
Syntex: - (type name) expression
Example: - # include <stdio.h>
Void main ()
{
int Sum = 17, count = 5;
double mean;
mean = (double) sum /count;
printf ("Vales of mean: %f", mean);
}
Output: - Value of mean 3.400000
Type conversion
IMPLICIT TYPE CONVERSION: - If a compiler Converts one data type into another type of data
automatically is called implicit type conversion.
float a= 20.2;
EXPLICIT TYPE CONVERSION: - When data of one type is converted explicitly to another type with
the help of predefined functions. (done forcefully)
long double
double
float
long
int
short
char
UNIT 3rd NOTES
(Decision Making & Looping)
DECISION MAKING: - Decision making stricture requires that the programmer specifies one
or more conditions to be evaluated by the programs along with a statement to be executed
if condition is to be true and other statements to be executed of condition to be false.
Theres are 2 categories of control statement decision making statement
void main ()
int age;
2. If else statement: - This is used when you have two choices, one group of statement
is executed when condition is true and another group is executed when it is false. It
is also known as two-way branching statement or two-way decision statement.
SYNTAX
if (Condition)
{
Statement 1;
}
else
{
Statement 2
First condition is tested; if condition is true statement 1 is executed and if condition
is false statement 2 is executed.
Flow chart
Example
#include <stdio.h>
void main ()
{
int age;
printf (" enter age of person");
scanf ("%d", & age);
if (age>=18)
{
printf (" He/she have right to vote");
}
else
{
printf (" He/she has no right to vote");
}
}
3. Nested If statement: - Some times there is more than one condition that should be
true before certain action is performed. In that case nested if used. When a If
statement is used within another if statement it is called Nested If Statement.
SYNTAX
if (condition-1)
{
if (condition-2)
{
Statement 1,
}
else
{
Statement 2;
}
Else
{
statement 3;
}
First of all, condition 1 is tested if it is true condition 2 is tested if condition 2 is true, statement 1 is
executed if condition 2 is false statement 2 is executed. if condition 1 is false statement 3 is
executed.
FLOW CHART
Example
#include <stdio.h>
void main ()
if (var1! = var 2)
else
else
void main ()
int week;
Switch (week)
break;
break;
break;
case 4: printf("Thursday");
break;
break;
break;
Example: - if (a>b)
{
goto a1
}
else
{
goto a2;
}
a1: printf (" Largest number is %d", a);
a2: printf ("Largest number is %d", b);
In C program you can have or can use any number of goto statements but no two
statements can have same Label.
LOOPING STATEMENT: - These are those statements which can be used for repeating a
block of code in specified number of times of until the condition returns false. These are also
known as Iterative statements.
There are three types of wop statements:
1) while loop
2) do-while loop
3) for loop
1) while loop Statement: - A while loop in C programming repeatedly executes a target
statement as long as a given condition is true.
when the condition is false, the following body Statements is skipped and the control is
transferred out of the loop.
while loop is a entry controlled loop.
SYNTAX
initialization;
while (condition)
{
Statement;
}
while loop can be addressed as an entry control loop. It is completed in 3 steps:
1. Variable initialization (int x = 0)
2. Condition (x <= 10)
3. Variable increment/ decrement (x++ or x--)
FLOW CHART
Output = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
2) do-while loop: - In Some situation it is necessary to execute body of loop before testing the
condition. Such situation can be handled with the help of do-while wop. do statement
evaluates the body of loop first and at the end, the condition is checked using while
statement. It means the body of the loop will be executed at least once, even though the
starting condition inside while is initialized to be false.
SYNTAX
Initialization
Do
{
statement
Inc/dec
} while (condition);
Afta executing do statement the condition will be checked If it returns tree then only repeat
the Code otherwise control transfers to the next statement immediately after do-while loop.
FLOW CHART
Example: -
/* Program to print first to multiple of 5*/
#include <stdio.h>
void main ()
{
int a, i;
a=5;
do
{
Printf ("%d\t", a*i)
I++;
} while (i<= 10);
Output = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
3) for loop: - The for-loop statement works well where the number of iterations of the loop is
known in advance. The is known in advance. format of the wop consists of three parts
Separated by semicolons.
In for loop we have exactly two semicolons, one after initialization and second after the
condition.
In this loop we Can have more then one initialization or inc/dec separated by comma
operator
SYNTAX
for (initialization; condition; increment/dec)
{
Statement;
}
FLOW CHART
Example: - /* program to print first 10 Natural No*/
#include <stdio.h>
void main ()
int x;
for (x=1; x <= 10; x++)
{
Printf ("%d\t", x);
}
Output = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
NESTED LOOP: - Nesting of loop is the feature in C that allow you to define looping of
Statement inside Any number of loops can be defined inside another loop i.e. there is no
restriction for defining any number of loops. for example, you can define while loop inside a
for loop.
SYNTAX
Outer loop
inner loop
{
inner loop statement;
}
Outer loop statement:
Example: -
#include <stdio.h>
void main ()
int n;
print("\n");
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
BREAK STATEMENT: - The Break statement is used to exit from the loop constructs.
The break statement is usually used with the Switch statement and it can be also use it
within the while loop, do-while loop, or the fir loop.
When break statement is encountered then the control is exited from the loop construct
immediate
SYNTAX Break;
FLOW CHART
Example: -
#include <stdio.h>
int main ()
int i;
if (i ==3)
break;
return 0;
Output = 0, 1, 2
FLOW CHART
Example: -
#include <stdio.h>
int main ()
{
int i;
for (i=0; i<5; i++)
{
if (i ==3)
Continue;
Printf ("%d", i);
}
return 0;
}
Output = 0, 1, 2, 4