Input and Output Formatting in C Language - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Input and Output Formatting in C Language

Share This

Input formatting using scanf() function


To take input from terminal using scanf function, the function can be used in different forms. Use only first three syntaxes and avoid the last two syntaxes, the last one will not work according to your expectation.

Try the following code and try to understand the differences.
#include <stdio.h> int main(void) { int m, n; printf("\nEnter an integer:\n"); scanf("%d", &m); printf("m = %d", m); printf("\nEnter two integer:\n"); scanf("%d %d", &m, &n); printf("m = %d n = %d ", m, n); printf("\nEnter two integer:\n"); scanf("%d%d", &m, &n); printf("m = %d n = %d ", m, n); printf("\nEnter two integer:\n"); scanf("%d\t%d", &m, &n); printf("m = %d n = %d ", m, n); printf("\nEnter two integer:\n"); scanf("%da%d", &m, &n); printf("m = %d n = %d ", m, n); printf("\n"); return 0; }
Here, we will discuss scanf using another format specifier in the following program. First, take a look at the following program.
#include <stdio.h> int main (void) { int x, y, z, p; printf("Enter three integers:"); scanf("%2d %4d %2d", &x, &y, &z); scanf("%d", &p); printf("%d %d %d %d\n", x, y, z, p); return 0; }
Run the program and try to understand the output as shown below.


At this time, we have entered a 10-digited number. The first two digits are being stored in variable x , next four digits are being stored in y , next two digits in z and rest of the digits are being stored in p .


In this example, first two digits are being stored in x . As there is a space after 123, only 3 is being stored in y . The next digit 4 is being stored in z as there is a space after 4. The last number is being stored in p .


Output formatting using printf() function


First run the following code, you will realize the requirement of format specifiers.

#include <stdio.h> typedef struct stud{ int roll; char name[20]; char dept[10]; } st; int main() { int i; st stds[3]; printf("Enter roll name and department\n"); for(i = 0; i < 3; i++) { scanf("%d", &stds[i].roll); scanf("%s", stds[i].name); scanf("%s", stds[i].dept); } printf("Output: \n"); for(i = 0; i < 3; i++) { printf("%d", stds[i].roll); printf("\t%s", stds[i].name); printf("\t%s", stds[i].dept); printf("\n"); } printf("Output: \n"); for(i = 0; i < 3; i++) { printf("%3d", stds[i].roll); printf("\t%-20s", stds[i].name); printf("\t%3s", stds[i].dept); printf("\n"); } return 0; }
In the fisrt output, we have used tab based separation. As the length of name field varies a lot, the next fields are not aligned vertically. But the users preferrs to get output in tabular form, which is not possible by tab seperator.

The next output shows the result in a table format, which can be done by mentioning the width of the fields to be printed.

Here the code is self-explanatory. Please look into the code.

#include <stdio.h> int main(void) { printf("\n%d", 123456); // here field width is 10 characters and the data will be right aligned. printf("\n%10d", 123456); // here field width is 10 characters and the data will be left aligned. printf("\n%-10d", 123456); // here field width is 8 characters, the data will be right aligned and left side will be padded by zero. printf("\n%08d", 123456); // here field width is 8 characters and the data will be left aligned. printf("\n%-08d", 123456); printf("\n"); return 0; }

#include <stdio.h> int main(void) { // here field width is 10 characters and the data will be right aligned. printf("\n%10.2d", 123456); // here field width is 10 characters and the data will be left aligned. printf("\n%-10.2d", 123456); // here field width is 8 characters, the data will be right aligned and left side will be padded by zero. printf("\n%08.2d", 123456); // here field width is 8 characters and the data will be left aligned. printf("\n%-08.2d", 123456); printf("\n"); return 0; }



#include <stdio.h> int main(void) { // the data will be left aligned as the length is more than 2 digit before decimal point. printf("\n%4.2d", 123456); // the data will be left aligned as the length is more than 2 digit before decimal point. printf("\n%-4.2d", 123456); printf("\n"); return 0; }


#include <stdio.h> int main(void) { // by default for floating point numbers there will be 6 digits after decimal point. // the data will be left aligned. printf("\n%f", 12345.6); // the data will be left aligned. printf("\n%15f", 12345.6); // the data will be left aligned. printf("\n%-15f", 12345.6); // the data will be left aligned. printf("\n%08f", 12345.6); // the data will be left aligned. printf("\n%-08f", 12345.6); printf("\n"); return 0; }


#include <stdio.h> int main(void) { // total length is 10 digits and after decimal point there will be 2 digits. // hence the data will be right aligned. printf("\n%10.2f", 12345.6); // hence the data will be left aligned. printf("\n%-10.2f", 12345.6); // the data will be right aligned and left side will be padded by 0. printf("\n%010.2f", 12345.6); // hence the data will be left aligned. printf("\n%-10.2f", 12345.6); printf("\n"); return 0; }


#include <stdio.h> int main(void) { // data will be left aligned. printf("\n%s", "pneumonoultramicroscopicsilicovolcanoconiosis"); // data will be right aligned. printf("\n%50s", "pneumonoultramicroscopicsilicovolcanoconiosis"); // data will be left aligned. printf("\n%-50s", "pneumonoultramicroscopicsilicovolcanoconiosis"); // data will be right aligned and only 10 characters from left will be printed. printf("\n%50.10s", "pneumonoultramicroscopicsilicovolcanoconiosis"); // data will be left aligned and only 10 characters from leftside will be printed. printf("\n%-50.10s", "pneumonoultramicroscopicsilicovolcanoconiosis"); printf("\n"); return 0; }


#include <stdio.h> int main() { // \b backspace BS printf("\n He\bllo "); // \f form feed FF (also clear screen) printf("\n He\fllo "); // \n new line NL (like pressing return) printf("\n He\nllo "); // \r carriage return CR (cursor to start of line) //printf("\r"); //printf("Hi....."); // \t horizontal tab HT printf("\n He\tllo "); // \v vertical tab printf("\n He\vllo "); // \" double quote printf("\n He\"l\"lo "); // \' single quote character ' printf("\n He\'l\'lo "); // \\ backslash character printf("\n He\\llo "); // \ddd character ddd where ddd is an ASCII code given in octal or base 8, See Character Conversion Table. //printf(" He\dddllo ", 'a'); // \xddd character ddd where ddd is an ASCII code given in hexadecimal or base 16, See Character Conversion Table. //printf(" He\xdddllo ", 'a'); return 0; }


Happy Exploring!

No comments:

Post a Comment