0% found this document useful (0 votes)
32 views6 pages

Input and Output

Standard input, output, and error files are used for input/output in C. Common functions include scanf() and printf() for formatted I/O, and getchar(), putchar(), gets(), and puts() for unformatted I/O. scanf() reads input from stdin and printf() writes output to stdout. getchar() and putchar() handle single characters while gets() and puts() handle strings. These functions allow programmers to easily collect input and display output in C programs.

Uploaded by

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

Input and Output

Standard input, output, and error files are used for input/output in C. Common functions include scanf() and printf() for formatted I/O, and getchar(), putchar(), gets(), and puts() for unformatted I/O. scanf() reads input from stdin and printf() writes output to stdout. getchar() and putchar() handle single characters while gets() and puts() handle strings. These functions allow programmers to easily collect input and display output in C programs.

Uploaded by

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

C I n p u t a n d O u t p u t - s c a n f , p r i n t f , g e t s , p u t s , g e t c h a r, p u t c h a r

Input and Output in C

In C programming, for input and output, operations are supplied as functions in the

standard library. So, we can take the data through input functions and sends results

through output functions.

Standard Files in C

C language treats all its inputs and outputs as files. A file is a place where information

comes from or can be sent. These files can either be read or written only. These files

are called I/O streams. C language has 3 I/O files (also called streams). Streams are

always open and ready to use. Streams in C are as shown in the table below.

Standard Files File Pointer Meaning

Standard input stdin used to take the input from the device such as Keybo

Standard output stdout used to send output to a device such as monitor or scr

Standard error stderr used to route the error message to the screen

Therefore, the keyboard and screen are referred to as the standard input and output

files. In C, all library functions are covered by the stdio.h header file. For using I/O

functionality, the programmer must include the stdio.h header file into the program.

Types of Input Output

There are 2 kinds of I/O functions as given below.


Formatted Input Output Functions

scanf() in C

 scanf is the input function that gets the formatted input from the file stdin that is

the keyboard. As mentioned earlier, C treats the keyboard input as a file. scanf

is a built-in function that comes with all C compilers. Its header file is stdio.h.

 The scanf() reads all types of data values given by the user and these values

are assigned to the variables. It requires the conversion specification (such as

%s and %d ) to identify the type of data to be read during the program

execution.
 You need to pass the address of the variable to the  scanf function along with
other arguments so that the read values can be assigned to the correct
destination.
scanf() Syntax

int scanf(const char *format, arg1, arg2,....);

 The first argument is the format string (conversion specification) and followed by

a list of variable arguments of pointer types ie., the address of variables that

need to be passed.
scanf() Example

int a;
double b;
scanf("%d\n", &a);
scanf("%lf\n", &b);

 scanf() reads the characters from standard input and interprets them according

to the format string specification. It stops reading when it encounters a space or

when some input fails to match with the conversion specification.

printf() in C programming

 The printf() prints all types of data value to the standard output(typically the

screen). It requires a conversion symbol and variable name to print the data.

The printf() is part of every C compiler. Its header file is stdio.h.

printf() Syntax

int printf( const char *format, arg1, agr2,....);

 The first argument is the format string which is composed of ordinary characters

and conversion specification. This is followed by zero or more optional

arguments.

printf() Example

printf("welcome");
printf("The result is %d ",a);
 Usually the format string is enclosed with the double quotation marks in

both scanf() and printf().

scanf printf Example

Let see the example program using scanf() and printf() functions.

#include<stdio.h>

int main()
{
int a;
printf("Enter the value of a = ");
scanf("%d",&a);
printf("\nThe value of a = %d",a);
return(0);

}O u t p u t

when you compile and execute the above program, you will get the following

output.

Enter the value of a = 10


The value of a = 10

Unformatted I/O Functions

getchar() in C Programming

The getchar() reads character type data from input file  stdin. It reads one character

at a time till the user pressses the enter key. The  getchar() returns a character type.

It reads next character in the input file.

getchar() - Syntax

variable-name = getchar();

getchar() - Example
char ch;
ch = getchar();

This places the next character into the variable ch. It is a low level function.

putchar() in C Programming

The putchar() writes the character type data to the output file  stdout(screen). It is

used to print one character at a time.

putchar() - Syntax

putchar( variable-name);

putchar() - Example

char ch = 'C';
putgachar(ch);

getchar putchar example


#include<stdio.h>

int main()
{
char a;
a = getchar();
putchar(a);
return(0);

The most important point is that the both  getchar() and putchar() could be

implemented as macros, rather than function. This means that it might not be possible

to use functions as parameters inside them:

putchar( function() );
gets() in C Programming
The gets() is used to read string from standard input file  stdin and places it into some

buffer which the programmer must provide. The  gets() stops reading when an enter

key is pressed or EOF is reached.

gets - Syntax

char *gets(char *str)

puts() in C Programming

The puts() is used to send the string to the output file  stdout, until it finds a NULL

end of string marker. The NULL is not written, instead the newline character is written

to the output stdout.

puts - Syntax

int puts(const char *str)

puts() returns an integer value, whose value is only guaranteed if there is an error.

gets puts Example C Program


#include<stdio.h>

int main()
{
char ch[20];
printf("Enter the text: ");
gets(ch);
puts(ch);
return(0);

When you compile and execute the above program, you will get the following output.

Output

Enter the text: welcome


welcome

You might also like