2. INTRODUCTION
Reading , processing and writing are the three main
functions in a program.
For reading and writing we are using two formatted I/O
function
* printf() -for writing
* scanf() -for reading
There are two types of I/O functions
* formatted I/O
* unformatted I/O
3. Formatted I/O
printf() ~ used to print output to the screen.
SYNTAX:
printf(“format”,var1,var2…);
format represent the text to be displayed or format
specification a or some escape sequence.
Ex: printf(“n A Value : %d “,a);
Escape sequence Text to be
displayed
Format
specification
variable
4. printf() never proceed in a newline automatically.
printf() also support special format specification.
General form:
%w.p type-specifier
w-total number of columns for a output value.
p-number of digits right side of the decimal.
Example:
printf(“%2d”,a); - for integer value.
printf(“%2.7f”,a); - for real value
printf(“%2c”,a); - for a character
printf(“%2s”,a); - for a string
6. scanf()- getting input from the user.
SYNTAX:
scanf(“format”,&var);
format represent the what type specifier ?
& represent the where the input to be stored.
var is a variable.
Ex: scanf(“%s”,&a);
Format specifier variable
7.
8.
9. UNFORMATTED I/O
getchar() - is used to read a single character.
General form:
var_name=getchar ();
putchar() – is used to write a character.
General form:
putchar(var_name);
10.
11.
12. To read and write a single character we are using
getchar() , putchar() , %c.
To read and write a string we normally use %s . Another
way is gets() and puts().
gets() - This function is used to get a string.
puts() - This function is used to print a string.
General Form:
gets(variable);
puts(variable);
(or)
puts(“sting to be displayed”);
13.
14.
15. STRING FUNCTIONS
In C there are predefined string functions or string
manipulation.
common string function
* strcat() – combine two strings
* strcmp() – compare two strings
* strcpy() – copy two strings
* strupr() – convert to uppercase
* strlen() – length of the string
* strlwr() – convert to lowercase
<string.h> header file is used to use these string functions.
19. Other string functions
strncpy() - it copies the source string to the destination
string
SYNTAX:
strncpy( dest , source , n);
dest – where the content to be copied.
source – Thus is the string to be copied
n – number of character to be copied from the
source.
20. strncmp() - compares the first n bytes of string1 and
string2.
SYNTAX:
strncmp(string1,string2,n);
string1 – first string
string2 – second string
n - maximum number of characters to be
compared
strstr() – used to find the substring is present or not.
SYNTAX:
strstr(string1,string2);
21. strncat() - It is used to combine two strings. combine the
first n bytes of string1 and string2.
strncat(string1,string2,n);
string1 – first string
string2 – second string
n - maximum number of characters to be
combined.