Thursday, 11 July 2013

Printf() & scanf() input output functions in c with format specifiers

Input and output functions are useful to read and display the data. C Programs need to interact with the user by reading data from the standard input and performing operations and finally need to display the output. In c we primarily use two input and output functions printf() and scanf() to display and read the data respectively. Here printf() is output function used to display the data and scanf() is the input function used to read the data from the user. These two inbuilt library functions are defined in  the header file stdio.h. In this article i will explain clearly about the use of printf() and scanf() functions in c and also how to declare and use printf(), scanf() functions in c to read and write data.

Use of Printf() Function in C :-

printf function
Printf() function in c is used to print any message, a value or result. This is an inbuilt function defined in the stdio.h header file. The syntax for using a printf function in c is given below.

Syntax For Printf() Function:-

Printf("<your message here>");
The above syntax is followed to print a message using printf() function. The message which you want to be printed should be placed inside the double quotes. you can print any message and any number of characters using printf() function.

Example To Print a Message Using Printf Function:-

printf(" hello world printf demo ");
Output :-
hello world printf demo.
In the similar way, we can print any message by using the printf() function.

What are Format Specifiers in C ?

format specifiers in c
Format specifiers are the operators used in printf() function to print the data which is referred by an object or a variable. when a value is stored in a particular variable, Then we cannot print the value stored in the variable directly with out the using format specifiers. We can retrieve the data stored in the variables and can print them using these format specifiers in a printf function. Format specifiers start with a percentage(%) symbol and follows a special character to identify the type of the data. There are basically six types of format specifiers available in c they are:
  1. %d
  2. %f
  3. %c
  4. %s
  5. %u
  6. %ld
I explained all these six format specifiers available in c clearly below.

Integer Format Specifier %d :-

int datatype
This %d format specifier is used to represent integer values. It is used in printf() function to print the integer value stored in the variable. The value stored in a integer variable can be retrieved and printed by using the %d format specifier. It is a reference to an integer data. When ever we need to print a integer data, we should use %d format specifier.

Syntax To Use %d Integer Format Specifier:-

printf("%d",<variable name>); 
Here
  • %d is the format specifier for an integer data.
  • variable name is the name of the integer variable.
When ever we declare the printf statement using above syntax, the integer variable returns the value stored in it. Then the returned integer value is printed by using the integer format specifier %d.

Example To Use %d Integer Format Specifier:-

printf("%d",a);
The above example is used to print the value stored in the variable a.  Here the variable a returns a value to the printf function and the returned value is printed using the %d format specifier. For example if there is a integer data 10 in the variable a, then after returning the value, the printf function look like
printf("%d",10);
Now the returned value 10 is printed by using the %d format specifier.

Float Format Specifier %f :-

float data type
This %f format specifier is used to represent fractional values. It is used in printf() function to print the fractional or floating value stored in the variable. The value stored in a float variable can be retrieved and printed by using the %f format specifier. It is a reference to an fractional or floating data. When ever we need to print a fractional or floating data, we should use %f format specifier.

Syntax To Use %f Float Format Specifier:-

printf("%f",<variable name>); 
Here
  • %f is the format specifier for an floating or fractional data.
  • variable name is the name of the float variable.
When ever we declare the printf statement using above syntax, the float variable returns the value stored in it. Then the returned fractional value is printed by using the float format specifier %f.

Example To Use %f Float Format Specifier:-

printf("%f",a);
The above example is used to print the value stored in the variable a.  Here the variable a returns a value to the printf function and the returned value is printed using the %f format specifier. For example if there is a fractional data 5.5 in the variable a, then after returning the value, the printf function look like
printf("%f",5.5);
Now the returned value 5.5 is printed by using the %f format specifier.

Character Format Specifier %c :-

char data type
This %c format specifier is used to represent characters. It is used in printf() function to print the character stored in a variable. The value stored in a char variable can be retrieved and printed by using the %c format specifier. It is a reference to a character data. When ever we need to print a character data, we should use %c format specifier.

Syntax To Use %c Character Format Specifier:-

printf("%c",<variable name>);
Here
  • %c is the format specifier for a character data.
  • variable name is the name of the char variable.
When ever we declare the printf statement using above syntax, the char variable returns the character stored in it. Then the returned character is printed by using the character format specifier %c.

Example To Use %c character Format Specifier:-

printf("%c",a);
The above example is used to print the character stored in the variable a.  Here the variable a returns a character to the printf function and the returned character is printed using the %c format specifier. For example if there is a character 'S' in the variable a, then after returning the value, the printf function look like
printf("%c",S);
Now the returned character 'S' is printed by using the %c format specifier.

String Format Specifier %s :-

string data type
This %s format specifier is used to represent strings. It is used in printf() function to print a string stored in the character array variable. A string stored in a character array variable can be retrieved and printed by using the %s format specifier. It is a reference to a string(collection of characters) data. When ever we need to print a string, we should use %s format specifier.

Syntax To Use %s String Format Specifier:-

printf("%s",<variable name>);
Here
  • %s is the format specifier for a string.
  • variable name is the name of the character array variable.
When ever we declare the printf statement using above syntax, the character array variable returns the string stored in it. Then the returned string is printed by using the string format specifier %s.

Example To use %s String Format Specifier:-

printf("%s",a);
The above example is used to print the string stored in the character array variable a.  Here the variable a returns a value to the printf function and the returned value is printed using the %s format specifier. For example if there is a string "printf" in the variable a, then after returning the string, the printf function look like
printf("%s","printf");
Now the returned string "printf" is printed by using the %s format specifier.

Address Format Specifier %u :-

memory address in c
This %u format specifier is used to represent address of a variable stored in the memory. It is used in printf() function to print the memory address of a variable. When ever we need to print the memory address of a variable, we should use %u format specifier.

Syntax To Use %u Address Format Specifier:-

printf("%u",<variable name>);
Here
  • %u is the format specifier for representing the address of a variable.
  • variable name is the name of the variable.
When ever we declare the printf statement using above syntax, Then the memory address of the specified variable will be printed.

Example To Use %u Address Format Specifier:-

printf("%u",a);
The above example is used to print the memory address of the variable a.

Long Int Format Specifier %ld :-

long int data type
This %ld format specifier is used to represent long integer values. It is used in printf() function to print the long integer value stored in the variable. The value stored in a long int variable can be retrieved and printed by using the %ld format specifier. It is a reference to a long integer data. When ever we need to print a long integer data, we should use %ld format specifier.

Syntax To Use %ld Long Int Format Specifier:-

printf("%ld",<variable name>); 
Here
  • %ld is the format specifier for a long integer data.
  • variable name is the name of the long int variable.
When ever we declare the printf statement using above syntax, the long int variable returns the value stored in it. Then the returned long integer value is printed by using the long int format specifier %ld.

Example To Use %ld Long Int Format Specifier:-

printf("%ld",a);
The above example is used to print the long integer value stored in the variable a. Here the variable a returns a value to the printf function and the returned long integer value is printed using the %ld format specifier. For example if there is a long integer data 45232 in the variable a, then after returning the value, the printf function look like
printf("%ld",45232);
Now the returned value 45232 is printed by using the %ld format specifier.

Rules To Be Followed While Using Printf() Function :-

  • The message which is to be printed should be placed in between double quotes(" ").
  • separate the format specifiers and variables using comma (,).
  • The printf() function should follow a semi colon at the end. This indicates the compiler that It is the end of the statement.
  • As C is a case sensitive language so you should not use capital letters while writing printf() function. how ever you can use capital letters in the body of the message which is to be printed.

Video Tutorial To Learn Printf Function in C:-

Printf function is an important concept in c programming. This video is especially developed for the beginners to learn more about printf function in c programming. watch this video tutorial and learn about printf function in c.

