Introduction
One of the essential operations performed in a C language programs is to provide input values to the program and output the data produced by the program to a standard output device. We can assign values to variable through assignment statements such as x = 5 a = 0 ; and so on. Another method is to use the Input then scanf which can be used to read data from a key board. For outputting results we have used extensively the function printf which sends results out to a terminal. There exists several functions in ‘C’ language that can carry out input output operations. These functions are collectively known as standard Input/Output Library. Each program that uses standard input / out put function must contain the statement.# include < stdio.h >
at the beginning.The file name stdio.h is an abbrevation for standard input-output header file. The instruction #include<stdio.h> the compiler 'to search for a file named stdio.h and places its contents at this point in the program.
Single Character Input/output Functions:
The basic operation done in input output is to read a characters from the standard input device such as the keyboard and to output or writing it to the output unit usually the screen. The getchar function can be used to read a character from the standard input device. The scanf can also be used to achieve the function. The getchar has the following form.
Variable_name = getchar();
Variable_name is a valid ‘C’ variable, that has been declared already and that possess the type char.
Example program :
#include<stdio.h> //assigns stdio-h header file to your program
void main() // Indicates the starting point of the program.
{
char C; // variable declaration
printf (“Type one character:”); // message to user
C=getchar(); //get a character from key board and Stores it in variable C.
printf(”The character you typed is = %c”, C); //output
} // Statement which displays value of C on
// Standard screen.
Writing a character on screen
The putchar function which in analogus to getchar function can be used for writing characters one at a time to the output terminal. The general form is
putchar(variable_name);
Where variable_name is a valid 'C' type variable that has already been declared as char type.
Example
answer='y';
putchar(answer);
putchar('\n');
would cause the cursor on the screen to move to the beginning of the next line.
Program shows the use of getchar function in an interactive environment.
#include<stdio.h> // Inserts stdio.h header file into the Pgm
void main() // Beginning of main function.
{
char in; // character declaration of variable in.
printf(”please enter one character”); // message to user
in=getchar(); // assign the keyboard input value to in.
putchar(in); // output ‘in’ value to standard screen.
}
String Input and Output Functions:
If you want to read a string from standard input, you can use the gets function, the name of which stands for "get string". However, this function is deprecated means it is obsolete and it is strongly suggested you do not use it because it is dangerous. It is dangerous because it provides no protection against overflowing the string into which it is saving data. Programs that use gets can actually be a security problem on your computer.
The
gets function takes one parameter, the string in which to store the data read. It reads characters from standard input up to the next newline character (that is, when the user presses <RETURN>), discards the newline character, and copies the rest into the string passed to it. If there was no error, it returns the same string (as a return value, which may be discarded); otherwise, if there was an error, it returns a null pointer. #include<stdio.h>
void main()
{
char my_string[500];
printf("Enter some text\n");
gets(my_string);
printf ("You typed: %s\n", my_string);
}Writing a string on screen
puts function is a simplified version of the printf() function. It displays the string of characters somewhat like printf function, but in a simple default format.
Example
#include<stdio.h>
void main()
{
puts("hello there");
puts("what is your name?")
puts("It is nice to meet you")
}- Formatted Input/Output Functions:
The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanf function has the general form.
Scanf (“control string”, arg1, arg2, arg3 ………….argn);
The format field is specified by the control string and the arguments
arg1, arg2, …………….argn specifies the address of location where address is to be stored.
The control string specifies the field format which includes format specifications and optional number specifying field width and the conversion character % and also blanks, tabs and newlines.
The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed by the type of data that is to be assigned to variable of the assignment. The field width specifier isoptional.
The general format for reading a integer number is
%xd
Here percent sign (%) denotes that a specifier for conversion follows and x is an integer number which specifies the width of the field of the number that is being read. The data type character d indicates that the number should be read in integer mode.
Example :
scanf (“%3d %4d”, &sum1, &sum2);
If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum 2. Suppose the input data was follows 1342 and 175.
The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number 1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating point numbers are assigned then the decimal or fractional part is skipped by the computer.
To read the long integer data type we can use conversion specifier % ld & % hd for short integer.
Input specifications for real number:
Field specifications are not to be use while representing a real number therefore real numbers are specified in a straight forward manner using % f specifier.
The general format of specifying a real number input is
scanf(%f “, &variable);
Example:
scanf(“%f %f %f”, &a, &b, &c);
With the input data
321.76, 4.321, 678 The values
321.76 is assigned to a , 4.321 to b & 678 to C.
If the number input is a double data type then the format specifier should be % lf instead of %f.
Input specifications for a character.
Single character or strings can be input by using the character specifiers.
The general format is
%xc or %xs
Where C and S represents character and string respectively and x represents the field width.
The address operator need not be specified while we input strings.
Example :
scanf(“%C %15C”, &ch, nname);
Here suppose the input given is a, Robert then a is assigned to ch and name will be assigned to Robert.
Formatted Output function
Printing One Line:
printf();
The most simple output statement can be produced in C’ Language by using printf statement. It allows you to display information required to the user and also prints the variables we can also format the output and provide text labels. The simple statement such as
printf(“Enter 2 numbers”);
Prompts the message enclosed in the quotation to be displayed.
A simple program to illustrate the use of printf statement:-
#include<stdio.h>
main()
{
printf(“Hello!”);
printf(“Welcome to the world of Engineering!”);
}
Output:
Hello! Welcome to the world of Engineering.
Both the messages appear in the output as if a single statement. If you wish to print the second message to the beginning of next line, a new line character must be placed inside the quotation marks.
For Example :
printf(“Hello!\n”);
OR
printf(“\n Welcome to the world of Engineering”);
Conversion Strings and Specifiers:
Syntax
Printf (“conversion string”, variable list);
The conversion string includes all the text labels, escape character and conversion specifiers required for the desired output. The variable includes all the variable to be printed in order they are to be printed. There must be a conversion specifies after each variable.
Specifier Meaning
%d – Print a Integer
%i – Print a Integer
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%o – Print actual value
%s – Print a string
%x – Print a hexadecimal integer (Unsigned) using lower case a – F
%X – Print a hexadecimal integer (Unsigned) using upper case A – F
%a – Print a unsigned integer.
%p – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long


0 comments:
Post a Comment