ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Chapter 10: C language Input and Output

prodevelopertutorial August 15, 2018

C language Input and output can be classified into 2 types:

  1. Formatted I/O
    1. printf ()
    2. scanf()
  1. Un-Formatted I/O
    1. getch()
    2. putch()
    3. getchar()
    4. putchar()
    5. gets()
    6. puts()
  1. Escape sequence
  2. Format Specifiers

 

1. Formatted I/O:

 

Formatted functions allow us to display the results according to our requirements. The formatted functions return a value after executing the results. The return value will be equal to the number of variables successfully written or read from the input.

 

As formatted functions work with various data types they require format specifiers to understand the type of the variable they are working with.

 

a. printf():

 

“printf()” is used to display the output to the console. When “printf” is used, it will return number of bytes it has written to the console.

 

As “printf” can be used to display output of different types of variables, we need to include a format specifier. List of format specifer have been given at the end of the tutorial.

 

Example of a “printf()” statement displaying different types of variables.

#include<stdio.h>

int main()
{
	int iNum = 1;
	float fNum = 12.34;
	char c = 'a';

	int bytes_written = printf("%d  %f  %c \n", iNum, fNum, c );

	printf("The number of bytes written on console by previous printf statement = %d \n", bytes_written);
}

Output:

1  12.340000  a
The number of bytes written on console by previous printf statement = 17

 

Width specifiers in printf():

“.<number>” – This is used in floating numbers. It tells the number of digits to be displayed after decimal point.

 

“%<number>d” – This is used for integer numbers.

 

Example:

#include<stdio.h>

int main()
{
	int iNum = 12;
	float fNum = 123.456789;

	printf("%3d\n", iNum);
	printf("%4d\n", iNum);

	printf("%*d\n", 3, iNum);
	printf("%*d\n", 4, iNum);

	printf("%.2f\n", fNum);
	printf("%.3f\n", fNum);
	printf("%.4f\n", fNum);

}

Output:

12
  12
 12
  12
123.46
123.457
123.4568

 

b. scanf():

 

scanf function is used to accept input of different data types. Hence format specifiers are important. This function will return the number of variables read from the console.

 

General syntax:

	scanf(“format_specifier”, &<variable_name>);

Here we give the address “&” symbol, the compiler will go to that address and save the input value.

 

Example of scanf:

 #include<stdio.h>

int main()
{
	int iNum = 0;
	float fNum = 0;

	printf("Enter a int and float numbers\n");
	int read_bytes = scanf("%d %f", &iNum, &fNum);
	printf("The number of variable read from console is = %d \nThe value read is = %d  and %f\n",read_bytes, iNum, fNum );

}

Output:

Enter a int and float numbers
12
12.34
The number of variable read from console is = 2
The value read is = 12  and 12.340000

 

2. Unformatted I/O:

Unformatted functions, we will not be having any control of how the data can be modified. They are just printed from starting of the line.

Unformatted functions will treat all the variables as a character data type. Hence format specifier is not necessary.

 

a. getch() and putch()

getch() accepts input from the console but doesn’t displays it.

getche() accepts input from console and displays it.

putch() displays a character to the console.

 

All the above 3 functions will work on one character at a time.

 

Example:

#include<stdio.h>
#include<string.h>

int main()
{

	char ch;

	printf("Enter a character\n");
	ch = getch();


	printf("\nEntered character is = \n");
	putch(ch);

}

Note: Most of the modern compilers will not support above 3 functions.

 

b. getchar() and putchar()

Both of the above functions are used to accept a single character and display a single character respectively.

#include<stdio.h>
#include<string.h>

int main()
{

	char ch;

	printf("Enter a character\n");
	ch = getchar();


	printf("\nEntered character is = \n");
	putchar(ch);

}

Output:

Enter a character
d

Entered character is =
d

 

c. gets() and puts()

gets() and puts() functions are used to read string from input and display the string to output respectively.

 

Example:

#include<stdio.h>
#include<string.h>

int main()
{

	char string[20];

	printf("Enter a string\n");
	gets(string);


	printf("\nEntered string is = \n");
	puts(string);

}

Output:

Enter a string
warning: this program uses gets(), which is unsafe.
hello

Entered string is =
hello

 

3. Escape sequence

 

Escape sequence is used to give a special meaning. Usually they will be accompanied by a backslash and another character.

 

Below are the escape sequence available in C:

\n		New Line
\\		Backslash
\b		Backspace
\’		Single quote
\t		Horizontal Tab
\”		Double quote
\0		Null
\?		Question Mark

 

4. Format Specifiers

As we saw in “Formatted I/O” we need to specify the correct format for a particular data type to work. Below are the different format specifiers that will be used in C language.

Short Int			                %d
Short Unsigned Int		%u
Long Int			                %ld
Long Unsigned			%lu
Floating			                %f
Double Floating		        %lf
Signed Character		        %c
Un Signed Character		%c
String 				        %s
Pointer address		        %p
Octal Number			%o

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT