input / output in C
most languages have facility for input and output, built into the language. the C language does not have built in statement of I/O full stop it has function to achieve the I/O. the standard I/O function ( scanf(),printf()) are normally performed by using the "studio.h" header file. it has conio.h header file to perform console I/O function(gets), puts(),getchar,etc.
standard I/O functions
* scanf()
this function allow to enter data from keyboard that will be formatted in a certain way full stop the journal form of scanf( ) statement is
scanf(" formatted string", list of address of variables):
FOR EXAMPLE
scanf("%d%F,"&a,&b);
here and donate the address of operator this is necessary because the values received from keyboard must be dropped in two variables corresponding to this addresses.
%d and %f are format specifiers which work as conversion characters. format specifier used to in C are as follows:
format specifier data types
- %d,%i signed integer
- %u unsigned integer
- %ld,%li long integer
- %lu unsigned long integer
- %f float
- %lf,%le,%lg double
- %Lf,%Le,%Lg long double
- %c character
- %s string, group of characters
* printf()
this function writes formatted data to screen. this function allowed to supply the input in a fixed format and to obtain the output in the specified form. the printf() function interprets the contact of the format string.
syntax
printf("formatted string" , variables);
example 1
printf (" average =%d percentage=%f",avg.per);
here the %d and %f are conversion characters.
They printf() to print the value of avg as an integer and per as a float.
example 2.
printf (" hello student \n how\t are\t you");
here \n, \t are escape sequence. escape sequence used to in C are
escape sequence function (output)
- \a beep (alert)
- \b black space
- \n new line
- \t horizontal tab
- \\ backslash
- \, single quote
- \" double quote
- \V. vertical tab
- \? question mark
- \0000. code specified in octal
- \xhh code is specified in hexadecimal
console I/O functions
- getch() reads a character from keyboard
- getche() read a character from keyboard and echoes it
- getchar() reads a character from keyboard (macro version)
- gets() read a line from keyboard
- putchar() writes are to a screen (macro version)
- puts() writes a line
- scanf() reads formatted data from keyboard
- printf() writes formatted data to screen
control statements in 'C'
to control the flow of the execution of program, the C language provides the following statements
conditional statements:
these statements enable us to change the flow of the program. in other words, these statements are used to transfer the control from one point to another point (depend on condition(s)). in C language following conditional statements are used:
if statement/if-else statement
This statement allows decisions to be made by evaluating a given condition as true or false. The keyword it tells the compiler that it is a decision control instruction. the condition if is always enclosed within a pair of Parenthesis. The relational operators allow us to compare two values to see whether they are equal to each other, greater or less than the other. The general form of if statement is
if ( condition)
{
statements
}
·in the above example statements are executed when condition is true
if ( condition (s) )
{
statement 1
}
else
{
statement 2
}
In t this statement 1 will be executed when the condition is true otherwise statement 2 will be executed.
Switch Case Statement :
This control statement, which allows us to make a decision from the number of choices is called a switch. With the help of switch and case statements we may choose any number of decisions, or more correctly a switch-case default, since the combination of these three key words go together to make up the control statement.
Syntax=>
switch ( expression)
{
case 1:
statements (Group 1)
case 2 :
statements (Group 2)
case 3 :
statements (Group 3)
default:
statements ( Group 4)
}
The switch statement checks, whether an expression matches with number of integer or character constant.
Example : for "if-else"
# include < stdio.h >
void main ( )
{
int age;
printf ("Enter your age");
scanf("% d", & age);
if (age>= 18)
{
printf("You can vote");
else
printf ("You can't vote");
}
getch();
}
Example : for "switch -case"
To calculate simple interest and compound interest.
#include< stdio.h >
#include< conio.h >
void main ()
{
int ch;
float p, r, t, si, ci;
clrscr ( );
printf ("Enter principle");
scanf("¾f", & p);
printf ("Enter rate \n");
scanf ("%f", & r);
printf ("Enter time \n");
scanf ("% f", & t);
printf("\n 1. Simple Interest");
printf("\n 2. Compound Interest");
printf("\n Enter your choice \n'-');
scanf ("% d ", & ch);
switch (ch)
{
case 1:
si = (p * r * t) / 100 ;
printf ("% f\ n", si);
break;
case 2:
ci = p ( ( 1 + r/100) ^t -p);
printf (" % f\ n", ci );
break;
default:
printf ("wrong choice");
}
getch ( );
}
* Looping Statements :
A computer program is a set of statements, which is normally executed sequentially. But in most of the cases it is necessary to repeat certain steps to meet a specific condition. This repetitive operation is done through a loop control structure.
A loop is basically the execution of sequence of statements repeatedly until a particular condition is true or false. In 'C' language following loop. statements are used: . mg. while Statements : The while loop repeats a statement or a set of statements until a certain condition is true.
Syntax:
while ( condition)
{
statements
}
Here, the condition may be any expression having non-zero value. The loop continues until the condition is true. When the condition fails, the program body attached with loop (statements), will not be executed.
Example : To print first 20 natural numbers.
# include < stdio.h >
#include< conio.h >
void main ( )
{
int a= 1;
while (a<= 20)
{
printf ("% d", a);
a++;
}
getch ();
}
• do-while statement:
Working of this statement is similar to the working of while statement but in this at least one time the attached loop (statements) is executed, ' no matter whether the condition is true or false.
Syntax⇒
do
{
statements
}
while ( condition);
Example:SYNTAX OF do while To print first 10 odd numbers.
{
int a=1;
do
{
printf (" % d", a);
a +=2;
{
while ( a < = 19);
}
• for loop statement :
The for loop is an ideal looping statement when we know how many times the loop will be executed.
Syntax⇒
for (initialization; condition; counter)
{
statements
}
Here,
• Initialization is generally an assignment which is used to set the loop control variable, e.g., a= 0.
• Condition is always tells the limit of the loop or determines when to exit the loop. e.g., a< 10
• Counter defines how the loop control variable will change each time the loop is repeated. This may be incremented or decremented.
e.g., a++, a--.
Example : To print your name 10 times.
# include < stdio.h >
#include< conio.h >
void main()
{
int i ; char name [20];
clrscr ( );
printf ("Enter your name");
gets (name);
for(i= l;i<=l0; i++)
{
puts (name);
}
getch ( );
}