Basic Input/Output |
|
Console I/O function |
|
Console I/O refers to the operation that occur at the keyboard and the screen of your computer. |
|
Console I/O function can also be classified in two parts: |
|
1. Formatted console I/O: |
|
Formatted I/O function accepts or present in a particular format. The example of formatted console I/O function is printf( ) and scanf( ). |
|
printf |
|
It is highly desirable that the output are presented in such a way that they are understandable and are in a form easy to use. |
|
The printf() statement provides certain features through which the screen output is effectively controlled. |
The general form of printf() function is: |
|
printf("Control String ",arg1,arg2....); |
|
Control string may contain: |
|
1. character that are simply printed as they are. |
|
2. Conversion specification that begin with a sign. |
|
3. Escape sequence that begin with \ sign. |
|
Given below is a list of conversion charcter that can be used with printf() function: |
|
 |
|
scanf |
|
scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read. |
|
However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. |
|
Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &. |
|
Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents. |
|
2. Unformatted console I/O: |
|
This function cannot control the format of reading and writing the data. some of the example of this function are getch( ),getche( ),getchar( ),gets( ),putchar( ),putch( )andputs( ). |
|
getchar |
|
getchar returns the next character of keyboard input as an int. |
|
If there is an error then EOF (end of file) is returned instead. It is therefore useful to compare this value against EOF before using it. |
|
If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly. |
|
As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d. |
|
 |
|
Output of the program: |
|
 |
|
putchar |
|
putchar puts its character argument on the standard output (usually the screen). |
|
The following example program converts any typed input into capital letters. |
|
To do this it applies the function toupper from the character conversion library ctype.h to each character in turn. |
|
An example program using putchar. |
|
 |
|
Output of the program: |
|
 |
|
gets |
|
gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. When allinput is finished, NULL as defined in stdio.h is returned |
|
puts |
|
puts writes a string to the output, and follows it with a newline character. |
|
An example program which uses gets and puts to double space typed input |
|
 |
|
Out put of the program |
|
 |
|
|
Note: putchar, printf and puts can be freely used together. So can getchar, scanf andgets. |
|
|
|
|
Basic Input/Output |
|
Disc & Port I/O function |
|
Function which performs secondary storage devices like floppy disk or hard disk is calleddisk I/O functions. |
|
|
Port I/O functions
|
|
This type of functions performs I/O operation among various codes like printer port,mouse port. |
|
|
|
|
|
|
|
Control Structures |
|
Sequence control statements |
|
This type of statements insured that the instruction in a program is executed in the same order. In which they appear in the program. |
|
An example of sequential Control Statements: |
|
 |
|
Out put of the program |
|
 |
|
Explanation of the program: |
|
There are three variable a, b, c is declared as int datatype the value of a=2, b=3 and the result of the sum is stored in the c variable which is 5. |
|
|
|
|
Control Structures |
|
Decision control & conditional statements |
|
Decision control statements and Conditional Statements allow the computer to take decision. And force to work under given condition: |
|
Decision control statements: |
|
It gives the control to the computer for taking the decisions. |
|
Two decisions control instruction which are implemented in C are following: |
|
a) The if statements. |
|
b) The if-else statements. |
|
General form of if statements is: |
if (condition is true) execute this statement; |
|
A structure image of if condition > |
|
 |
|
Here we can add else statement also as follow: |
|
else |
printf("you have enter number greater than 50"); |
} |
|
An example of if-else statement: |
|
 |
|
Out put of the program: |
|
 |
|
Explanation of the program: |
|
In the above given program you will be asked to enter any no which is less than 50. |
|
Suppose you enter 47 then you will get output, you have entered no. less than 50 value. |
|
Otherwise you have entered number greater than 50. |
|
A structure image of if else condition > |
|
 |
|
|
|
|
Control Structures |
|
Case control statements |
|
The Case control statements allow the computer to take decision as to be which statements are to be executed next. It is a multi way decision construct facilitate number of alternatives. |
|
C has multi way decision statement known as switch statements. It tests the value of a given variable or expression against a list of case value and when a match found a block of statement associated with the case is executed. |
|
Syntax of switch statement: |
|
switch(expression) |
{ |
case constant_1: |
statements; |
case constant_2: |
statements; |
case constant_n: |
statements; |
default: |
statements; |
} |
|
Explanation: |
|
First in expression parentheses we give the condition. |
This condition checks to match, one by one with case constant. |
If value match then its statement will be executed. Otherwise the default statement will appear. |
|
A structure image of switch statements> |
|
 |
|
An example program to check whether a given number is even or odd, using switch statements: |
|
 |
|
Out put of the program |
|
 |
