Formatted Input and Output in C
Formatted input:
Formatted input refers to an input of data that has been
arranged in a a particular format.
Scanf function is used for this purpose.
Syntax : Scanf(“Control string”, argument 1, argument 2, argument3,…., argument n);
The control string specifies the field format in which data
is to be entered and the arguments arg1, arg2,… Specify the address of
locations where the data is stored.
Control string contains filed specifications, which detect the interpretation
of input data. It includes field or format specifications consisting of the
conversion character %, a data type character and an optional number specifying
the field width.
Formatted output :
The printf functions provides features that can be used to
contrl the alignment and spacing of printouts on screen.
Syntax : printf (“Control String”, arg1, arg2, arg 3, ….. , arg n);
Examples of scanf() and printf() functions :
1. Input and output of the name of a website.
#include<stdio.h>
#include<conio.h>
voidmain()
{
char name[20]; // This is a character array and is called
string.
printf(“Enter the name of website :\n”); // output
scanf(“%s”,name); //input
printf(“The name of the website is %s.”, name);
}
Result :
Enter the name of website : www.readforlearning.blogspot.com
(Enter button)
The name of the website is www.readforlearning.blogspot.com
Format codes for printf/scanf functions :
Code
|
Meaning
|
%c
|
Single character
|
%d
|
Read a decimal integer
|
%e
|
Read a floating point value
|
%f
|
Read a floating point value
|
%g
|
Read a floating point value
|
%h
|
Read a short integer
|
%i
|
Read a decimal, hexadecimal or octal integer
|
%o
|
Read an octal integer
|
%s
|
Read a string
|
%u
|
Read an unsigned decimal integer
|
%x
|
Read a hexadecimal integer
|
%[..]
|
Read a string of words
|
Comments
Post a Comment