Working with character data type.

C language handles character data in a very different way every character value that is assigned to a character variable is treated as its ASCII value.

Following program shows how a character variable is handeled in C language.

#include< stdio.h>
#include< conio.h>
void main()
{
char ch1,ch2;
clrscr();
ch1='A';
ch2=ch1+1;
printf("the character is %c",ch1);
printf("the ASCII value is %d"ch1);
printf("the next character is %c",ch2);
ch2=ch1-1;
printf("the previous character is %c",ch2);
getch();
}


Note -: A character variable is strongly related with integer data type .a character variable when displayed with %c control string it displays the character values but when displayed with %d it displays the ASCII value of that character.

Program that stores 'X' to a character variable and displays two characters next and two character previous to it.


#include< stdio.h>
#include< conio.h>
void main()
{
char ch1,ch2,ch3,ch4,ch5;
clrscr();
ch1='X';
ch2=ch1+1;
ch3=ch2+1;
ch4=ch1-1;
ch5=ch4-1;
printf("the character next to X is %c",ch2);
printf("the two character next to X is %c",ch3);
printf("the character previous to X is %c",ch4);
printf("the two character previous to X is %c",ch5);
getch();
}


Program that stores 'A' ,'B',and 'c' into three variables and displays the small case characters of these three variables.


#include< stdio.h>
#include< conio.h>
void main()
{
char ch1,ch2,ch3,ch4,ch5,ch6; clrscr();
ch1='A';
ch2='B';
ch3='C';
ch4=ch1+32;
ch5=ch2+32;
ch6=ch3+32;
printf("the character next to X is %c",ch2);
printf("the two character next to X is %c",ch3);
printf("the character previous to X is %c",ch4);
printf("the two character previous to X is %c",ch5);
getch();
}


scanf()

scanf is a function defined in stdio.h and is used to bring values from the console screen into the variable thus, scanf() can perform data input function for variables of any data type defined in C language. Syntax-: scanf("%d%f",&a,&b);

Program that inputs two integer values and swaps.


#include< stdio.h>
#include< conio.h>
void main()
{
int num1,num2,temp;
clrscr();
printf("enter two numbers");
scanf("%d%d",&num1,&num2);
temp=num1;
num1=num2;
num2=temp;
printf("the first value was %d" ,num1);
printf("the first value was %d" ,num2);
getch();
}


Program that inputs an amount and distributes the maximum number of currency notes required use the notes of 1000,500,100,50,20,10,5,2,1.


#include< stdio.h>
#include< conio.h>
void main()
{
int amt,c1,c2,c3,c4,c5,c6,c7,c8,c9,total;
printf("enter the amount");
scanf("&d",&amt);
c1=amt/1000;
amt=amt%1000;
c2=amt/500;
amt=amt%500;
c3=amt/100;
amt=amt%100;
c4=amt/50;
amt=amt%50;
c5=amt/20;
amt=amt%20;
c6=amt/10;
amt=amt%10;
c7=amt/5;
amt=amt%5;
c8=amt/2;
amt=amt%2;
c9=amt/1;
amt=amt%1;
total=c1+c2+c3+c4+c5+c6+c7+c8+c9;
printf("number of noted of 1000 are %d",c1);
printf("number of noted of 500 are %d",c2);
printf("number of noted of 100 are %d",c3);
printf("number of noted of 50 are %d",c4);
printf("number of noted of 20 are %d",c5);
printf("number of noted of 10 are %d",c6);
printf("number of noted of 5 are %d",c7);
printf("number of noted of 2 are %d",c8);
printf("number of noted of 1 are %d",c9);
printf("the total number of notes are are %d",total);
getch();
}

Program that inputs the marks of 5 subjects and displays percentage.


#include< stdio.h>
#include< conio.h>
void main()
{
float s1,s2,s3,s4,s5,p,t;
clrscr();
printf("Enter the marks of 5 subjects");
scanf("%f%f%f%f%f",&s1&s2&s3&s4&s5);
t=s1+s2+s3+s4+s5;
p=(t/500)*100;
printf("the percentage of student is %f",p);
getch();
}

Program that input basic salary ,ta da ,hra and print gross salary.


#include< stdio.h>
#include< conio.h>
void main()
{
long float basic,ta,da,hra,gs;
clrscr();
printf("Enter the basic salary of empoyee");
scanf("%lf",&basic);
printf("Enter the percentage of TA of empoyee");
scanf("%lf",&ta);
printf("Enter the percentage of DA of empoyee");
scanf("%lf",&da);
printf("Enter the percentage of HRA of empoyee");
scanf("%lf",&hra);
ta=(ta/100)*basic;
da=(da/100)*basic;
hra=(hra/100)*basic;
gs=ta+da+hra+basic;
printf("the gross salary of employee is %lf",gs);
getch();
}

Program that calculates area and circumference of circle.


#include< stdio.h>
#include< conio.h>
void main()
{
int ar,cr,r;
clrscr();
printf("Enter the value of radius");
scanf("%f"&r);
ar=pi*r*r;
cr=2*pi*r;
printf("the area of circle is %d",ar);
printf("the circumference of circle is %d",cr);
getch();
}

getchar() function

getchar is a function that takes a single character from the console and stores it into a variable getchar() requires enter button to be pressed necessarily

getche() function

it is quite similar to getchar() but it does not require enter instead any key can be pressed here, e means echo

getch()function

getch() too is similar to getchar() or getche but it does not require any further key to be pressed.

Syntax of the above listed all three funnctions-:

a=getchar();
a=getch();
a=getche();

Formatted Output

Our output in C can be formatted in such a way that it looks like what we wanted, for this we generally modify our control strings.
For example -:

float a;
a=34.68932;
printf("%5.2f",a);

Here, %5.2f will work like-:
5 total digits and 2 is number of digits after decimal point
Thus, the final output will look like-:  34.68

The following modification describe a concept more precisely


printf("%f",a);       -> 34.68932
printf("%5.2f",a);  -> 34.68___
printf("%6.2f",a);  -> 034.68__
printf("%-5.2f",a); -> ___34.68
printf("%-6.2f",a); -> __34.68_