Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C Programming Lecture Notes

Input and Output Functions


Input and Output Functions:
Some of the built in function are used to read the input from the key board and display the output on the
screen. The following three types of I/O functions are important for writing programs in ‘c’.

Character oriented I/O


1) getchar()
2) putcahr()

Formated I/O functions


1) scanf
2) printf

String I/O functions


1) gets()
2) puts()

getchar() function:- (Single character input)

 The function getchar() is used to read a single character from the key board.
 This function returns the integer value of the character in the machines character code. If the system uses
the ASCII character code, then the ASCII value of the character is returned.
 The general format for using this function is
variable = getchar();

Where the variable may be either int or char data type. The getchar() function does not take any argument.

Putcahr() function:- (Single character output)

 The putchar() function transmits a single character to a standard output device.


 The character being transmitted will normally be represented as a character-type variable.
 It must be expressed as an argument to the function, enclosed in parentheses. following the world
putchar.
The general format for using this function is

Putchar(variable);
Where the variable may be represented by an integer constant or a character constant.
Ex: char c;
C=getchar(); putchar(c); putchar(65);

Scanf() function:- (Entering Input data)

Input data can be entered in to the computer from a standard input device by means of the ‘c’ library
function scanf.
This function can be used to enter any combination of numerical values, single characters
and strings. This function returns the no.of data items that have been entered successfully.
The general format for scanf() function is

Scanf(“control_string”,argument_list);

Where control_string contains the required formatting specifications enclosed with in double quotes and
argument_list contains the address of the memory locations where the input data is stored.

 The addresses are separated by commas.


 The control_string consist of individual groups of characters with one character group for each input data
item.
 Each charater group must begin with a percent sign(%).
 In its simplest form, a single character group will consist of the percent sign, followed by a conversion
character. This indicates the type of the corresponding data item.

Conversion character meaning

VVSV 17
C Programming Lecture Notes

%c Single character
%d integer
%f float
%h short int
%i decimal, hexa decimal,octal
%o octal
%s string
%u unsigned int
%x hexadecimal
%[ ] string, may include white space characters.

 The following points should be kept in mind while using scanf() function.
1) To read the value of a variable belonging to any one of the fundamental data types, the operator &
gives the address of the operator ‘&’. The operator & gives the address of the variable to which it is
applied.
2) The order and the type of conversion specification must match the argument in the argument_list.

Ex: char ch;


Int a;
float b;
scanf(“%d %f %c”,&a,&b,&ch);

Input & Output functions:


( pic remainng)
Unformatted input functions:-

getchar():- This function is used to read a single character from the key board. This function does not
requires any arguments. So, it is followed by empty parentheses.

Syntax:- character_ variable = getchar();


The getchar() requires enter key to be passed after inputting the character. The getchar () echos the
given character on the screen.

Ex:- main()
{
char ch;
ch = getchar();
printf(“ %c”,ch);
}

OUTPUT:
a
a.

getch():- This function is used to read a single character from the key board. But it does not echoes the
given character on the screen and also it does not require enter key to pressed.

Ex: main()
{
char ch;
ch = getch();
printf(“ ch=%c”,ch);
}
OUTPUT
Ch = a

getche():- This function is used to read a single character from standard input device
i.e. keyboard. Like getch() it doesn’t required any enter key to be pressed. Unlike getch() it echoes the given
character on the screen.

Syntax:- Character_ variable = getche();

Ex: main()

VVSV 18
C Programming Lecture Notes

{
char ch;
ch = getche();
printf(“ch=%c”,ch);
}
OUTPUT:
ch=a
ch=a

gets():- The gets() is used to read a string of values (or) no. of characters from the keyboard.

Syntax: gets(string_name);

Here string_name represents previously defined character array.

Ex: Write a program to read name of a person and print it.

#include<stdio.h>
main()
{
char name[20];
printf(“Enter name of a person”);
gets(name);
printf(“name is=%s”,name);
}

output:
Enter the name of a person
Sachin
Name is =sachin

Difference between scanf() and gets():