Use of scanf() function in c :-

scanf function in c
Scanf is a built in library function in c. It is a standard input function used to take input from the user during execution of the program. The main use of the scanf function is to take the input from the user and store it inside a variable during execution of the program. This function has a wide use in run time initialization process. By using scanf() function we can store the values in a variable during the execution of the program. The main advantage of scanf function is that we can change the data stored in a variable during execution of the program.

Syntax To Use Scanf() Input Function in C :-

Scanf("<format specifiers>",&<variables>);
The above syntax is to be followed to read values from a console input during the execution of a program. Here
  • The format specifiers are the operators used to indicate the type of the data which is supplied by the user. we should use the same format specifier of the data type of the variable which is used to store the data.
  • variable is the identifier or variable name used to store the data in the memory block of the variable.
  • The symbol  '&' is used to represent the memory address of the variable to store the data.
If we declare the & symbol, Then the compiler stores the value given as input at run time in the address of that particular variable.

Example To Use Scanf() Input Function in C:-

Scanf("%d",&a);
In The above example
  • %d is a integer format specifier.
  • Symbol '&' is used to indicate the memory address of the variable.
  • a is the integer variable name used to store the value in the memory block of the variable.
The scanf function will read an integer value from the console input and stores it in the memory address of the variable a. There are many format specifiers used in scanf function. The different format specifiers used in scanf function are listed below.

Format Specifiers Used in Scanf() Function :-

There are basically five format specifiers used in scanf function they are:
  • %d.
  • %f.
  • %c.
  • %s.
  • %ld.
I will explain all these format specifiers and how to use them clearly.

Integer Format specifier %d :-

int data type
The format specifier %d indicates integer data. It is a reference to a integer data. In scanf function It is used to read integer values and store them in a integer variable. When %d is used inside a scanf function then the compiler understands the data which is given as input by the user as a integer data and it stores the data in the integer variable specified.

Syntax To Use Integer Format Specifier %d:-

Scanf("%d",&<variable name>);
here
  • %d is the integer format specifier to represent integer data.
  • '&' symbol is used to represent memory address of the variable.
  • variable name is used to represent the variable in which the data is going to be stored.

Example To Use Integer Format Specifier %d:-

Scanf("%d",&a);
The above example is used to read a integer value supplied by the user at run time and store it in the integer variable 'a'. For example if the user enters 10 at the console, then the integer data 10 is recognized by the integer format specifier %d and the data is stored in the integer variable 'a'.

Floating Format specifier %f :-

float data type
The format specifier %f indicates floating or fractional data. It is a reference to a float or fractional data. In scanf function, It is used to read fractional values and store them in a float variable. When %f is used inside a scanf function then the compiler understands the data which is given as input by the user as a fractional data and it stores the data in the float variable specified.

Syntax To Use Float Format Specifier %f:-

Scanf("%f",&<variable name>);
here
  • %f is the float format specifier to represent fractional data.
  • '&' symbol is used to represent memory address of the variable.
  • variable name is used to represent the variable in which the data is going to be stored.

Example To Use Float Format Specifier %f:-

Scanf("%f",&a);
The above example is used to read a fractional value supplied by the user at run time and store it in the float variable 'a'. For example if the user enters 10.5 at the console, then the fractional data 10.5 is recognized by the float format specifier %f and the data is stored in the float variable 'a'.

Character Format Specifier %c :-

char data type
The format specifier %c indicates character data. It is a reference to a character. In scanf function It is used to read a character and store it in a character variable. When %c is used inside a scanf function then the compiler understands the data which is given as input by the user is a character data and it stores the data in the char variable specified.

Syntax To Use Character Format Specifier %c:-

Scanf("%c",&<variable name>);
here
  • %c is the character format specifier to represent character.
  • '&' symbol is used to represent memory address of the variable.
  • variable name is used to represent the variable in which the data is going to be stored.

Example To Use Character Format Specifier %c:-

Scanf("%c",&a);
The above example is used to read a character supplied by the user at run time and store it in the char variable 'a'. For example if the user enters 'S' at the console, then the character 'S' is recognized by the character format specifier %c and the data is stored in the char variable 'a'.

String Format Specifier %s :-

string data type
The format specifier %s indicates strings. Strings is a collection of characters which is stored in a character array variable. The format specifier %s is a reference to a string. In scanf function It is used to read a string(collection of characters) and store it in a character array variable. When %s is used inside a scanf function then the compiler understands the data which is given as input by the user is a string and it stores the data in the character array variable specified.

Syntax To Use String Format Specifier %s:-

Scanf("%s",<variable name>);
here
  • %s is the string format specifier to represent integer data.
  • variable name is used to represent the variable in which the data is going to be stored.

Example To Use String Format Specifier %s:-

Scanf("%s",a);
The above example is used to read a string supplied by the user at run time and store it in the character array variable 'a'. For example if the user enters "scanf" at the console, then the string "scanf" is recognized by the string format specifier %s and the data is stored in the character array variable 'a'.
Note :-
While using string we wont use '&' symbol in scanf function because. The variable used to read a string is a character array variable. So An entire array itself represent memory address. So there is no need to specify again '&' symbol to represent memory address of the variable.

Long Integer Format specifier %ld :-

long int data type
The format specifier %ld indicates Long integer data. It is a reference to a long integer data. In scanf function It is used to read long integer values and store them in a long int variable. When %ld is used inside a scanf function then the compiler understands the data which is given as input by the user is a long integer data and it stores the data in the long int variable specified.

Syntax To Use Long Integer Format Specifier %ld:-

Scanf("%ld",&<variable name>);
here
  • %ld is the integer format specifier to represent Long integer data.
  • '&' symbol is used to represent memory address of the variable.
  • variable name is used to represent the variable in which the data is going to be stored.

Example To Use Long Integer Format Specifier %ld:-

Scanf("%ld",&a);
The above example is used to read a long integer value supplied by the user at run time and store it in the long int variable 'a'. For example if the user enters 42520 at the console, then the long integer data 42520 is recognized by the long integer format specifier %ld and the data is stored in the long int variable 'a'.

Rules to be followed while using scanf() function :-

  • The format specifiers should always be placed in between double quotes(" ").
  • separate the format specifiers and variables using comma (,).
  • scanf() statement should be ended with a semi colon (;). This indicates the compiler that it is the end of the statement.
  • Always use a right format specifier while using scanf() function in c. If you are reading an integer variable, then use %d format specifier.
  • Always use the exact data type of the variable for reading the data. if you are reading a integer data then use only int variable.
  • As C is a case sensitive language we should not use capital letters while writing scanf() function.

Video Tutorial To Learn Scanf Function in C :-

Scanf function in c is the important concept in c basics. This video tutorial is especially developed for beginners to learn clearly the concept of scanf function and how to use a scanf function to read the data during run time from the console in c. Watch the below video tutorial to learn more about scanf function in c

C Program to use Printf() and scanf() functions:

#include<stdio.h>
int main(void)
{
int a,b;
printf("\n enter two values :");
scanf("%d%d",&a,&b);
printf("\n value of a is %d",a);
printf("\n value of b is %d",b);
return 0;
}

Conclusion:

Printf() and scanf() are the input and output functions used in c programming language. I hope this article helped you. if you have any doubts, then share them in the comments below. Thank you........

We Will Send Some More Information For Free!

Like This Story? Please Share!

Other Ways To Share:

Like Our Site? Follow Us!


0 comments:

Post a Comment

Your feedback is always appreciated. We will try to reply to your queries as soon as time allows.
Please do not spam. Spam comments will be deleted upon our review.

Regards:
Srinath Reddy.

 

Articles cannot be reproduced without permission from the author.

BEGINNERS-TUTORIALS © 2013. All Rights Reserved | Contact | About