Programming in C: by Dhishna Devadas
Programming in C: by Dhishna Devadas
By Dhishna Devadas
Input/Output Functions
C provides a set of build in functions to read the
data from input device and sending them to output device C language has a collection of input output functions that performs input from standard input device and output to a standard output device such functions are known as console I/O functions. For using I/O library functions header file named <stdio.h> must be included in all C programs.
Console I/O functions are of two types:1. Unformatted functions 2. Formatted functions
Unformatted I/O functions: The data accepted or printed with default settings by I/O functions is called unformatted I/O data Unformatted I/O functions are of two types:1. Character I/O functions 2. String I/O functions
getchar() : this function is used to read one character at a time from standard input device. Syntax : Character_variable = getchar(); After typing a character the user has to press Enter key That character is echoed on the screen
Example
#include<stdio.h> #include<conio.h> void main() { char ch; printf(\nEnter any character from keyboard:); ch= getchar(); printf(\nvalue of character is\n%c",ch); getch(); } OUTPUT Enter any character from keyboard: F Value of character is F
getche()
a)
getche():
This function is also used to read one character at a time from standard input device. syntax is: Character_variable = getche(); After typing a character the pressing Enter key is not required The character is also echoed on the screen
Example
#include<stdio.h> main() { char ch; printf(\nEnter any character from keyboard); ch= getche(); } OUTPUT: Enter any character from keyboard f
getch()
a)
getch():
getchar() The function is also used to read one character at a time from standard input device. Syntax is: Character_variable = getch(); After typing a character the pressing Enter key is not required That character is not echoed on the screen
Example
#include<stdio.h> main() { char ch; printf(\nEnter any character from keyboard); ch= getch(); } OUTPUT: Enter any character from keyboard.
putchar()
a)
putchar():
This function is used to display a single character at on time on the screen The character to be printed is passed as an argument to putchar() function. Syntax is: putchar(character_variable);
Example
#include<stdio.h> #include<conio.h> main() { char ch; printf(\nEnter any character from keyboard); ch= getchar(); printf(\nCharacter you have entersed is:\n"); putchar(ch); getch(); } OUTPUT: enter any character from keyboard F Character you have entered is: F
puts()
puts() function:- this function is used to output a string of characters from on the screen Syntax is: puts(str1);Where str1 is a string variable.
EXAMPLE
#include<stdio.h> #include<conio.h> main() { char name[20]; printf(Enter your name"); gets(name); printf(The name you have entered is\n"); puts(name); getch(); }
scanf() function:
This function is used to input data from the user
as per specified format. This function is used to read numeric values, single character or string. It is used to read the mixed type of data from standard input device. Syntax: scanf(control string var1, var2.); Where control string is format specifiers requested for formatting information. Var1, var2, indicates addresses of data items.
printf() function:printf() :
This function is used to output string as well as numbers This function is also used deliver the formatted output of any number of variables. General syntax is: printf(control string ,arg1, arg2.argn); Control string consists of three types of items: Characters that will be printed on the screen. Format specifications that define output format for display of each item Escape sequence characters such as \n. \t. Control string indicates how many arguments follow and what their types are. The arg1, agr2 ,argn are the variables whose values are printed according to specification of the control string. The arguments should match in number in order and type with the format specification.
Control statements:Control statements: the statements used for controlling order of execution in a program are referred as control statements. There are three types of control statements. Decision making or Branching statements. Looping statements. Jumping statements.
Decision making statements supported by C language are: if statement If-else statement Switch statement if statement This is the most simple form of the branching statements. It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. if statements take the following form:
if statement
Syntax if (this condition is true) { block of statements executed; }
Example
void main() { int x=5; if (x > 1) { x=x+10; }
printf("%d", x);
if-else statement
In case of if statement the block is executed only when the condition is true If condition is false nothing is done and the control is transferred to the next to the next statement following the if block If you want to execute specific statements in both the cases then if-else statement is used. The conditional expression in an if statement is a comparison between two values. If the conditional expression is true, then the "true" block of code is executed. If the conditional expression is evaluated as false, then the else block of code is executed.
The if....else statement is an extension of the simple if statement. The general form is
if (this condition is true) { Execute True-block statement(s) } else { Execute False-block statement(s) }
If the condition is true, then the true-block statement(s), immediately following the if statement are executed; otherwise the false-block statement(s) are executed.
When a series of decisions are involved, we have to use more than one if.else statement Nested statement refers to the situation when an if statement occurs within another if statement and/or in the body of else statement.
Syntax:
If(test condition 1) { If(test condition 2) { Statement1; } else { Statement2; } } else { Statement3; } Statement-x
Multi-way decisions arise when number of conditions are checked for executing various statements
Syntax:
If(condition 1) statement -1; else if( condition 2) statement -2; else if( condition 3) Statement-3; else if( condition n) Statement-n; else default-Statement; Statement-x;
Example
#include<stdio.h> void main() { int m1,m2,m3,m4,m5,total,per; char grade; printf(\nEnter the marks in 5 subjects:\n"); scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5); total=m1+m2+m3+m4+m5; per=(total/500)*100; if(per >= 90 && per<=100) grade=A+; else if(per >= 80 && per<90) grade=A; else if(per >= 70 && per<98) grade=B;
else if(per >= 60 && per < 70) grade=C; else if(per >= 50 && per < 60) grade=D; else printf(Fail); printf(Grade=%c,grade); getch(); }
switch statement
switch statement is multi-way decision making statement which select one of the several alternatives based upon the value of integer variable or expression. Syntax