Structured programmer language “C”
C language is structured programming language. It’s a case sensitive language before
it was called as B, Basic Compiled Programming language. In this language we are using
compiler to execute. The complier name is TC (turbo C).
Parts of the program In “C” language
1. declarations
2. input statements
3. Arithmetic and logical works
4. Output statements
Functions
Header file Library File
Standard input output , header file
( stdio.h
( conioh ( console input , output) Get CH
It helps to execute
Graphic .h
void main()
Void main ()
Variable Declaration
Input
Calculation
Output
Functions
{} set brackets
[] square bracket
Main.obj
Main Cc
Main Compiler
Main.bak
(file Name )
Main.exe
Main.C ( souce file)
Note: single has comment //
Structured programmer language “C”
Multiple comment /+ ----- +/
Key words
Keywords are reserved words. In C language there are 32 keywords. These keywords
cannot be used as variables eg: if, in, else, for, while, void , int etc.
Data types
- Integer int
- Decimal float
- Character char
Char x;
String Char x[10]
In c language, there are three types of data can be used
Note all the variable should be declared before it used
Eg: int a ‘
A is variable it can store integer value
Variable: is to store data.
The value of the variables can be changed during the execution of the program
the variable name stats with alphabets.
Numerals and special characters are not allowed at beginning of the variable.
The blank spaces does not allow between the variables.
Underscore is allowed. The name of the variable should be in understand format
& = memory address operator
Scanf(“format specifer”,& variable name);
Format specifer
Integer %d
Float %f
Character %C
string %s
Format specifer: it tells to complier about the type of data which we are
getting from the keyboard.
Eg scanf(“%d”,&a1);
Scanf(‘%f”,&salary);
Scanf(“%c”,&option);
Scanf(“%s”,&name);
Scanf(“%d%f%c%s”,&a1,&salary,&option,&name);
// - used for comments
\n – new line
//program for scanf ()
#include<stdio.h>
#include<conio.h>
Void main ()
{
Int a;
Structured programmer language “C”
Char x, name [20];
Clrscr ();
Printf (“enter the integer value \n”);
Scanf (“%d”, &a);
Printf (“the given integer value is %d”,a);
Getch ();
}
Write a C program to read a float value and display?
#include<stdio.h>
#include<conio.h>
Void main()
{
Float a;
Clrscrn();
Printf(“float value \n”);
Scanf(“%f”,&a);
Printf(“the given float is %f”,a);
Getch();
}
Error names: statement missing
It to programmer, there was a mistaken from the previous line that is , we miss
the semi colon from the previous line .
Undefined symbols: the compiler does not understand your variable names in the
program. All the variables must be declared before it has used. Sometimes we use
Printf instead of printf
Unable to open OBJ file, linker, library, output directory path was not set
properly
//program for float value and fixed ()
#include<stdio.h>
#include<conio.h>
Void main ()
{
Float a1,a2,a3;
Clrsrn’
Prinf(“enter the decimal vale\n”);
Scanf(“%f%f”,&a1,&a2);
A3+a1+a2;
Peintf(“the amount of %.2f”,a3)
Getch()’
}
Read the employee salary, traveling allowance and medical allowance
//progarme for employee salry ()
#include<stdio.h>
#include<conio.h>
Void main ()
{
Float e,t,m;
Clrsrn’
Prinf(“enter the decimal vale\n”);
Structured programmer language “C”
Scanf(“%f%f%f”,&e,&t,&m);
A1= e+t+m;
Peintf(“the amount of %.2f”a1,)
Getch()’
String :
//read a string and display ()
#include<stdio.h>
#include<conio.h>
Void main ()
{
Char x[20];
Clrscr();
Printf(“enter the string\n”);
Scanf(“%s”,&x);
Printf(“\n the given string is %s”,x);
Getch;
}
Wrte a progarme fro emplee id , name , address, salary and
do the following calculation
TA( traveling allowance ) salary *7 /100
Ma ( medical allowance ) sa;ry *9/100
Hra salary +10/100
Fa( food allawnace ) salry * 11/100
Loan =salry *1/100
Gross =salry +ta+ma+hra+fa
Net=gross-loan
//progarme for employee salry ()
#include<stdio.h>
#include<conio.h>
Void main ()
{
Int a; //emplee id
Char x[20],y[20]
Float b,ta,ma,hra,fa,oan,gross,net;
Clrscr();
Printf(“ \n enter the emplee id”);
Scanf(“\n %d”,&a)
Printf(“ \n enter the name”);
Scanf(“\n %s”,&x)
Printf(“ \n enter the address”);
Scanf(“\n %s”,&y)
Printf(“ \n enter the salary”);
Scanf(“\n %f”,&b)
Ta=b*7/100
Ma=b*9/100
Hra=b*10/100
Fa=b*11/100
Loan=b*1/100
Structured programmer language “C”
Gross=b+ta+ma+hra+fa
Net=gross-loan
Printf(“ \n the amount of ta %f”,ta);
Printf(“ \n the amount of ma %f”,ma);
Printf(“ \n the amount of hra %f”,hra);
Printf(“ \n the amount of fa %f”,fa);
Printf(“ \n the amount of loan %f”,loan);
Printf(“ \n the amount of gross %f”,gross);
Printf(“ \n the amount of net %f”,net);
Getch();
}
Write a progarme fro simple interest and display
it?
SI =PRN
/* this is my thired programe
date 04/04/2010*/
//#include<stdio.h> /headerfile
//#include<conio.h>
void main()
{
float si,p,r,n;
clrscr();
printf("\n enter the principle amount");
scanf("\n %f",&p);
printf("\n enter the rate of interest");
scanf("\n%f",&r);
printf("\n enter the number of years");
scanf("\n%f",&n);
si=p*r*n/100;
printf("\n simple amount is %.2f",si);
getch();
}
Write A C PROGARME TO READ A DOLLEER VALUE AND CONVERTION INTO THE RUFIYAA VALUE
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Incremnet and decremenets
These methods are used when in looping
Incremnet = post incerment and pre incremnet
Decermenet = post decrement and pre decremenet
Pre incremnet : ++I
The value of the varibele I is inceremeneted by one , then it will used by
another varibles .
Exmaple x = 100
Structured programmer language “C”
A =++x
In this case the value of the varible “x” is incremnetd from 100 to 101 then its
been asssign to a
Post increment : I++
The value of the varible I is assgin to another varible first then it will be
incremneted by one
Example
X=100
A=X++
out put is A =100
X =101
Because x is assgin frist , aftre assgning it been incremneted
/* this is 5th programe
date 20/04/2010*/
//#include<stdio.h> /headerfile
//#include<conio.h>
void main()
{
Int x, y;
Clrscr;
X=100,
Y=++x
Printf(”value of x is %d\n”,x),
Prinf(“ the value of y is %d\n”,y),
getch();
-----------------------------------------------------------------------------
/* this is 6th programe
date 20/04/2010*/
//#include<stdio.h> /headerfile
//#include<conio.h>
void main()
{
Int x, y;
Clrscr;
X=100,
Y=x--
Printf(”value of x is %d\n”,x),
Prinf(“ the value of y is %d\n”,y),
getch();
}
----------------------------------------------------------------------------
/* this is 7th programe
date 20/04/2010*/
//#include<stdio.h> /headerfile
//#include<conio.h>
void main()
{
Structured programmer language “C”
Int x;
Float b;
Char c;
Clrscr;
Printf(”enter the value of interger %d\n”,x);
scanf(“%d,&a”);
printf(“vaue of x is float\n’,x”);
scanf(“%f,&b”,b);
printf(“value of x is charcater\n’,x”);
scanf(“ %c,&c”);
printf (“values are fllows”)
printf(“%d\t%f\t%c”)
getch();
Diffrence between character and string
charcterconstant String constant
Char option
Option-‘y’;
Char symboles
Symbles=’#’
----------------------------------------------------------------------------
/* this is 8th programe
date 25/04/2010*/
#include<stdio.h> /headerfile
#include<conio.h>
void main()
{
Int year , reminder;
Clrscr();
Printf(”enter the year\n”);
Scanf (“%d,&year”):
Reminder=year%4;
IF(reminder==0)
Printf(“year %d is leap year”,year);
Else
Printf(“year %d is not a leap year”,year);
Structured programmer language “C”
Getch();
----------------------------------------------------------------------------
/* this is 9th programe
date 25/04/2010*/
#include<stdio.h> /headerfile
#include<conio.h>
void main()
{
Int m1,m2,m3,m4,total
Float arg;
Char name[20] *grade{10},
Clrscrn;
Printf(“ \n enter the name:\t”);
Scanf(“ %s”,&name);
Printf(“”)
Switch case
It’s a multi branching control statement
General format
Switch(condition)
Case value:
-----------------------------------------------------------------------------------\--
-------------------------------------------------------------------------------------
Break;
Case value2:
Break;
Case value3:
Structured programmer language “C”
Break;
Default:
Break;
In switch case there many available cases. In case consider as branch. The switch
condition has to be evaluated at first.
The switch condition value compare with case values. Which case value is match with
switch condition value that specific case will be executed
If there is no match found the control will go to default branch.
Note: every case have break statement. When the control comes to break statement, the
switch case will be terminated
Ex: char value;
Value =’a’,
Switch(value)
Case’a’’
Printf(the letter a aplhepet a”);
Break;
Case’e’
Printf(“the letter is alpaheebt e”);
Break;
Default:
Printf(“no value match”);
Structured programmer language “C”
Break;
#include<stdio.h>
#inculde<conio.h>
void main()
char option;
clrscr();
printf("enter a letter \n");
scanf(" %c",&option);
printf("the given letter is %c \n",option);
switch(option)
case'a':
case'A':
case'e':
case'E':
case'i':
case'I':
case'o':
case'O':
case'u':
case'U':
printf("the letter is a vowel");
break;
default;
printf("this is not a vowel");
getch();
}
Structured programmer language “C”
#include<stdio.h>
#inculde<conio.h>
void main()
int a,b,c,option
clrscr();
printf("\n\t\t menu\n\n");
printf("\t\t 1.....add\n");
printf("\t\t 2.....sub\n");
printf("\t\t 3.....mulit\n");
printf("\t\t 4.....division\n");
printf("\t\t 5.....close\n");
printf("\n select your choice\t");
scanf("%d,&option");
printf("enter two number\n");
scanf("%d%d",&a,&b);
switch(option)
case1:
c=a+b;
break;
case2:
c=a-b;
break;
case3:
c=a*b;
break;
case4:
if (b==0||b,0)
Structured programmer language “C”
printf("deiviosn not possible");
if(b.0)
c=a/b;
default:
printf("select proper option\n");
break;
print("result is %d",c)
getch();
Itrative statement
1. Do loop
2. While loop
3. For loop
Part of Itertive statement
a. Intailize
b. Incerment/decrements
c. Condition
Do loop
General format
Initail Value
Do
Incremnet or dercremnet
While (lgigal condition)
Structured programmer language “C”
EXCESIE 1
N=1;
Do
Printf{“welcome”};
N=n+1;
While (n<10);
In the above example the value of variable n=1, it is called finalize variable
Then do loop, has to be started, inside the do loop there certain artthemiatic and
processing statements is available
And end of the do loop the intize variable should be increment or decrement depends on
the condition.
Then the increment variable has to be verified with logical condition. when the
condition is true again the do loop will be continued .
The do loop will be executing continually until the given condition is not satisfied.
If you miss increment variable , or if the increment variable is not given properly
.it will lead to infinite loop
Do a program for number of times
#include<stdio.h>
#include<conio.h>
void main()
int max,i;
char *name[20];
*name="the devil";43c e3f3r
clrscr();
printf("\n how many times you wnat to display you name\n");
scanf("%d",&max);
i=1;
do
Structured programmer language “C”
printf("my name is %s\n"*name);
i=i+1;
while(i<=max);
getch();
For-loop
General format
For (initialize, condition, increment)
example
For (i=o;I<=10;i++)
}
Structured programmer language “C”
Write a c-progarme to display your name in “n” using for loop
#include<stdio.h>
#include<conio.h>
void main()
int n,i,total,a[100];
clrscr();
printf("how many time you wnat to entrer");
scanf("%d",&n);
printf("enter the number one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
total=0;
for (i=0;i<n;i++)
total=total+a[i];
printf("the total %d",total);
getch();
Structure
1. Variable=> store the data
2. Array=> many data stored at one variable
Structured programmer language “C”
3. Structure =>different type as data (disimialr type of data)
can store in one variable name
How gto declare
General format
Struct structure name
Int id
Float salary
Char // these are structure number
};
Struct structure name variable name;
File concept
How to declare
1. Declare the file pointer variables
FILE *FP
Data type file pointer
2. The text file should be opned using file pointer and file
mode( file mode means to write and read purpose)
There are three file mode
Write : it
Read: it just read
Append
File =fopen( “ab1.txt”, “w+”)
Structured programmer language “C”
#include <stdio.h>
#include<conio.h>
void main()
struct student
int id;
float m1,mt,m3,tot;
char name[20];
struct student s;
file *fp;
int recsize;char option;
clrscr;
fp=fopen(“stud.txt”,”w+”);
recsize=sizeof(s);
do
Clrscr
Printf(“enter the id \t”);scanf(“%d”,&s.id);
Fwrite(&s,recsize,1,fp);
Printf(do u want to add another recosrd”);
Sacnf(“%c”,&option
While ( option==”y”);
Structured programmer language “C”
Getch()
String fuction
Its is a collection of Alpha and neumericals and symbols
How to declare string variable
Char varibelname [size];
Example char a [100];
a= the variable
100= the size of variable
The format specifer for string is %s
#include<string.h>
Strlen(string);
Structured programmer language “C”