|
Explanation of the program: |
|
First you will be asked to enter any number |
Suppose you enter 7 |
|
Now the given condition check, so the modulus is 0 or not. (% operator is use to find out the remainder) |
|
if the modulus is 0 then ch=1 otherwise 2, statement will be execute. |
|
|
|
|
Control Structures |
|
Repitition & loop control statements |
|
This type of statements helps the computer to execute a group of statements repeatedly. |
|
This allows a set of instruction to be performed until a certain condition is reached. |
|
There are three types of loops in C: |
|
1. for loop |
2. while loop |
3. do-while loop |
|
The for loop |
|
There are three parts of for loop: |
|
a)counter initialization. |
b)check condition |
c)modification of counter. |
|
Syntax: |
for (variable initialize; check condition; modify counter) |
|
{ |
statements 1; |
-----------; |
-----------; |
statements n; |
} |
|
Explanation: |
|
1. The initialization is usually an assignment that is used to set the loop control variable. |
|
2. The condition is a relational expression that determines when the loop will exit. |
|
3. The modify counter defines how loop control variables will change each time the loop is repeated. |
|
These three sections are separated by semicolon (;). |
|
The for loop is executed as long as the condition is true. When, the condition becomes false the programe execution will resume on the statement following the block. |
|
Advantage of for loop over other loops: |
|
All three parts of for loop (i.e. counter initialization, check condition, modification of counter) are implemented on a single line. |
|
A structure image of for loop > |
|
 |
|
An example program to print a message 5 times using for loop: |
|
 |
|
Out put of the program |
|
 |
|
Explanation of the program: |
|
The o/p will be in the loop 1 time, 2times till 5 times. |
|
Something more about for loop: |
|
1. for (p=1,n=2;n<17;n++):- we can assign multiple variable together in for loop. |
|
2. for (n=1,m=50;n<=m;n=n+1,m=m-1):-The increment section may also have more than one part as given. |
|
3. for (i=1,sum=0;i<20&&sum<100;++i):-The test condition may have any compound relation as given. |
|
4. for (x=(m+n)/2;x>0;x=x/2):-It is also permissible to use expressions in the assignment statements of initialization and increment section as given. |
|
5. for (;m!=100;):-we can omitted the initialization and increment section to set up time delay. |
|
while loop |
|
It is a primitive type looping control because it repeats the loop a fixed no. of time. It is also called entry controlled loop statements. |
|
Syntax: |
|
while (test_condition) |
{ |
body of loop |
} |
|
Explanation: |
|
The test condition is evaluated if the condition is true, the body of loop will be executed. |
|
A structure image of while > |
|
 |
|
An example of program to print the number 1 to 10 using while loop: |
|
 |
|
Out put of the program |
|
 |
|
The do-while loop |
|
The minor Difference between the working of while and do-while loop is the place where the condition is tested. |
|
The while tests the condition before executing any of the statements within the while loop |
|
As against this, the do-while loop tests the condition after having executed the statement within the loop. |
|
syntax: |
|
do |
{ |
body of loop; |
} |
while (test condition); |
|
A structure image of do-while loop > |
|
 |
|
Explanation: |
|
It first executes the body of the loop, and then evaluates the test condition. If the condition is true, the body of loop will executed again and again until the condition becomes false. |
|
Example of program using do-while loop: |
|
 |
|
Out put of the program |
|
 |
|
Explanation of the program: |
|
First it will print "hello there" then it will go for condition statements. |
|
|
|
|
Control Structures |
|
Some more statements |
|
The break Statement |
|
We have already met break in the discussion of the switch statement. It is used to exit from a loop or a switch, passing control to the first statement beyond the loop or a switch. |
|
With loops, break can be used to force an early exit from the loop, or to implement a loopwith a test to exit in the middle of the loop body. |
|
A break within a loop should always be protected within an if statement which provides the test to control the exit condition. |
|
An Example program to determine whether a number is prime or not: |
|
 |
|
Out put of the program |
|
 |
|
The continue Statement |
|
This is similar to break but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement. |
|
In a while loop, jump to the test statement. |
|
In a do while loop, jump to the test statement. |
|
In a for loop, jump to the test, and perform the iteration (looping). |
|
Like a break, continue should be protected by an if statement. You are unlikely to use it very often. |
|
An example of program using continue statement: |
|
|
 |
|
Out put of the program |
|
 |
|
The goto Statement |
|
C has a goto statement which permits unstructured jumps to be made. It requires a label in order to identify the place where the branch is to be made. |
|
A label is any valid variable name,and must be followed by a colon(:). |
|
Syntax: |
|
goto label; |
.................. |
label: |
statement; |
|
The label can be any where in the program either before or after the goto label; statement. |
|
An example program using goto statement: |
|
 |
|
Out put of the program |
|
 |
|
Explanation of the program |
|
Here the program will ask untill you give negative value at the time when you give positive value then instantly it will show you the square root value of the given number. |
|
Another use of the goto statement is to transfer the control out of a loop (or nested loops) when certain particular conditions are encountered. |
|
Note: We should try to avoid using goto as far as possible because, it is not good for readability of the program or to improve the program execution speed. |
|
The exit() function |
|
This function is used for terminating the execution of C program. |
|
Syntax: |
|
exit(int status); |
|
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment