Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

The formatted I/O in C language can be done using two popular functions known as printf() and scanf().

Printf function

printf() stands for print formatted which has the following syntax:

Syntax1: printf(“string”);

Using the above syntax, we can print any text or phrase that is enclosed in between the two double quotes on the console to the user.

Syntax2: printf(“string [%formatspecifier]”,[variable]);

If you have used any format specifiers(like %d, %c etc) inside the double quotes of 1st argument of printf() function, then the place of the format specifier would be replaced with the corresponding variable value provided as 2nd argument while printing.

In other words, we can say there is no need of using format specifiers inside the text string of printf() function.  In this case printf() function would have only one argument.  But if we have used any format specifiers then the number of arguments that printf() function would have is depends upon the number of format specifiers inside the 1st argument.

Example:

printf(“[%formatspecifier1][%formatspecifier2] ….[%formatspecifierN]”, var1,var2,…,varN);

the place format specifier1 would be replaced with the value of var1 and specifier2 would be replaced with var2.

Two consecutive printf statements cannot print your specified text in multiple lines.  It can only be done with the help of newline character.

For example check the following code:

printf(“First Line”);

printf(“Second Line”);

printf(“Third Line”);

The output of the above code would be : First LineSecond LineThird Line

From the above output we can understand text divided into multiple printf statements could not mean to the system that it has to be displayed in multiple lines.  Change the same code with newline character as below to get your desired output.

printf(“First Linen”);

printf(“Second Linen”);

printf(“Third Line”);

OR

printf(“First LinenSecond LinenThird Line”);

scanf() function

Let us now continue with the second formatted input function scanf().  This is used to get input from the user using standard Input i.e. keyboard and store in a memory location referring by an identifier.

Syntax of scanf() function:

scanf(“[%format specifier1][%format specifier2]….[%formatspecifierN]”, var1,var2,…,varN);

//code to demonstrate all basic data types of C Language

#include

void main()

{

    int a;

    float b;

    double c;

    char d;

    printf(“Enter any Character:”);

    scanf(“%c”,&d);

    printf(“Enter any Integer Value:”);

    scanf(“%d”,&a);

    printf(“Enter any Float Value:”);

    scanf(“%f”,&b);

    printf(“Enter any Double Value:”);

    scanf(“%lf”,&c);

    printf(“\n\nYour data is:\n”);

    printf(“Integer value in a is %d\n”,a);

    printf(“Float value in b is %f\n”,b);

    printf(“Double value in c is %lf\n”,c);

    printf(“Character value in d is %c”,d);

}

The printf() following scanf() function need no newline character since user will press Enter after his input.  But printf() following another printf() needs newline character if you want to print the output in next line.

//check the following code to understand the working of printf followed by scanf

void main()

{

    printf(“Enter a number:”);

    int a;

    scanf(“%d”,&a);

    printf(“value of a is %d”,a);

}

//printf following by printf

void main()

{

printf(“First Line”);

printf(“Second Line”);

printf(“\nThird Line”);

printf(“\nFourth Line\nFifth Line”);

printf(“\nSixth Line”);printf(“\nSeventh Line”);

}

Unformatted I/O

Unformatted I/O in C language is done by the functions like getch, getche, putch, purchar() etc.  These functions will be discussed later in this course in Strings Handling.

You cannot copy content of this page