Beruflich Dokumente
Kultur Dokumente
Basant
September 19, 2016
Contents
1 Introduction to C language
1.1 What is C . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.1 Important Aspects of any Language . . . . . . . . . . .
1.1.2 History of C . . . . . . . . . . . . . . . . . . . . . . . .
4
4
4
4
.
.
.
.
4
4
5
5
5
.
.
.
.
.
6
6
6
7
7
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
8
8
9
9
9
9
10
10
11
5 Some C Programmes
11
6 C Instructions
6.1 Type Declaration Instruction . . . . . . . . . . . . . . . . . .
6.2 Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . .
6.3 Control Instructions . . . . . . . . . . . . . . . . . . . . . . .
13
13
13
14
7 Control Structure
7.1 for loop . . . .
7.2 factorial for . .
7.3 for for loop . .
7.4 if else loop . . .
7.5 for if else loop .
7.6 while loop . . .
7.7 do while loop .
7.8 tax calculation
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
8 Function
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
14
14
15
15
15
17
17
18
18
19
1
1.1
1.1.1
Introduction to C language
What is C
Important Aspects of any Language
Four important aspects of any language are the way it stores data, the way
it operates upon this data, how it accomplishes input and output and how
it lets you control the sequence of execution of instructions in a programme.
1.1.2
History of C
Communicating with a computer involves speaking the language that computer Understands which immediately seems out to be English as the language of communication with computer.
Learning C is similar and easier to learning the English. Instead of
straight-away learning. How to write programs we must first know what
alphabets,numbers and special numbers are used in C, then how using them,
constants, variables and keywords are constructed and finally, how are these
combined to form an instruction. A group of instructions would be combined
to form a program.
Steps in learning C language:
2.0.1
The following figure shows the valid alphabets, numbers and special symbols allowed
Alphabet
A, B, C.........X, Y, Z
Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
SpecialSymbols !@#%& () + = |{}[] :; 0 <>, .?
2.0.2
The alphabets, numbers and special symbols when properly combined form
constants, variables and keywords. Let us see what are constants and variables in C.
A constant is an entity that does not change, whereas a variable
is an entity that may change.
Types of C Constants
C constants are further classified into two major categories:
(a) Primary Constants:Integer Constant, Real Constant, Character Constant.
(b) Secondary Constants: Array, Pointer, Structure, Union, Num., etc.
2.0.3
2.1
2.1.1
2.1.2
2.2
2.3
Types of C Variables
An entity that may vary during program execution is called a variable. variable names are names given to locations in memory. These locations can
contain integer, real or character constants.
In any language the type of variable that it can support depends on the type
of constants taht it can handle. This is because particular type of variable
can hold only a real constant and a character variable can hold only a character constant.
The rules for constructing different types of character constant are.:
2.3.1
2.4
C Keywords
Keywords are the words whose meaning has already been explained to C
compiler. The keywords cannot be used as variable names because if we
do so, we are trying to assign a new meaning to the keyword, which is not
allowed to the computer. Thus keywords are also called as Reserved words.
There are only 32 keywords available in C.
auto
double
int
struct
break
else
long
switch
case
enum register typedof
char
extern return
union
const
f loat
short unsigned
continue
f or
signed
void
def ault
goto
sizeof
volatile
do
if
static
while
Armed with the knowledge about the types of variables, constants & keywords the next logical step is to combine them to form instructions.
Important points to remember before start writing our first C program are:
(a) Each instruction in a C program is written as a separate statement.
Therefore, a complete C program would comprise a series of statements.
(b)The statement in a program must appear in the same order in which we
wish them to be executed; unless of course the logic of problem demands
a deliberate a jump or transfer of control to a statement, which is out of
sequence.
(c) blank spaces may be inserted in between two words to improve the readability of the statement. However, no black spaces are allowed within a
7
4
4.1
4.2
main()
4.3
Variable
(a) Any variable used in the program must be declared before using it.
4.4
Statement
4.5
Operator
In the statement
si=p*n*r/100;
*and/ are the arithmatic operators. The arithmatic operators available in C
are +,-,* and /. C is very rich in operators. There are about 45 operators
available in C.
9
4.6
printf()
4.7
\n
10
4.8
To make the program general, the program itself should ask the user to
supply the values of p (principal amount), n (no. of years) and r (rate of
interest) through the keyboard during execution. This can be achieved using
a function called scanf(). This function is a counter-part of the printf()
function.
printf() outputs the values to the screen whereas scanf() receives them from
the keyboard.
/* Calculation of simple interest*/
/*Author Basant Date 16/09/2016*/
#include<stdio.h>
int main()
{
int p,n;
float r,si;
printf("Enter the value of p,n,r")
scanf("%d%d%f", &p, &n,&r);
si=p*n*r/100;
printf("%f\n",si);
return o;
}
The first printf() outputs the message Enter values of p, n,r on the screen.
Here we have not used any expression in printf() which means that using
expressions in printf() is optional.
Note that the ampersand (&) before the variables in the scanf() function
is a must. & is anAddress of operator. It gives the location numbers used
by the variable in the memory. When we say &a, we are telling scanf() at
which memory location should it store the value supplied by the user from
the keyboard.
Some C Programmes
{
float a,b,c,area,s;
printf("write the value of a,b,c");
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&c);
s=((a+b+c)/2);
area=sqrt((s-a)*(s-b)*(s-c));
printf("value of s=%f/n,value of area=%f/n",s,area);
}
2.\*Area of a Circle*\
#include<stdio.h>
#include<math.h>
int main ()
{
float A,r;
printf("write the value of r of circle");
scanf("%f",&r);
A=3.14*r*r;
printf("value of s=%f/n",A);
return(0);
}
3.\* Calculate z=((x+3)*x*x*x)/((y-4)*(y+5)) in C.*\
#include<stdio.h>
#include<math.h>
int main ()
{
float x,y,z;
printf("write the value of x and y");
scanf("%f",&x);
scanf("%f",&y);
z=((x+3)*x*x*x)/((y-4)*(y+5));
printf("value of z=%f/n",z);
return(0);
}
12
C Instructions
Let us look at the instructions that we used in these programs. There are
basically three types of instructions in C.:
(a) Type Declaration Instruction
(b) Arithmetic Instructions- It is used to perform arithmetic operations on
constants and variables. +, -, *,/ are the arithmatic operators.
(c) Control Structure
Since the elementary C programs would usually contain only the type of
declaration and the arithmetic instructions; we would discuss only these two
instructions at this stage.
6.1
6.2
Arithmetic Instructions
13
6.3
Control Instructions
7
7.1
Control Structure
for loop
#include<stdio.h>
int main()
{
int A[5],R,T,i=0;
float s[5];
for(i=1;i<5;i++);
{
printf("enter the amount:");
scanf("%d",&A[i]);
}
printf("enter the rate:");
scanf("%d",&R);
printf("enter the time:");
scanf("%d",&T);
for(i=1;i<5;i++);
{
s[i]=(A[i]*R*T)/100;
printf("ans:%d %f \n",i,s[i]);
}
}
14
7.2
factorial for
#include<stdio.h>
int main()
{
int f=1,i,n;
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("ANS: %d",f);
}
7.3
#include<stdio.h>
int main()
{
int A[5],R,T,i=0;
float s[5];
for(i=1;i<5;i++)
{
printf("enter the amount:");
scanf("%d",&A[i]);
}
printf("enter the rate:");
scanf("%d",&R);
printf("enter the time:");
scanf("%d",&T);
for(i=1;i<5;i++)
{
s[i]=(A[i]*R*T)/100;
printf("ans:%d %f\n",i,s[i]);
}
}
7.4
if else loop
#include<stdio.h>
15
int main()
{
float a,b;
printf("enter the first number:");
scanf("%f",&a);
printf("enter the second number:");
scanf("%f",&b);
if(a>b)
{
printf("first number is big:");
printf("%f",a);
}
else
{
printf("second number is big:");
printf("%f\n",b);
}
}
//program to check whether an integer entered by the user is odd or even
#include<stdio.h>
int main()
{
int number;
printf("enter the number:");
scanf("%d", &number);
if(number%2==0)
{
printf("number is even\n");
}
else
{
printf("number is odd\n");
}
}
16
7.5
#include<stdio.h>
#include<math.h>
int main()
{
int a,d,n,i,tn;
int sum=0;
printf("Enter the first term value of the A.P. series: ");
scanf("%d", &a);
printf("Enter the total numbers in the A.P. series: ");
scanf("%d", &n);
printf("Enter the common difference of A.P. series: ");
scanf("%d", &d);
sum = (n * (2 * a + (n - 1)* d ))/ 2;
tn = a + (n - 1) * d;
printf("Sum of the A.P series is: ");
for (i = a; i <= tn; i = i + d )
{
if (i != tn)
printf("%d + ", i);
else
printf("%d = %d ", i, sum);
}
return 0;
}
7.6
while loop
7.7
do while loop
7.8
tax calculation
#include<stdio.h>
#include<math.h>
int main()
{
float a,t,t1,t2,t3,z;
printf("enter the amount");
scanf("%f",&a);
18
if(a<35000)
{
t=0;
printf("tax:%f",t);
}
else if(a>35000&&a<60000)
{
z=a-35000;
t=z*20/100;
printf("tax:%f",t);
}
else if (a>60000&&a<120000)
{
z=(a-60000);
t1=(z*30)/100;
t2=(25000*20)/100;
t=t1+t2;
printf("tax:%f",t);
}
else if(a>120000)
{
z=a-120000;
t1=(z*40)/100;
t2=(60000*30)/100;
t3=(25000*10)/100;
t=(t1+t2+t3);
printf("tax:%f",t);
}
}
Function
#include<mathlib.h>
float F(float, float, float*, float*);
void main()
{
float a,b,c,d;
printf("Enter the value of a,b");
scanf("%f%f", &a, &b);
F(a,b,&c,&d);
printf("%f%f",c,d);
}
float F (float x, float y, float*s, float*av);
{
*s=x+y;
*av=(x+y)/2;
}
20