Rohini 66913029385
Rohini 66913029385
Rohini 66913029385
4) getche( )
5) fgets( ) 4)fputs( )
6)fscanf( ) 5)fprint( )
Input means to provide the program with some data to be used in the program
Output means to display data on screen or write the data to a printer or a file.
input- getchar()
output- putchar()
The int getchar(void) function reads the next available character from the screen and returns
it as an integer. This function reads only single character at a time.
The int putchar(int c) function puts the passed character on the screen and returns the same
character. This function puts only single character at a time.
program
#include <stdio.h>
int main( ) {
output
int c; $./a.out
Enter a value : this is DS class
printf( "Enter a value :"); You entered: t
c = getchar( );
return 0;
}
The gets( ) function reads a line from stdin into the buffer pointed to by s until either a
terminating newline or EOF (End of File).
The puts( ) function writes the string 's' and 'a' trailing newline to stdout.
Program
return 0;
}
scanf()
scanf() is a predefined function in "stdio.h" header file. It can be used to read the input value
from the keyword.
Example
double d;
char c;
long int l;
scanf("%c%lf%ld",&c&d&l );
o Printf
o Printf is a predefined function in "stdio.h" header file, by using this function, we can
print thedata or user defined message on console or monitor. While working with
printf(), it can take any number of arguments but first argument must be within the
double cotes (" ") and every argument should separated with comma ( , ) Within the
double cotes, whatever we pass, it prints same, if any format specifies are there, then
value is copied in that place.
Program
#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("C Programming"); //displays the content inside quotation
return 0;
}
Output
C Programming
Program(integer and float)
#include <stdio.h>
#include <conio.h>
void main();
{
int a;
float b;
clrscr();
printf("Enter any two numbers: ");
scanf("%d %f",&a,&b);
printf("%d %f \n",a,b);
getch();
}
Program
#include <stdio.h>
int main()
{
return 0; Output
}
4 digit integer right justified to 6 column: 9876
4 digit integer right justified to 3 column: 9876
Floating point number rounded to 2 digits: 987.65
Floating point number rounded to 0 digits: 988
Floating point number in exponential form: 9.876543e+02
The fgets() function takes three arguments, first is the string read from the file, second is size
of string(character array) and third is the file pointer from where the string will be read.
Example File*fp;
Str[80];
fgets(str,80,fp)
Example program
#include<stdio.h>
void main()
{
FILE *fp;
char str[80];
fp = fopen("file.txt","r"); // opens file in read mode (“r”)
while((fgets(str,80,fp))!=NULL)
printf("%s",str); //reads content from file
fclose(fp);
}
Data in file...
C is a general-purpose programming language.
It is developed by Dennis Ritchie.
The fputs() function takes two arguments, first is the string to be written to the file and
second is the file pointer where the string will be written.
Syntax:
fputs(char str[], FILE *fp);
#include < stdio.h >
int main ()
{
FILE *fp;
fp = fopen("proverb.txt", "w+"); //opening file in write mode
fputs("Cleanliness is next to godliness.", fp);
fputs("Better late than never.", fp);
fputs("The pen is mightier than the sword.", fp);
fclose(fp);
return(0);
}
Output
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
int roll;
char name[25];
fp = fopen("file.txt","r");
printf("\n Reading from file...\n");
while((fscanf(fp,"%d%s",&rollno,&name))!=NULL)
printf("\n %d\t%s",rollno,name);//reading data
fclose(fp);
}
Output :
Examp
le
progr
am
#incl
ude<s
tdio.
h>
void main()
{ Output
FILE *fp;introll;
char name[25]; 6666
fp = fopen("file.txt","w"); john
scanf("%d",&roll);
scanf("%s",name);
fprintf(fp,"%d%s%",roll,name);
close(fp);
}