Scanf() stops reading when it gets a space or when it reads a space, but gets() reads characters including
spaces but it stops reading when its gets a new line characters. Gets() reads new line character and in the
place of new line character it replaces null character.

Unformatted output function:

Putchar(): The putchar() is used to output or to print a single character on the screen.
Syntax: putchar(character_variable);
Putchar(‘character_constant’);

Ex:
#include<stdio.h>
main()
{
putchar(‘a’);
putchar(‘=’); output: a=0
putchar(‘0’);
}

puts():This function is used to output or to print string of values or group of characters on the screen.

Syntax: puts(string_name);
Puts(“string_constant”);

Ex:
#include<stdio.h>
main()
{
char name[20]=”HELLO”; output: HELLO

VVSV 19
C Programming Lecture Notes

puts(name);
}

Difference between printf() function and puts() function.


When we are using printf statements, we have to explicitly specify \n with each statement in order to print
output in multiple lines, but when we use puts() there is no need to specify \n. The puts() after printing a
statement places the cursor to the beginning of the next line.

Formatted input function:

Scanf(): It is used to read values in a given specified format.


Syntax: scanf(“format specifiers”,&arg1,&arg2, &argn);

Format specifiers Datatype


%d int
%u unsigned int
%d or %hd signed short int
%hu unsigned short int
%ld long int
%lu unsigned long int
%f float
%e float value in exponent form
% both fractional form or exponential form
%lf double
%Lf long double
%o octal
%x hexadecimal
%c single character
%s group of characters or string

 #include<stdio.h>
main()
{
int a,b,c;
printf(“Enter 3 numbers:”);
scanf(“%3d%3d%3d”,&a,&b,&c);
printf(“a=%d\n b=%d\n c=%d\n”,a,b,c);
}
output:
123 456 789 12 345 678 1 234 5678
a=123 a=12 a=1
b=456 b=345 b=234
c=789 c=678 c=567

 main()
{
char name[20];
printf(“Enter a name:”);
scanf(“%5s”,&name);
printf(“%s”,name);
}
output:
ANILKUMAR
ANILK
ANIL KUMAR
ANIL

scanf() can also be used to restrict a set of values when we are inputting some characters.

VVSV 20
C Programming Lecture Notes

main()
{
char name[20];
scanf(“%[aeiou]”,name);
printf(“%s”,name);
}
output:
aeiou

main()
{
char name[20];
scanf(“%[a-z A-Z 0-9]”,name);
printf(“%s”,name);
}
output :
All combinations will be printing

main()
{
char name[20];
scanf(“%[^aeiou]”,name);
printf(“%s”,name);
}

putch():- This function is used to write a character word.

main()
{
char ch;
printf(“print any key\n”);
scanf(“ %c”,&ch); (or) ch=getchar();
putch(ch);
}

printf:- The printf is used to print value on the screen in the given format.

Syntax:- printf(“format_specifiers”,argument_list);

Data type field  %d


%f
%c
%s
here %f indicates conversion character,
 for integer mode %d is used.
 for real mode %f, %lf,%Lf.
 for character mode %c or %s.

Integer mode output(%nd):- here ‘n’ indicates in how many digits places the given value to be filled.

main()
{
int a=123;
Printf(“%5d\n”,a);
Printf(“%4d\n”,a);
Printf(“%3d\n”,a);
Printf(“%2d\n”,a);
Printf(“%d\n”,a);
}

float mode output(%n.df):- here ‘n’ indicates in how many no.of digits to be printed after the decimal
point.
main()

VVSV 21
C Programming Lecture Notes

{
float pi=3.141592;
Printf(“%10.6f”,pi);
Printf(“%10.5f”,pi);
Printf(“%10.4f”,pi);
Printf(“%10.3f”,pi);
}

character mode output(%ns):- here ‘n’ indicates in how many digits places.
main()
{
Char name[10]=”rvrjc”;
Printf(“%8s”,name);
Printf(“%9s”,name);
Printf(“%10s”,name);
}

VVSV 22

You might also like