You are on page 1of 4

INPUT OUTPUT FUNCTIONS IN C

-->Unformatted Input and Output functions


getchar()
putchar()
getch()
getche()
putch()
gets()
puts()
-->Formatted Input and Output functions
printf() - output function
scanf() - input function
--------------------------------------------------------
FORMAT SPECIFIERS / PLACE HOLDERS / CONTROL STRINGS:
Format specifiers define the type of data to be printed on standard output screen.
we need to use format specifiers whether we are printing formatted output with
printf() or reading or accepting input with scanf() function.

%d %i --> integer value


%ld --> long int value
%lld --> long long int value
%f %g %e %E --> float value
%lf --> double or long double value
%c --> char value
%s --> string value
%o -->octal
%x --> hexa decimal
%p --> pointer
%u --> unsigned decimal
%% --> prints % sign

output function:
printf():
--> this is an output function which is used to display output on to the console.
-->this function is available in <stdio.h> header file.
-->this function can take any number of arguments. but first argument should be
string and then next we can give any type.
syntax:
printf("string type",var args);
--> In printf() what ever will write in double quotes that is printed as usual on
to the output screen.
Ex: printf("welcome"); --> welcome
int a=10;
printf("a=%d",a); -->a=10
printf("%d hello %d"); --> gv hello gv
printf("%dhello%d",10,20,30); -->10hello20
printf("%d %d",100); --> 100 gv
printf("%d,%d,%d",10,20,30); -->10,20,30
printf("%d,%d,%d"10,20,30); -->error

printf("%d%d",100,200); -->100200
printf("%d\t%d",100,200); -->100 200
printf("%d\n%d",100,200); -->
100
200
printf("%d",2,34,456); -->2
printf("%d hello students %d",10,20) -->error
printf("20+30=%d",20+30); -->20+30=50
printf("%d*%d=%d",4,6,4*6); -->4*6=24
printf("%d %d %d",10,20); --> 10 20 gv
printf("%d %d",100,200,300); --> 100 200
printf("salary=%d",50,000); -->salary=50
printf("salary=%d",50000); -->salary=gv(-32768 to +32767)
printf("salary=%ld",50000); -->salary=50000(-2147483648 to +2147483647)
printf("salary=%d",5000); -->salary=5000
printf("salary=%d"); -->salary=gv
printf("%d %d",3>5,1!=3>5); -->(false=0,true=1)
1!=3>5
1!=0
true
1
o/p: 0 1
float b=12.5;
printf("b=%f",b);
char ch='B'; //character variable 1 byte
printf("ch=%c",ch);
char name[10]="bindu"; //string or character array
name[0] name[1] name[2] name[3] name[4] name[5] name[6] name[7]........
b i n d u \0
printf("name=%s",name);
----------------------------------------------
void main()
{
a=10;
printf("a=%d",a);
}
o/p: error undefined symbol a
C is statically typed language.
--------------------------------------------
int a
int a;
inta;
int a b c;
int a,b,c;
int abc,d;
int 1a,1b;
int a1,b1,c1;
int if;
int If;
int IF;
int total-sal;
int total_sal;
int _;

-------------------------------------------------------
INPUT FUNCTION:
scanf() function:
this function is used to read data through the keyboard at runtime.
this is present in <stdio.h> header file
syntax:
scanf("control string", address1, address2...);
Ex: int a;
scanf("%d",&a);
& - address operator.

float b;
scanf("%f",&b);
char ch; //character variable which occupies 1 byte of memory.
scanf(" %c",&ch);

void main()
{
int a;
float b;
char ch;
printf("enter an integer value:");
scanf("%d",&a);
printf("\nenter a float value:");
scanf("%f",&b);
printf("\nenter a character value:");
scanf(" %c",&ch);
printf("\na=%d\nb=%f\nch=%c",a,b,ch);
}
----------------------------------------------------
reading strings in C:
1st way:
char city[20];//string or character array which occupies 20bytes bcz we gave
20 as the size
scanf("%s",city);
this will reads only single word string.
2nd way:
char city[20];
gets(city);
this will read multiple word strings
3rd way:
char city[20];
scanf("%[^:\n]s",city);
this will read multiple word strings until the user presses enter key
4th way:
char str[100];
scanf("%[^:~]s",str);
this will read multi line characters until the user presses '~' symbol.
5th way:
char city[20];
scanf("%10c",city);
input: hyderabad (it will be stored in 0 index to 8 index)
city[9] = '\0';
6th way:
char s1[10];
scanf("%[a-z]s",s1);
input1: abcd123efg
output: abcd
input2: 123asd
output: no o/p

GETCHAR():
this function is used to read an alpha numeric character.
ex:
char ch;
scanf("%c",&ch);
(or)
ch = getchar();
PUTCHAR():
this function is used to print an alpha numeric character.
Ex: char ch;
ch = getchar(); //h
//to print
printf("%c",ch);
(or)
putchar(ch);
GETS():
this function is used to read a multiple word string until the user presses
enter key.
Ex: char name[30];
gets(name); //ramesh kumar
PUTS():
this function is used to print a string on to the console.
Ex: char name[30];
gets(name); // bindu reddy
puts(name);
(or)
printf("%s",name);
GETCH():
this function is used to read a character but the read character will not be
displayed on to the screen.
Ex: char ch;
ch = getch();
GETCHE():
this function is used to read a character but the read character will be
displayed on to the screen.
Ex: char ch;
ch = getche();
PUTCH():
this function is used to print a character on to the screen.
Ex:
char ch;
ch = getch();
putch(ch);

You might also like