Using Formatted I/O (Input-Output) Functions With Example in C

Introduction To C

The C Programming Language is also called the Mother of languages. The C language was developed by Dennis Ritchie between 1969 and 1973 and is a second and third generation of languages. The C language provides both low and high level features it provides both the power of low-level languages and the flexibility and simplicity of high-level languages.

C provides standard functions scanf() and printf(), for performing formatted input and output .These functions accept, as parameters, a format specification string and a list of variables.

The format specification string is a character string that specifies the data type of each variable to be input or output and the size or width of the input and output.

Now to discuss formatted output in functions.

Formatted Output

The function printf() is used for formatted output to standard output based on a format specification. The format specification string, along with the data to be output, are the parameters to the printf() function.

Syntax:

printf (format, data1, data2,……..);

In this syntax format is the format specification string. This string contains, for each variable to be output, a specification beginning with the symbol % followed by a character called the conversion character.

Example:

printf (“%c”, data1);

The character specified after % is called a conversion character because it allows one data type to be converted to another type and printed.

See the following table conversion character and their meanings.

Conversion Character Meaning
d The data is converted to decimal (integer)
c The data is taken as a character.
s The data is a string and character from the string , are printed until a NULL, character is reached.
f The data is output as float or double with a default Precision 6.
Symbols Meaning
\n For new line (linefeed return)
\t For tab space (equivalent of 8 spaces)

Example

printf (“%c\n”,data1);

The format specification string may also have text.

Example

printf (“Character is:”%c\n”, data1);

The text "Character is:" is printed out along with the value of data1.

Example with program
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. Main()  
  4. {  
  5. Char alphabh="A";  
  6. int number1= 55;  
  7. float number2=22.34;  
  8. printf(“char= %c\n”,alphabh);  
  9. printf(“int= %d\n”,number1);  
  10. printf(“float= %f\n”,number2);  
  11. getch();  
  12. clrscr();  
  13. retrun 0;  
  14. }  
  15. Output Here…  
  16. char =A  
  17. int= 55  
  18. flaot=22.340000  
See the following image:

c program

What is the output of the statement?

printf(“Integer is: %d; Alphabet is:%c\n”,number1, alpha);

Where number1 contains 44 and alpha contains "Krishna Singh".

Give the answer below.

Between the character % and the conversion character, there may be:
  • A minus sign: Denoting left adjustment of the data.

  • A digit: Specifying the minimum width in which the data is to be output, if the data has a larger number of characters then the specified width occupied by the output is larger. If the data consists of fewer characters then the specified width, it is padded to the right or to the left (if minus sign is not specified) with blanks. If the digit is prefixed with a zero, the padding is done with zeros instead of blanks.

  • A period: Separating the width from the next digit.

  • A digit following the period: specifying the precision (number of decimal places for numeric data) or the maximum number of characters to be output.

  • Letter 1: To indicate that the data item is a long integer and not an int.

    Format specification string Data Output
    |%2d| 9 |9|
    |%2d| 123 |123|
    |%03d| 9 |009|
    |%-2d| 7 |7|
    |%5.3d| 2 |002|
    |%3.1d| 15 |15|
    |%3.5d| 15 |0015|
    |%5s| “Output sting” |Output string|
    |%15s| “Output sting” |Output string|
    |%-15s| “Output sting” |Output string|
    |%15.5s| “Output sting” |Output string|
    |%.5s| “Output sting” |Output|
    |%15.5s| “Output sting” |Output|
    |%f| 87.65 |87.650000|
    |%.4.1s| 87.65 |87.71|
Example based on the conversion character:
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. Int num=65;  
  6. printf(“Value of num is : %d\n:, num);  
  7. printf(“Character equivalent of %d is %c\n”, num , num);  
  8. getch();  
  9. clrscr();  
  10. rerurn o;  
  11. }  
Output Here…
char =A
int= 55
flaot=22.340000

character conversion in c programming

Formatted Input

The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf().

Syntax

scanf (format, num1, num2,……);
The function scnaf() reads and converts characters from the standards input depending on the format specification string and stores the input in memory locations represented by the other arguments (num1, num2,….).

For Example:

scanf(“ %c %d”,&Name, &Roll No);

Note: the data names are listed as &Name and &Roll No instead of Name and Roll No respectively. This is how data names are specified in a scnaf() function. In case of string type data names, the data name is not preceded by the character &.

Example with program

Write a function to accept and display the element number and the weight of a proton. The element number is an integer and weight is fractional.

Solve here:
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. Int e_num;  
  6. Float e_wt;  
  7. printf (“Enter the Element No. and Weight of a Proton\n”);  
  8. scanf (“%d %f”,&e_num, &e_wt);  
  9. printf (“The Element No.is:”,e_num);  
  10. printf (“The Weight of a Proton is: %f\n”, e_wt);  
  11. getch();  
  12. return 0;  
  13. }  
function in c programming

So friends, these are a few articles that will help you in "Using Formatted I/O (Input-Output) Functions" and examples with the C language and output screenshots are also in this article. Best convenience for you. Next time I will provide some more interesting commands, thank you. I Hope this is helpful for you. Enjoy :).KR…

Up Next
    Ebook Download
    View all
    Learn
    View all