Nothing Special   »   [go: up one dir, main page]

Rohini 66913029385

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

Managing Input and Output operations

Constructs for getting input Constructs for displaying output


1)scanf( ) 1)scanf( )
2)putchar() 2)getchar()
3)puts() 3)gets( )

4) getche( )

5) fgets( ) 4)fputs( )
6)fscanf( ) 5)fprint( )

C Input and Output

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.

1. Single character input and output[getchar( ) and putchar()]

 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( );

printf( "\nYou entered: ");


putchar( c );

return 0;
}

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

2. String input and output[gets() and puts()

 Input--- gets (str)


 Output---puts (str)

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

#include <stdio.h> Output


int main( ) {
$./a.out
char str[100]; Enter a value : this is DS class
You entered: this is DS class
printf( "Enter a value :");
gets( str );

printf( "\nYou entered: ");


puts( str );

return 0;
}

3.Formatted Input [ scanf ( ) ] and Formatted Output [ printf ( ) ]


Specifier Meaning
%c – Print a character
%d – Print a Integer
%i – Print a Integer
%u-- Unsigned int
%ld-- Long int
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%lf --Double
%lf-- Long double
%o – Print octal 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

scanf()
scanf() is a predefined function in "stdio.h" header file. It can be used to read the input value
from the keyword.

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY
Syntax of scanf() function
1. & ampersand symbol is the address operator specifying the address of the variable
2. control string holds the format of the data
3. variable1, variable2, ... are the names of the variables that will hold the input value.

scanf("control string", &variable1, &variable2, ...);


Example
int a;
float b;
scanf("%d%f",&a,&b);

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();
}

Output: Enter any two numbers:10 3.5


10
3.5

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

Program
#include <stdio.h>
int main()
{

int integer = 9876;


float decimal = 987.6543;

printf("4 digit integer right justified to 6 column: %6d\n", integer);

printf("4 digit integer right justified to 3 column: %3d\n", integer);

printf("Floating point number rounded to 2 digits: %.2f\n",decimal);

printf("Floating point number rounded to 0 digits: %.f\n",987.6543);

printf("Floating point number in exponential form: %e\n",987.6543);

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

FILE INPUT and OUTPUT

4. File string input and output using fgets( )and fputs( )


The fgets() function
The fgets() function is used to read string(array of characters) from the file.
Syntax fgets(char str[],int n,FILE *fp);

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.

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

C is a general-purpose programming language.


It is developed by Dennis Ritchie.

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY
Output :

The fputs() function


The fputs() function is used to write string(array of characters) to the file.

 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

Cleanliness is next to godliness.


Better late than never.
The pen is mightier than the sword.

4. File string input and output using fgets( )and fputs( )


The fscanf() function
The fscanf() function is used to read mixed type(characters, strings and integers) form the file.
The fscanf() function is similar to scanf() function except the first argument which is a file pointer
that specifies the file to be read.
Syntax: fscanf(FILE *fp,"format-string",var-list);
Example program

#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 :

Reading from file...


6666 keith
7777 rose

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C


ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

The fprintf() function:


 The fprintf() function is used to write mixed type(characters, strings and
integers) in the file.
The fprintf() function is similar to printf() function except the first argument
which is a filepointer specifies the filename to be written.
Syntax
fprintf(FILE *fp,"format-string",var-list);

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);
}

UNIT-1 EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C

You might also like