Formatted console i/o functions

Formatted console i/o functions


Functions printf() and scanf() are come under the formatted console i/o functions. These functions allow us to input read from the keyboard or the output display on the screen in a formatted way as per our requirements

We can control how many floating points are there in output, how many spaces would be present between the two values, even we can decide where the space should be on the left side or right side and the number of places after the decimal points using formatted functions.

Let us discuss these functions one by one first of all we have gone to talk about the printf() function, we have habituated to use of it. but without the introduction of it formally.

The general form of printf() looks like

printf("control string", list of variables);

The control string can contain three things.

  1. Characters that printed (on-screen) without any change.

  2. Escape sequences which start from character backslash \ sign

  3. Conversion or format specifications that begin with % sign followed by format specifier.


For example

#include<stdio.h>


int main() {


int num1, num2, total;


num1=12;


num2=10;


total=num1 + num2;


printf("This program does total of two numbers \n");


printf("number are %d and %d \n",num1,num2);


printf("Total is %d", total);


return 0;


}


Output

This program does a total of two numbers

number are 12 and 10

Total is 22

Now in the above program when the first printf is executed it printed all characters on screen without any change. When it met escape sequence it takes appropriate action in our case it is '\n' when it met it the cursor is beginning of the new line

In second print executed it prints the characters when the format specifier met it picked up the value from variables by their order that we specified on the right side of the string as arguments.

Now in c the functions calling convention is from right to left as we know the printf() is the function so the value of format specifies are comes from the right to left, in our example first num2's value is placed on the string then the value of num1. we use %d as format specifies in the string because the variable type is integer.

Now you can understand the third printf in your program.

Escape sequences

In c programming language, there are 256 numbers of character in character set they mainly divided into 2 parts

  1. ASCII characters set

  2. Extended ASCII characters set.


Apart from that, some other characters are also there which are not part of any character set, they known as Escape characters

We saw that the newline character \n in formatting string takes the cursor to the beginning of the next line. Here "\" symbol is considered as escape character so-called them Escape sequences

Examples of escape sequences







































\nNewline
\tTab (Horizontal)
\bBackspace
\aalert (Beep)
\vvertical tab
\'Single quote
\"Double quote
\\backslash
\fForm feed,

\t moves the cursor to the next tab stop.

\r moves the cursor to the beginning of the line in which it is currently placed.

\a alerts the user by sounding the speakers.

\b moves the cursor one position to the left of its current position.

\f (form feed) advances the computer stationery attached to the printer to the top of the next page

Example

#include<stdio.h>

int main()

{

print("here we go! 3\a 2\a 1\a");


printf("\n This is escape sequences");


printf("\n \" back\bspace\" ");


return 0;


}

Output:

here we go! 3 2 1

This is escape sequences

"backspace"

Format specifications

the %d we used earlier to print total and num1 and num2 here %d is called as format specifiers, Followings are format specifiers that can be used with the printf() function.




























DatatypeFormat specifies
Charcharsigned char

unsigned char
%c%c

%c
Integershort intunsigned short int

unsigned int

int

long int

long long int

unsigned long int

unsigned long long int
%hd%hu

%u

%d

%ld

%lld

%lu

%llu
Realfloatdouble

long double
%f%lf

%Lf
String%s

Specifiers in format specifications

Printing integer number:

As we know %d is used as format specifier for print integer number

Now to print integer in formatted way we use %wd

Where w denotes the with of the field in number of character for example %4d is print the variable as a decimal integer in a field of 4 columns. if the value to be printed is not fill up all entire field then the value is align to right and the best blanks fields are padded on the left side.

Now if we assign a minus (-) sign to the format specifier then if the value to be printed is not fill up all entire field then the value align to left and rest blank space align to right.

Example:

#include<stdio.h>

int main()

{

int n=1234;


printf("123456789123456789123456789\n");


printf("Number is %d printed now \n", n);


printf("123456789123456789123456789\n");


printf("Number is %2d printed now \n", n);


printf("123456789123456789123456789\n");


printf("Number is %4d printed now \n", n);


printf("123456789123456789123456789\n");


printf("Number is %6d printed now \n", n);


printf("123456789123456789123456789\n");


printf("Number is %6d printed now \n", n);


printf("123456789123456789123456789\n");


printf("Number is %5d printed now \n", n);


return 0;


}

Output:

123456789123456789123456789

Number is 1234 printed Now

123456789123456789123456789

Number is 1234 printed Now

123456789123456789123456789

Number is  1234 printed Now

123456789123456789123456789

Number is   1234 printed Now

123456789123456789123456789

Number is   1234 printed Now

123456789123456789123456789

Number is  1234 printed Now

Now point to note is if the field width is less then the number of digits, then it simply takes more space ofter the field and no digits are lost.

Now for printing floating-point number:

The format is simple as an integer but we just provide the number of digits that print after the floating-point

For printing real Date type we use %w

Lets clear this by Example

#include<stdio.h>

int main()

{

printf("%f %f %f, 2.0, 12.5, 144.8);


printf("\n %f %f %f, 909.9, 1100.5,2002.0);


return 0;


}

Output:

2.000000, 12.500000, 144.800000

909.900000, 1100.500000, 2002.000000

The numbers are printed but not in format and property. We can print them a better way by this method

#include<stdio.h>

int main()

{

printf("123456789123456789123456789");


printf("\n %9.1f %9.1f %9.1f", 2.0 , 12.5, 144.8);


printf("\n %9.2f %9.2f %9.2f", 909.9, 1100.5, 2002.0);


return 0;


}

Output:

123456789123456789123456789

2.0  12.5  144.8

909.90  1100.50  2002.00

We can use the same format to print the character it is as same as case of integers.

Now we are going to learn about scanf() function to read Data from keyboard

The format of scanf() is as below.

scanf("Control string", List of variables);

Now in case of scanf function, the control string has format specifiers and as the list of variables it has the address of variables, to get the address of variable we use the address of operator that denoted  by 'f'

Now let's take example to understand the scanf function

#include<stdio.h>

int main()

{

int num1;


float num2;


scanf("%d %f", &num1, &num2);


return 0;


}

Now in the above program, if we provide a value as 12 13.50 than 12 is stored in num1 and 13.50 is stored in num2

As we provide %d format specifier for integer and %f format specifier for the float and here &num1 and &num2 are the memory location of the variable num1 and num2

Now point to remember is the value stored in the variable is only the thing that comes before the first blank or enter key. We can not store "will smith" in scanf variable because the smith is never got stored in variable or string it has blank or space between the will and smith.

whenever we use scanf() to read more than one value; always remember to separate them by blank or enter key when you enter the values.