Input-Output
Outline
1. Data Type and Range
2. Basic Input and Output
3. Header Files
Data Type and Ranges
Integer Type:
Data Type and Ranges
Float Type:
The % Format Specifiers
%c char single character
%d (%i) int signed integer
%e (%E) float or double exponential format
%f float or double signed decimal
%g (%G) float or double use %f or %e as required
%o int unsigned octal value
%p pointer address stored in pointer
%s array of char sequence of characters
%u int unsigned decimal
%x (%X) int unsigned hex value
The % Format Specifiers
%d (print as a decimal integer)
%6d (print as a decimal integer with a width of at least 6 wide)
%f (print as a floating point)
%4f (print as a floating point with a width of at least 4 wide)
%.4f (print as a floating point with a precision of four characters after the
decimal point)
%3.2f (print as a floating point at least 3 wide and a precision of 2)
Format Specifiers Example
example
Library Function with Header file
stdio.h: I/O functions:
getchar() returns the next character typed on the keyboard.
putchar() outputs a single character to the screen.
printf()
scanf()
string.h: String functions
strcat() concatenates a copy of str2 to str1
strcmp() compares two strings
strcpy() copys contents of str2 to str1
Library Function with Header file
ctype.h: Character functions
isdigit() returns non-0 if arg is digit 0 to 9
isalpha() returns non-0 if arg is a letter of the alphabet
isalnum() returns non-0 if arg is a letter or digit
islower() returns non-0 if arg is lowercase letter
isupper() returns non-0 if arg is uppercase letter
math.h: Mathematics functions
cos() returns cosine of arg
exp() returns natural logarithm e
fabs() returns absolute value of num
sqrt() returns square root of num
pow() returns power of num as specified
Library Function with Header file
time.h: Time and Date functions
time() returns current calendar time of system
difftime() returns difference in secs between two times
clock() returns number of system clock cycles since program execution
stdlib.h:Miscellaneous functions
malloc() provides dynamic memory allocation
rand() generates random numbers
srand() used to set the starting point for rand()
The scanf() & printf() functions
The C library function scanf() reads input from stdin.
The C library function printf() sends output to stdout..
The scanf() & printf() functions
examle2
The getchar() & putchar() functions
The getchar() function reads only single character at a time.
The putchar(c) function displays single character on the screen.
Programming example with getchar( ) and putchar()
char c;
printf( "Enter a value :");
c = getchar();
printf( "\nYou entered: ");
putchar(c);
The gets() & puts() functions
The gets() function reads a line of text .
The puts() function displays a line of text.
char str[100];
printf( "Enter a value :");
gets(str);
puts(str);
Thank You.
Questions and Answer
References
Books:
1. Programming in ANSI C By E. Balagurusamy
2. Teach yourself C. By Herbert Shield
3. Programming With C. By Byron Gottfried
Web:
4. www.wikbooks.org
and other slide, books and web search.