Notes For C Language Part 1
Notes For C Language Part 1
What is Complier?
Compiler is a program that converts the source code (program written in a
language other than machine language ) in to machine code. Computer
can understand machine code and execute machine code. Therefore we
can state that compiling is a process of converting source code to machine code
if source code is error free.
What is an IDE?
An IDE is nothing else but it is a program which includes all facilities to develop
and run program, such as editor, compiler, debugger and so on.
What is C?
C is a programming language developed at AT & B bell lab. of USA in 1972. It was
developed by Dennis Ritchie. It is user friendly, general purpose and reliable
language.
What is Syntax?
Syntax is correct way of writing statement according to rules suggested by
language.
What is variable?
Variable is name given to a location of computer memory where we can store
value and retrieve the stored value by variable name. Value of variable can
change during program run time and it is fundament requirement of any
programming language.
Rules for constructing identifier (variable/pointer/function name):
1. A variable name is any combination of alphabets, digits and underscore.
Some compiler supports 40 character lengthy variable names.
2. The first character in variable name must be alphabet.
3. No commas or blanks or special character can appear in variable name.
4. Variable name is case sensitive therefore variable Area and variable area are
different.
5. Variable name must not match with keywords.
e.g. few valid variable names are:
si_int
pop_pe_8
few invalid variable names are
a b /* we can not use space */
1b /* must start with alphabet */
char /* char is keyword */
What is keyword?
Keyword or reserved word is word whose meaning is well defined for compiler
we can use it in our program but we can not alter its meaning.
e.g. int, float, do, while etc.
There are such 32 keywords.
What is Expression?
An expression is formed using operator and operand. Operand can be variables
or constants on which operator works.
For example c=a+b;
In this statement ‘+’ is an operator .
a and b are operand.
How one can read or write values of variable or constant?/ explain component
of printf , scanf.
(a)Values of variable or constant can be read by:
scanf function whose syntax is
scanf(“format strings”,&var1,&var2…);
format string can be constructed using format specifier character e.g.
%c, %d, %u, %f.
Which format specifier to use? depends on data type of variable to read value
in.
Suppose variables are declared as
int a,b;
float e;
char c;
then we need statement
scanf(“%d %d %f %c”,&a,&b,&e,&c);
Since a and b are int types we used corresponding format specifier %d two times
because there are two int type variables to read values in.
Since f and c are float and char types respectively we used corresponding format
specifier %f and %c.
It means that no. of variables required read values in, must match with no. of
format specifier. Each data type uses a predefined format specifier and we have
to use the predefined one.
(b) writing of values of variable or constant: it can be done using printf
statement whose syntax is as follows
printf(“message to print”);
e.g. printf(“enter an integer”);
or
printf(“format string”,var1,var2);
if we have declared variables as:
int a=10;
float b=20;
we can give statement
printf(“%d %f”,a,b);
Since a is int we used %d format specifier.
Since b is float we used %f format specifier
The values of variable or constant to print replace format specifier at print time.
Therefore output will be
10 20.000000
if we give statement
printf(“a=%d,b=%f”,a,b);
output will be
a=10 b=20.000000
this function needs conio.h This function needs stdio.h This function needs conio.h
(2) We need not to press (2)We need to press enter(2)We need not to press
enter key after pressing a key after pressing a enter key after pressing a
key. character. key.
What is qualifier?
Qualifier modifies the behavior of the variable type to which they are applied.
We have seen declarations such as
int I;
This declaration specifies that is ‘I’ is an integer which can take both positive and
negative values, that is, I is a signed integer by default. The above declaration
could also be written as:-
signed int i;
qualifier can be two types:
a)Size qualifier
size qualifier modifies the range of value, a variable can store.
short: applies to int
long: applies to int and double
b)Sign qualifier
signed: applies to int, char
unsigned: applies to int, char
Character constant can have a single String constant can have one or more
character enclosed inside single quote. character enclosed inside double quotes.
a character constant takes just one byte ofa string constant takes no. memory that is
memory. sum of no. of characters inside double
quotes plus 1 byte taken by null character.
Assignment operators =
+=,-=,%=,/= etc.
Comma operators ,
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
c=a|b; first row is a’s bit pattern ,second row is b’s bit pattern ,third row is c’s bit
pattern
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
c=a^b; first row is a’s bit pattern ,second row is b’s bit pattern ,third row is c’s bit
pattern
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
c=a<<3; first row is a’s bit pattern ,second row is c’s bit pattern
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0
3
clearly c= a*(2 )
c=a>>3; first row is a’s bit pattern ,second row is c’s bit pattern
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
clearly c= a/(23)
c=~a; first row is a’s bit pattern ,second row is c’s bit pattern
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
Explain the behavior of right shift operator for positive and negative integer.
Zeros are always inserted in the left if integer is positive otherwise 1 is inserted
in the left, if integer is negative when using right shift operator.
if we assign -3 to a variable ‘a’
int a=-3;
then binary representation of ‘a’ in two’s complementary system is computed as
follows:
binary representation of positive 3 is
0000 0000 0000 0011
taking complementary
1111 1111 1111 1100
by adding 1 to get two’s complementary
1111 1111 1111 1101 which is binary pattern of -3
now c=a>>2;
will shift the value of a by 2 bit positions to the right and will insert two 1’s in the
left. so value of c will be
1111 1111 1111 1111
What is the difference between a++ and ++a/(post increment and pre increment
operator)?
(a)If we use following statements
int a=5,b;
b=++a;
printf(“\n a=%d b=%d”,a,b); output will be a=6 and b=6.
(b)If we use following statements
int a=5,b;
b=a++;
printf(“\n a=%d b=%d”,a,b); output will a=6 and b=5.
1. if (condition)
do this;
2. if (condition)
{
do this;
and this;
}
3. if (condition)
do this;
else
do this;
4. if (condition)
{
do this;
and this;
}
else
{
do this;
and this;
}
5. if (condition)
do this;
else
{
do this ;
and this;
}
6. if (condition)
{
do this;
and this;
}
else
do this;
Write program to find root of quadratic equation.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,r1,r2;
printf(“\n enter coefficient of quadratic equation”);
scanf(“%f %f %f”,&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf(“\n roots are equal\n”);
r1=-b/(2*a);
r2=-b/(2*a);
printf(“\n root1=%f,root2=%f”,r1,r2);
}
else if(d>0)
{
printf(“\n roots are real and different\n”);
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf(“\n root1=%f,root2=%f”,r1,r2);
}
else
{
printf(“\n roots are imaginary\n”);
r1=-b/(2*a); /* real coefficient */
r1=sqrt(-d)/(2*a); /* imaginary coefficients */
printf(“\n root1=%f + %f i=%f”,r1,r2);
printf(“\n root1=%f - %f i=%f”,r1,r2);
}
getch();
}
Write short notes on ‘while’ (entry level control structure/test and do) loop.
In programming we require to execute same set of instructions a fixed number
of times. E.g. we want to calculate gross salary of ten different persons, we want
to convert temperatures from centigrade to Fahrenheit for 15 different cities.
The ‘while’ loop is suited for this.
/* calculation of simple interest for 3 sets of p,n and r */
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,t,i;
int count;
count=1;
while(count<=3)
{
printf(“\n enter principal,rate and time”);
scanf(“%f %f %f”,&p,&r,&t);
i=p*r*t/100;
printf(“\n simple interest =%f”,i);
count=count+1;
}
getch();
}
Explanation: the program executes all statements inside braces after the while 3
times. The logic for calculating the simples interest is written within a pair of
braces immediately after the while keyword. The statements form what is called
the body of the while loop. The parentheses after the while contain a condition.
So long as this condition remains true all statements within the body of the while
loop keep getting executed repeatedly. To begin with the variable count is
initialized to 1 and every time the simple interest logic is executed the value of
count is incremented by one. The variable count is known as loop counter or
index variable.
General format is
1.
initialize loop counter
while(condition is true)
{
do this;
increment loop counter;
}
Note: do not use ‘;’ after closing parenthesis.
As a rule the ‘while’ loop must test a condition that will eventually become false,
otherwise the loop would be executed forever, indefinitely.
write program to read a seven digit no. and find its sum of digit.
#include<stdio.h>
#include<conio.h>
void main()
{
long n;
int s;
printf(“\n enter a number”);
scanf(“%ld”,&n);
for(s=0;n>0;n=n/10)
{
s=s+n%10;
}
printf(“\n sum of digit=%d”,s);
getch();
}
Write short notes on do-while( exit level control structure/do and test) loop.
In programming you require to execute a set of statement a fixed number of
times. Perhaps you want to calculate gross salary of ten different persons, or you
want to convert temperatures from centigrade to Fahrenheit for 15 different
cities. The do while loop is suited for this.
/* calculation of simple interest for 3 sets of p,n and r */
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,t,i;
int count;
count=1;
do
{
printf(“\n enter principal,rate and time”);
scanf(“%f %f %f”,&p,&r,&t);
i=p*r*t/100;
printf(“\n simple interest =%f”,i);
count=count+1;
} while(count<=3);
getch();
}
Explanation: the program executes all statements inside braces inside do-while
loop body 3 times. After do keyword the logic for calculating the simples interest
is written within a pair of braces then while is followed by condition. The
statements form what is called the body of the do- while loop. The parentheses
after the while contain a condition. So long as this condition remains true all
statements within the body of the do-while loop keep getting executed
repeatedly. To begin with the variable count is initialized to 1 and every time the
simple interest logic is executed the value of count is incremented by one. The
variable count is known as loop counter or index variable.
Output will be 1,1 infinite no. of times because only printf statement will run and
statement to increment value of ‘i’ does not execute so braces are necessary to
execute printf and i=i+1 statements.
3
int i;
i=1;
for (;i<=10;i=i+1);
{
printf(“\n %d”,i);
}
Output will be 11 because closing parentheses is followed by ‘;’ which instructs
execution of loop without loop body.
4
int j,i;
for (i=1,j=1;i<=10;i++,j++)
{
printf(“\n %d %d”,i,j);
}
The keyword break takes the control out of the loop inside which it is placed.
Consider the following program which illustrates this fact.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0;
while(i<=10)
{
i++;
j=0;
while(j<=10)
{
j++;
if (j= =5)
break;
printf(“\ni= %d,j= %d”,I,j);
}
}
getch();
}
output will be
i=1 j=1
i=1 j=2
i=1 j=3
i=1 j=4
i=2 j=1
i=2 j=2
i=2 j=3
i=2 j=4 and so on for value of ‘i’ from 1 to 10
Write short notes on continue statement.
On some programming situations we want to take the control to the beginning
of the loop, bypassing the statements inside the loop which have not yet been
executed. The keyword ‘continue’ allows us to do this. When the keyword
continue is encountered inside any loop, control automatically transfers to:
1. Test condition if using ‘while’ or ‘do-while’ loop.
2. Increment/decrement/update section if using ‘for’ loop.
Now consider program to find division of exactly three sets of two nos. if user
enters denominator as ‘0’ program will ask to reenter the two nos.
#include <stdio.h>
#include <conio.h>
void main()
{
float a,b;
int i=1;
while(i<= 3)
{
printf(“\n enter %d set of numerator and denominator”);
scanf(“%f %f”,&a,&b);
if(b= =0)
continue;
printf(“\n division =%f ”,a/b);
i=i+1;
}
getch();
}
in above program user has to enter exactly three sets of two nos where
denominator is not zero. If user enters zero as denominator; user has to re-enter
the two nos. because of continue statement’s execution, control is transferred to
conditional test after bypassing statements: printf and i=i+1.
Continue Break