- C has 6 popular data input and output library functions. They are getchar, putchar, printf, scanf, gets and puts.
- These functions handle data transfer between computer and the input/output device (keyboard,monitor)
- The functions getchar and putchar allow single characters to be transferred into and out of the computer.
- The functions printf and scanf allow the transfer of single characters, numerical values and strings
- The functions gets and puts handle input and output of strings.
- To use these functions in a program the standard input output header file stdio.h must be included in the program.
- The getchar() function reads a single character from the input device(keyboard)
- Syntax:
char variable=getchar(); - Example:
char c;
c=getchar();
The character entered on the keyboard is read by the getchar function and assigned to the character variable c. - If an end of file condition is found when reading a character using getchar, the value of symbolic constant(-1) will be returned.
- The putchar() function display a single c character on the standard output device.
- Syntax:
putchar(variable); - Example:
char c;
putchar(c);
The character stored in the variable c is displayed in the monitor.
- scanf function used to read any numerical values, single characters or strings.
- The function returns the number of data items that have been entered successfully.
- Syntax:
scanf("control string",argument list);
control string=%conversion character. - Table shows the conversion character and the data item meaning
Conversion character Meaning c Single character d decimal integer e,f,g Floating point value h Short integer i decimal, hexa decimal or octal integer o octal integer u unsigned decimal integer x Hexadecimal integer s String [...] String include whitespace characters - The arguments may be variables or array.
- Each variable name must be preceded by an ampersand(&). Array name should not begin with &.
- Example
#include<stdio.h>
int main()
{
int no;
float f;
char text[50];
scanf("%d %f %s",&no,&f,text);
} - If 2 or more data items are entered they must be separeted by whitespace characters[blank space (or) new line (or) tab].
- While reading a string that include whitespace characters use []. specify the whitespace characters inside the []
- Example: Read a string which contains only capital letters and blank space
#include<stdio.h>
int main()
{
char text[50];
scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]",text);}here if the input is given as WELCOME TO C then text contain WELCOME TO C. If the input is given as Welcome To C then only the single letter W would be assigned to text - If the characters with in the brackets are given after circumflex(^) operator then the scanf will read all the characters except the characters in the []
scanf("%[^\n]",text);The program will read any characters(maximum 49 characters) until the newline is pressed.
- It is possible to limit the number of characters read by specifying a maximum field width for that data item. To do that an unsigned integer integer indicating the field width is placed with in the control string between the percent sign(%) and the conversion character.
- Syntax:
%fieldwidth conversioncharacter - Example:
%4d -->The decimal number can contain maximum of 4 digits
%5f -->The float number can contain 5 digits including the dot(20.20,200.1) - The data item may contain fewer characters than the specified field width but cannot exceed the specified field width. If we give more characters than the specified field width the leftover characters are incorrectly interpreted as a next data item
- Example1:
{
int a,b,c;
scanf("%2d %2d %2d",a,b,c);
}
- If the input is given as 12 34 56 then a=12 b=34 c=56.
- If the input is given as 123456789 then a=12 b=34 c=56
- If the input is 123 4 5678 then a=12 b=3 c=4
- Example 2:
float f;
scanf("%5f",&f);
- if the input is 555.55 then f=555.500000
- If the input is 456789.555 then f=45678.000000
- if the input is 55555.555 then f=55555.00000
- If there is no whitespace present between the conversion characters in the scanf function then the whitespace characters with in the input data will be interpreted as a data item.
- Example:
char a,b,c;If x y z is given as input then a=x b= (blankspace) c=y
scanf("%c%c%c",&a,&b,&c); - To avoid reading the blankspace use %1s instead of %c
char a,b,c;If x y z is given as input then a=x b=y c=z or we can write the scanf as scanf("%c %c %c",&a,&b,&c);
scanf("%c%1s%1s",a,b,c); - Unrecognized characters within the control string are expected to be matched by the same characters in the input data. The unrecognized characters are read but not assigned to any variable but if the characters are not present then the scanf function will terminate.
- Example:char a,b;If the input is given as a * b then only a=a b=b. if the input is given as a b then the function is stop executing as * is not given as input. so a is assigned to a . b is Null
scanf("%c * %c",&a,&b);
int recursive(int)
ReplyDelete{ if (n==1)return(1);
else return(recursive(n-1)+recursive(n-1));
}
//how to find complexity
deveshbharathan@dtu.co.in