Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

17.Formatting the output using printf(), floating point numbers,Escape sequences

 

Formatting the output using printf():

printf() is the function used to print the formatted output on the console output device (Monitor).

 

Formatting integer: (%wd)

‘w’ is the width in which number is printed.

If ‘w’ is the positive number then the number is right aligned.

If ‘w’ is the negative number then the number is left aligned.

If we specify 0 before w then leading 0s are added.

              

Example:


#include<stdio.h>
void main()
{
int x=2453;
clrscr();
printf(“%10d”,x);
printf(“\n%8d”,x);
printf(“\n%08d”,x);
printf(“\n%-10d”,x);
getch();
}

Output:

 

 

 

 


Formatting floating point numbers: (%w.pf)

Here ‘w’ specifies the width in which number is printed.

‘p’ is the number of decimal places to which number is rounded.

If ‘w’ is the positive number, then the number is right aligned.

If ‘w’ is the negative number, then the number is left aligned.

 


#include<stdio.h>
void main()
{
float x=75.256785;
clrscr();
printf(“%10.3f”,x);
printf(“\n%8.2f”,x);
printf(“\n%08.3f”,x);
printf(“\n%-10.2f”,x);
printf(“\n%*.*f”,8,2,x);          /* first * is replaced with 8, second * is replaced with 2*/
getch();
} 

 

Output:

 

 

 

 

 

 

 


Escape sequences: (Back slash characters)

There are some characters prefixed with a back slash (\) used to format the output.

Every back slash character is considered as a single character constant, has its ASCII value.

C language supports the following back slash characters.

 

Escape Sequence

ASCII

Purpose

\n

10

Next line character.

\t

9

Horizontal tag

\a

7

Produces the beep sound

\f

12

Form feed

\r

13

Sends the cursor to the first column of the same line

\\

92

To print \ (Back slash)

\”

34

To print “

\b

8

Back space character

\v

11

Vertical tab

 

\n:

It is called next line character used to send the control to the next line on the screen.

 

Example:


#include<stdio.h>
void main()
{
clrscr();
printf(“Hello\nworld”);
printf(“\n\n”);
printf(“C made easy”);
getch();
}

Output:

Hello

World

 

C made easy

 

\t:

It is the horizontal tab character prints 8 spaces horizontally.

 

Example :


#include<stdio.h>
void main()
{
clrscr();
printf(“Hello\tWorld”);
getch();
}

Output:

Hello        World

 

Example :


#include<stdio.h>
void main()
{
clrscr();
printf(“\n*\n*\t*\n*\t*\t*”);
getch();
}


Output:

*

*          *

*          *          *

 

\b:

It is a back space character sends the output control one character back in the same line.

 

Example:


#include<stdio.h>
void main()
{
clrscr();
printf(“Hello world\b\b”);
getch();
}

Output:

Hello world

 

Example explained:

The first \b sends the control to ‘d’, second \b sends the control to ‘l’. Now the control is on ‘l’.

 

Example:



#include<stdio.h>
void main()
{
clrscr();
printf(“Hello World\b\b\b\b\bComputer”);
getch();
}

Output:

Hello Computer

 

Example explained:

Five \b s send the control to ‘W’, Computer is overwritten on to the World.

 

\a:

It is an escape sequence produces the <beep> sound. It is generally used to get the user attention while displaying the error messages or  warnings.


 

Example:


#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\aEnter the first number:”);
scanf(“%d”,&a);
printf(“\aEnter the second number:”);
scanf(“%d”,&b);
c=a+b;
printf(“\aSum of two numbers %d”,c);
getch();
}

Example explained:

Produces beep sound for every input and output.

 

Note:

Make sure that, a speaker is provided on the mother board.

 

\r:

 Sends the cursor to the first column of the same line

 

Example :


#include<stdio.h>
void main()
{
clrscr();
printf(“Hello World\rComputer”);
getch();
}

Output:

Computerrld

 

Example explained:

\r sends the control to the first column of the same line (‘H’ of Hello world). “Computer” is overwritten on ‘Hello Wo’. Hence the output is “Computerrld”.

\\:

It prints a back slash (\).

 

Example :

#include<stdio.h>
void main()
{
clrscr();
printf(“nice\niit”);
getch();
}

Output:

nice

iit

 

Example explained:

The output is not as we expected, because ‘\n’ worked like a next line character.

 

Example rectified:


#include<stdio.h>
void main()
{
clrscr();
printf(“nice\\niit”);
getch();
}

Output:

nice\niit

 

\”:

It is to print “(Double quotation).

 

Example:


#include<stdio.h>
void main()
{
clrscr();
printf(“ “nice” is nice “);
getch();
}

Output:

Error: Function call missing ) in function main

 

Example explained:

A printf statement must not have more than two “(Double quotes).

 

Example rectified:


#include<stdio.h>
void main()
{
clrscr();
printf(“ \”nice\” is nice”);
getch();
}

Output:

“nice” is nice

 

\v: (Not for Turbo C)

It is vertical tab skips 8 lines next to the current line.

Note: It may not work in all environments.

 
#include<stdio.h>
void main();
{
printf(“Hello”);
printf(“\v”);
printf(“World”);
}


Output:

Hello

 

 

 

World

 

\f:

It is called form feed character. It sends the control to the first line of next page. It is equal to page break.

It only works when printing on paper using a C program.

 

0 comments:

Post a Comment