Lecture 4 - Format Specifiers and Escape Sequences
Format specifiers
Format
specifiers are the special characters which govern the formatting of the
variables on the screen.
Format Specifier
|
For (Data Type)
|
Format Specifier
|
For (Data Type)
|
%d
|
Signed
Decimal Integer
|
%s
|
String
|
%f
|
Floating
Point (decimal notation)
|
%x
|
Unsigned
Hexadecimal Integer
|
%c
|
Character
|
%o
|
Unsigned
Octal Integer
|
%e
|
Floating
Point (exponential notation)
|
l
|
Prefix,
e.g. %ld for long integer
|
%u
|
Un-signed
Decimal Integer
|
/* This program
demonstrates:-
Declaration of
Constants and Variables
Use of
Constant
Variables
Variable initialization
Format Specifier
Expression
Function call */
# include
<stdio.h>
Output
Area of Circle = 78.54
|
#define PI 3.1416 // Constant
void main()
{
int r; //
variable declaration
r=5; //
variable initialization
float area=0.0; //
variable declaration and initialization
clrscr(); //
clears the screen, for your program’s output.
area = PI * r * r;
printf(“Area of
Circle = %f”, area); //
function call to printf, with 2 parameters
getch();
}
/* This program
demonstrates:-
Declaration of
Constants
Variables
Use of
Constant
Variables
Variable initialization
2- Format Specifiers
Expression
Function call
*/
# include
<stdio.h>
Output
Radius = 5
Area of Circle = 78.54
|
#define PI 3.1416 // Constant
void main()
{
int r; //
variable declaration
r=5; //
variable initialization
float area=0.0; //
variable declaration and initialization
clrscr(); //
clears the screen, for your program’s output.
area = PI * r * r;
printf(“Radius = %d Area of Circle = %f”, r, area);
getch();
}
In
above example, the placement of the variable is very important. The first
variable will be displayed on the first format specifiers position and the next
on the next position and so on.
Escape sequence
These
are the commands for the compiler, which are embedded in a formatted string.
Normally, these commands are for aesthetics only, i.e. for formatted output.
Escape Sequence
|
Used for
|
Escape Sequence
|
Used For
|
\n
|
New Line
|
\f
|
From Feed
|
\t
|
Tab Space
|
\’
|
For printing single
quote
|
\b
|
Backspace
|
\”
|
For printing double
quote
|
\a
|
Alarm (Beep from PC
Speaker)
|
\\
|
For printing backslash
|
\r
|
Carriage Return
|
// This program
demonstrates the use of printf function.
Output
My Name is Hammad Naqvi
|
# include
<conio.h>
void main()
{
clrscr(); //
clears the screen, for your program’s output.
printf(“My Name is
Hammad Naqvi”); //
function call to printf
getch();
}
Steps for getting above program to work
Step
1 – Go to C:\TC\BIN
Step
2 – Double Click TC.EXE
Step
3 – Type above program on as-is basis
Step
4 – Press Ctrl + F9 keys, they will compile and run your program
You
will see My Name is Hammad Naqvi on
the screen
Remember
whatever you write in printf within
double quotes. It will be displayed as-it-is on the screen, except format specifiers
and escape sequences.
Consider
a scenario where you want to write like
My Name is Hammad Naqvi
I am a Lecturer in Computer Science at APS&C
Now we can achieve above output in two ways
1. By
using multiple (2) printf
2. By
using escape sequence
Using escape sequence
// This program
demonstrates the use of escape sequence.
Output
My Name is Hammad Naqvi
I am a Lecturer in Computer Science at APS&C
|
# include
<conio.h>
void main()
{
clrscr();
printf(“My Name is
Hammad Naqvi\nI am a Lecturer in Computer Science at APS&C”);
getch();
}
// This program
demonstrates the use of escape sequence.
Output
My Name is Hammad
Naqvi
|
# include
<conio.h>
void main()
{
clrscr();
printf(“My Name is \tHammad
Naqvi”);
getch();
}
// This program
demonstrates the use of escape sequence.
Output
Hammad Naqvi
|
# include <conio.h>
void main()
{
clrscr();
printf(“My Name is \rHammad
Naqvi”);
getch();
}
// This program
demonstrates the use of escape sequence.
Output
My Name iHammad Naqvi
|
# include
<conio.h>
void main()
{
clrscr();
printf(“My Name is\bHammad
Naqvi”);
getch();
}
Input from the user
So
far we have not written any interactive program. We shall use scanf
for taking input from the user. Let’s do that. For startup let’s just modify
example 2
Output
Please
enter radius of the circle : 5
Radius =
5 Area of Circle = 78.54
|
# include
<conio.h>
#define PI 3.1416 // Constant
void main()
{
int r; //
variable declaration
float area=0.0; //
variable declaration and initialization
clrscr(); //
clears the screen, for your program’s output.
printf(“\nPlease enter the radius of the circle :”);
scanf(“%d”, &r);
area = PI * r * r;
printf(“\nRadius =
%d Area of Circle = %f”, r, area);
getch();
}
Output
Please
enter the length and width of rectangle: 5, 5
Area of
the Rectangle = 25
|
# include
<conio.h>
void main()
{
float length, width,
area; //
variable declaration
clrscr(); //
clears the screen, for your program’s output.
printf(“\nPlease enter
the length and width of the rectangle:”);
scanf(“%f”,” “%f”,
&length, &width); // input in
multiple variable
area = length *
width;
printf(“\nArea of the
Rectangle = %f”, area);
getch();
}
Comments
Post a Comment