3.InputOutput Functions
3.InputOutput Functions
3.InputOutput Functions
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.
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);
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.
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.
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.
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.
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);
#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
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.
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);
}
#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);
}
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);
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