C - Input & Output When we are saying Input that means to feed some data into program. This can be...

17
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. When we are saying Output that means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output the data on the computer screen as well as you can save that data in text or binary files.

Transcript of C - Input & Output When we are saying Input that means to feed some data into program. This can be...

Page 1: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

C - Input & OutputWhen we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.

When we are saying Output that means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output the data on the computer screen as well as you can save that data in text or binary files.

Page 2: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard FilesC is a functional programming language. Therefore, it has no exclusive built-in statements to perform basic input-output operations. To perform these input-output operations, C provides a library of functions. This library is called a standard input-output library. It is denoted by stdio. The header file containing such library functions is called stdio.h.

Standard File File Pointer DeviceStandard input stdin KeyboardStandard output stdout ScreenStandard error stderr Your screen

Page 3: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard Files

There are two type of input output statements. They are:1. Formatted I/O statements2. Unformatted I/O statements

The formatted I/O statements enable user to specify the type of data and the way in which it should be read in or written out.

The unformatted I/O statements do not specify the type of data and the way it should be read or write.

Page 4: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

I/O Statements

There are two type of input output statements. They are:1. Formatted I/O statements2. Unformatted I/O statements

Input Statements Output Statements

Formated Scanf() Printf()

Unformated Getchar() Putchar()

Gets() Puts()

Page 5: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C Most of the program is ending with getch(),and so we think that getch() is used to display the output...but it is wrong.It is used to get a single character from the console. Just see the behaviors of various single character input functions in c. getchar() getche() getch()

FUNCTION : 1. GETCHAR()

getchar() is used to get or read the input (i.e a single character) at run time.

Page 6: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C

Declaration:int getchar(void);

Example Declaration:char ch;ch = getchar();

Return Value:This function return the character read from the keyboard.

Page 7: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C Example Program:void main(){char ch;ch = getchar();printf("Input Char Is :%c",ch);}

Program Explanation:Here,declare the variable ch as char data type, and then get a value through getchar()library function and store it in the variable ch.And then,print the value of variable ch.During the program execution, a single character is get or read through the getchar(). The given value is displayed on the screen and the compiler wait for another character to be typed. If you press the enter key/any other characters and then only the given character is printed through the printf function.

Page 8: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C 2. GETCHE()getche() is used to get a character from console, and echoes to the screen.Declaration:

int getche(void);

Example Declaration:char ch;ch = getche();

Remarks:getche reads a single character from the keyboard and echoes it to the current text window, using direct video or BIOS.

Return Value:This function return the character read from the keyboard.

Page 9: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C Example Program:void main(){char ch;ch = getche();printf("Input Char Is :%c",ch);}

Program Explanation:Here,declare the variable ch as char data type, and then get a value through getche()library function and store it in the variable ch.And then,print the value of variable ch.

During the program execution, a single character is get or read through the getche(). The given value is displayed on the screen and the compiler does not wait for another character to be typed. Then, after wards the character is printed through the printf function.

Page 10: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C 3. GETCH()getch() is used to get a character from console but does not echo to the screen.Declaration:

int getch(void);Example Declaration:

char ch;ch = getch(); (or ) getch();

Remarks:getch reads a single character directly from the keyboard, without echoing to the screen.

Return Value:This function return the character read from the keyboard.

Page 11: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

USE OF GETCH(),GETCHE() AND GETCHAR() IN C Example Program:void main(){char ch;ch = getch();printf("Input Char Is :%c",ch);}

Program Explanation:Here,declare the variable ch as char data type, and then get a value through getch() library function and store it in the variable ch.And then,print the value of variable ch.

During the program execution, a single character is get or read through the getch(). The given value is not displayed on the screen and the compiler does not wait for another character to be typed.And then,the given character is printed through the printf function.

Page 12: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard FilesThe int gets()function reads in every thing you enter from the keyboard until the ENTER key or RETURN is pressed. Everthing, means a string which is a sequence of all printable ASCII charaters. The RETURN key that was pressed at last would not be stored in the charater string. It overcomes the limitation of the scanf() statements with %S option.Syntax:

gets(string);Where string is a sequence of characters and is of type char e.gmain(){

char name[25];Printf(“ enter your name”);gets(name);

}

Page 13: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard FilesThe int putchar(int c) function puts the passed character on the screen and returns the same character. This function puts only single character at a time.

Putchar() functionputchar(ch_var);

Where, ch_var is a character variable which is enclosed within the parentheses.e.gmain(){Char ch;putchar(ch);}

Page 14: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard Files

include<stdio.h>main(){char letter;letter= getchar();putchar(letter);}

Page 15: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

#include <stdio.h>

int main( )

{

int c;

printf( "Enter a value :");

c = getchar( );

printf( "\nYou entered: ");

putchar( c );

return 0;

}

When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads only a single character and displays it as follows:

Enter a value : this is testYou entered: t

Page 16: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard Files-Input FunctionsData Type Functions Purpose

Char getc() Get character from the keyboard as soon as it is typed,no need to hit enter

getche() Similar to getch(),expect echoes the character on the screen

fgtechar() Gets the character and choes it on the screen,requires enter key to be hit.

getchar() A macro,works similar to fetchar()

String Gets() Accepts a string until enter key is hit

Page 17: C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.

The Standard Files-Output FunctionsData Type Functions Purpose

Char putch() Print a character on the screen

fputchar() Print a character on the screen

putchar() A macro,works similar to Putch()

String puts() Output a string to the screen