You are on page 1of 31

Programming In C

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

Character I/O functions:


a)

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():

Is a single character input function like getchar().

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():

This is a single character input function like

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

String I/O functions


gets() : This function is used to read a string of characters from standard input device. When you press enter key it will stop further reading and will terminate with a new line character End of string will be marked with null character(\0). This function reads the spaces as a part of functions. syntax is: gets(str1); where str1 is a string variable. After typing a character the pressing Enter key is not required.
a)

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(); }

Formatted I/O functions


Formatting means representation of data with different settings as per requirements. scanf() function is used for input printf() function is used for output These functions can be used to read or display any type of data like int, char, float etc.

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 or Branching statements


These statements alter the normal Sequentional execution of statements of the program depending upon the test condition to be carried out at a particular point in the program. Decision is taken regarding where the control should transfer, depends upon outcome of test condition.

Decision making 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.

if-else statement Syntax

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.

if-else statement Example


void main() { int age; printf(Enter age : "); scanf("%d", &age; if(age> = 18) { printf(\nEligible for vote); } else { printf(\nNot eligible for vote); } getch(); }

Nesting Of if-else statement


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.

Nested if and if-else statement

Syntax:

If(test condition 1) { If(test condition 2) { Statement1; } else { Statement2; } } else { Statement3; } Statement-x

Nested if-else statement Example


//To find biggest of 3 numbers void main() { int a,b,c; printf(Enter numbers : "); scanf("%d%d%d", &a,&b,&c); if(a > b) { if(a>c) { printf("%d is the biggest\n", a); } else printf("%d is the biggest\n", c); } else { if(b>c) printf("%d is the biggest\n", b); else printf("%d is greatest\n",c); } getch(); }

The Else If Ladder

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

You might also like