Monday, 11 May 2015

INPUT & OUTPUT STATEMENT

Hello folks, I hope my C language related topics are helping you out. C language is very easy to learn.If you have any problem or queries related to C language you can ask me in comments.

And today topic is Input & output statement

- To make our program interactive Input statements are used, which takes input at the run-time. Output statements are used for print result, which we have to evaluated or accepted.

Formatted I/O statements
*   Printf ()

The usually used output statement is printf (). It is one of the library function.
Syntax: printf(“format string”,argument list);

-          Format string may be a collection of escape sequence or/and conversion specification or/and string constant.
-          The format string directs the printf() to display the entire text enclosed within the double quotes without any change.

  •  Conversion specification

Conversion specification is also a pair of character. It is preceded by % and followed by a quote which may be a character. The conversion specification inscribes the printf() function that it could print some value at that location in the text. The conversion characters supported by C are as follow


Conversion character
                   meaning
%d
Data item is displayed as a signed decimal integer.
%i
Data item is displayed as a single decimal integer.
%f
Data item is displayed as a floating-point value without an exponent.
%c
Data item is displayed as a single character.
%e
Data item is displayed as a floating point value with an exponent.
%g
Data item is displayed as a floating point value using either e-type of f-type conversion depending on value.
%o
Data item is displayed as an octal integer, without a leading zero.
%s
Data item is displayed as string.
%u
Data item is displayed as an unsigned decimal integer.
%x
Data item is displayed as a hexadecimal integer, without a leading 0x.
 


Example

#include<stdio.h>
#include<conio.h>
Void main()
{
Char a=’x’;
Int b=10;
Float c=3.14;
Double d=3.14e+10;
Clrscr();
Printf(“character a=%C\n “,a);
Printf(“Integer b=%d\n”,b);
Printf(“float no c=%f\n”,c);
Printf(“double float d=%e\n”,d);
Getch();

}



*       Scanf()
The usually used input statement is scanf() function.

Syntax: Scanf(“format string”,argument list”);

-          The format string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer(%d),  float(%f), character(%c) or string(%s).
-          The argument list contains a list and separated by comma. The number of argument is not fixed; however corresponding to each argument there should be format specifier.
-          Inside the format string the number of argument should tally with the number of format specifier.
Example: if I is an integer and j is a floating point number, to input these two numbers we may use scanf(“%d%f”,&i,&j);

Example:
Include<stdio.h>
Include<Conio.h>
Void main()
{
            char a;
int b;
float c;
double d;
clrscr();
printf(“\n Enter a character”);
scanf(“%C”,&d);
printf(“\n Enter an integer”);
scanf(“%d”,&b);
printf(“\n enter float no”);
scanf(“\%d”,&c);
printf(“\n enter double no”);
scanf(“%1f”,&d);
getch()
}

 Unformatted I/O Statements
Getchar():
Getchar() will accept a character from the console or from a file, diplays immediately while typing and need to press Enter key for proceeding.

Syntax: int getchar(void)

It returns the unsigned char that they read. If end-of-file or an error is encountered getchar() functions return EOF.

Example: char a;
                                    a=getchar();

The function “getchar()” reads a single character from the standard input device and assigns it to the variable ”a”.

Example:

#include<stdio.h>
#include<conio.h>
Void main()
{
char a;
clrscr();
printf(“\n enter a character “);
a=getchar();
printf(“\n given character is %c”,a);
getch();
}

* Getch():
Getch() is used to get a characters from console but does not echoto yhe screen.

Syntax:
 int getch(void)

Example declaration:
char ch;
ch=getch();


Getch() reads a single character directly from the keyboard, without echoing to the screen, this function return the character read from the keyboard.

Example:
Void main()
{
Char ch;
Ch=getch();
Printf(“\n input char is :%c”,ch);
}
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 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().

*   getche():
getche() is used to get a character from console, and echoes to the screen. Getche reads a single character from the keyboard and echoes it to current text windows, using direct video or BIOS. This function return the character read from the keyboard.

Syntax:
Int getche(void)

Example Declaration:

char ch;
ch=getche();

Example:
Void main()
{
char ch;
ch=getche();
printf(“input char is:%c”,ch);
}

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 getch(). The given value is displayed on the screen and the compiler does not wait for another character to be typed.

*  putchar() :
putchar() function displays a single character on the screen.
Syntax: int putchar(int c);

Example:

#include<stdio.h>
#include<conio.h>
{
char a;
clrscr(0;
printf(“\n enter a character “);
a=getchar();
printf(“\n given character is”)
putchar(a);
getch();
}

* puts():
It is to display a string on a standard output device. The puts() function automatically insert a newline character at the end of each string it display so each subsequent string displayed with puts() is on its own line. puts() is on its own line. puts() return non-negative on success, or EOF on failure.


Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char *str;
clrscr();
printf(“\n enter a string”);
gets(str);
printf(“\n given no is”);
puts(str);
getch();
}

No comments:

Post a Comment