Data Input and Output in C
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.
• 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 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.
• 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.
• 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.