0% found this document useful (0 votes)
46 views10 pages

Data Input and Output in C

The document discusses various input and output functions in C like scanf(), printf(), gets(), puts() etc. It explains how to take input and display output in C programs. It also covers reading and writing of characters, strings, formatted input and different format specifiers that can be used with printf().

Uploaded by

Amlan Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views10 pages

Data Input and Output in C

The document discusses various input and output functions in C like scanf(), printf(), gets(), puts() etc. It explains how to take input and display output in C programs. It also covers reading and writing of characters, strings, formatted input and different format specifiers that can be used with printf().

Uploaded by

Amlan Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Input and Output in C

Introduction
A C program involves the following sections:
• Documentations (Documentation Section)
• Preprocessor Statements (Link Section)
• Global Declarations (Definition Section)
• The main() function
– Local Declarations
– Program Statements & Expressions
• User Defined Functions
Lets Start with an Example
/* Program to write Hello World on Screen*/
# include<stdio.h>
int main()
{
printf("Hello, World");
}

Output:
Hello, World
• Comments
/* Program to write Hello World on Screen*/
Comment can be used anywhere in the program to add info about the program or
code block, which will be helpful for developers to understand the existing code in
the future easily.
The compiler ignores comments and used by others to understand the code.

• #include<stdio.h>
This is a preprocessor command. That notifies the compiler to include the header
file stdio.h in the program before compiling the source-code.

• main()
This is the main function, which is the default entry point for every C program and
the void in front of it indicates that it does not return a value.

• Braces {}
Two curly brackets "{...}" are used to group all statements.
Curly braces which shows how much the main() function has its scope.

• printf()
It is a function in C, which prints text on the screen.
This is another pre-defined function of C which is used to be displayed text string in
the screen.
C Input and Output (I/O)
• The three essential functions of a computer are reading, processing and writing data.
• Majority of the programs take data as input, and then after processing the processed
data is being displayed which is called information.
• In C programming, we use scanf() and printf() predefined function to read and print
data.

• Example
#include<stdio.h>
void main()
{ In this program
int a,b,c;
printf("Please enter any two numbers: \n"); scanf() is used to take
scanf("%d %d", &a, &b); input from the user,
c = a + b;
printf("The addition of two number is: %d", c); And
}
printf() is used to
 Output display output result
Please enter any two numbers: 12 3 on the screen.
The addition of two number is:15
Managing Input / Output
• I/O operations are useful for a program to interact with users.
• stdlib is the standard C library for input-output operations.

• While dealing with input-output operations in C, two important streams


play their role. These are:
– Standard Input (stdin)
– Standard Output (stdout)

• Standard input or stdin is used for taking input from devices such as the
keyboard as a data stream.
• Standard output or stdout is used for giving output to a device such as a
monitor.

• For using I/O functionality, programmers must include stdio header-file


within the program.
Reading and Writing Characters in C
• READING CHARACTERS IN C
• WRITING CHARACTERS IN C
• putchar() function is used to write
• getchar() function can be used to read a
single character. This function is alternate characters, but one at a time.
to scanf() function.
• SYNTAX: putchar(var_name);
• SYNTAX: var_name = getchar();
• EXAMPLE
• EXAMPLE #include<stdio.h>
#include<stdio.h> void main()
void main() {
{ char result = 'P';
char title; putchar(result);
title = getchar(); putchar('\n');
} }

 For files, getc syntax used to accept a • For Files, putc syntax used for sending a
character from standard input. single character to the standard output.
int getc(FILE *stream); int putc(int c, FILE *stream);
Formatted Input
• It refers to an input data which has been arranged
in a specific format.
• This is possible in C using scanf(). EXAMPLE
#include<stdio.h>
void main()
• scanf("control string", arg1, arg2, ..., argn); {
int var1= 60;
• The field specification for reading integer inputted int var2= 1234;
number is: scanf("%2d %5d", &var1, &var2);
%w sd }
• Here the % sign denotes the conversion
specification;
• w signifies the integer number that defines the
field width of the number to be read.
• d defines the number to be read in integer format.

• Input data items should have to be separated by


spaces, tabs or new-line and the punctuation
marks are not counted as separators.
Reading and Writing Strings in C
• There are two popular library functions gets() and puts() to deal with strings in C.

• gets: The char *gets(char *str) reads a line from stdin and keeps the string pointed to by
the str and is terminated when the new line is read or EOF is reached.

• The declaration of gets() function is:


• SYNTAX
char *gets(char *str);
Where str is a pointer to an array of characters where C strings are stored.

• puts: The function - int puts(const char *str) is used to write a string to stdout, but it
does not include null characters. A new line character needs to be appended to the
output.

• The declaration is:


• SYNTAX
• int puts(const char *str)
where str is the string to be written in C.
C Format Specifiers
• Format specifiers can be defined as the operators which are used in association
with printf() function for printing the data that is referred by any object or any
variable.
• Format specifiers start with a percentage % operator and followed by a special
character for identifying the type of data.
• There are mostly six types of format specifiers that are available in C.

Specifier Description Syntax


%d Integer Format Specifier printf("%d",<variable name>);
%f Float Format Specifier printf("%f", <variable name>);
%c Character Format Specifier printf("%c",<variable name>);
%s String Format Specifier printf("%s",<variable name>);
%u Unsigned Format Specifier printf("%u",<variable name>);
%ld Long Int Format Specifier printf("%ld",<variable name>);

You might also like