Basic Input Output in C - Formatted IO
Basic Input Output in C - Formatted IO
TUTORIAL
Chapter
1. Basic Input Output in C - Formatted IO
Topics
1.2 printf()
1.8 scanf()
printf() and scanf() functions also use some escape sequences. These
are special character constants used during printing output to
screen. These are helpful in making the output more readable and
more catchy. The following are the escape sequences defined: -
Code Meaning
\b Backspace (delete the last character from cursor)
\f Form feed
\n New line
\r Carriage return (same as \n except it remains in same
line)
\t Horizontal tab (prints a tab in output)
\" Double quote (prints double quotes)
\' Single quote
\\ Backslash
\v Vertical tab
\a Alert
\? Question mark
\N Octal constant (where N is an octal constant)
\xN Hexadecimal constant (where N is a hexadecimal constant)
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 1/6
8/27/23, 6:49 PM PDF | CodeQuotient
printf()
1 #include<stdio.h> C
2
3 int main()
4 {
5 int a=10;
6 char b='b';
7 char str[100]="Hello and Welcome.";
8 float f=10.4;
9 double d=123456.9876;
10 long int l=123456789;
11 long double ld=28761872.67352;
12 printf("int a=%d, char b=%c",a,b);
13 printf("\nstr=%s",str);
14 printf("\nfloat f=%f, double d=%lf",f,d);
15 printf("\nlong int l=%ld, long double ld=%Lf",l,ld);
16 return 0;
17 }
18
We can also print the number in other base like octal or hexadecimal
by using %o and %x or %X or %p for example: -
1 #include<stdio.h> C
2
3 int main()
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 2/6
8/27/23, 6:49 PM PDF | CodeQuotient
4 {
5 int a=10;
6 int *p=&a;
7 printf("a is in base 10=%d\n in base 8=%o\n in base
16=%x\n in base 16=%X\n Address of a in base
16=%p",a,a,a,a,p);
8 return 0;
9 }
10
a is in base 10=10
in base 8=12
in base 16=a
in base 16=A
in base 16=0xbfd18208
There are also format modifiers for printf() like minimum field width,
number of decimal places, alignment etc. these will be placed
between % and format code. These are as follows: -
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 3/6
8/27/23, 6:49 PM PDF | CodeQuotient
1 #include<stdio.h> C
2
3 int main()
4 {
5 int a=10;
6 float f=1087.46;
7 printf("a=%d \n",a);
8 printf("a with minimum width=%9d \n",a);
9 printf("a with minimum width left=%-9d \n",a);
10 printf("f=%f \n",f);
11 printf("f with decimal fixed=%20.3f \n",f);
12 printf("f with decimal fixed left aligned=%-20.3f
\n",f);
13 return 0;
14 }
15
a=10
a with minimum width= 10
a with minimum width left=10
f=1087.459961
f with decimal fixed= 1087.460
f with decimal fixed left aligned=1087.460
scanf()
It is used to read the data from keyboard in the same way as the
printf() is used for all built-in types. The prototype of scanf() is: -
Code Meaning
%a Reads a floating -point value (C99 only).
%c Reads a single character.
%d Reads a decimal integer.
%i Reads an integer in either decimal, octal, or
hexadecimal format.
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 4/6
8/27/23, 6:49 PM PDF | CodeQuotient
scanf() function will take the address of the variable in which the
value needs to be saved so that the vlue entered from keyboard will
be placed at that location in memory. So the variable name is passed
preceded by a & (address of operator) sign.
The following program will show the use of printf() and scanf()
functions: -
#include <stdio.h>
int main( )
{
char str[100];
int i;
printf( "Enter a integer value :");
scanf("%d", &i);
printf( "Enter a String :");
scanf("%s", str); // as array name is the address already so
no need of & operator
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
codequot
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 5/6
8/27/23, 6:49 PM PDF | CodeQuotient
tient.com
Tutorial by codequotient.com | All rights reserved, CodeQuotient 2023
https://codequotient.com/tutorialpdf/5a01b531cbb2fe34b77750c5 6/6