Chapter 7. INPUT-OUTPUT IN C

input

Contributed by: Shehrevar Davierwala

When we are saying Input that means we feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.

When we are saying Output that means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output the data on the computer screen.

  1. Name the the functions used for input and output in C ?

Functions printf() and scanf() are the most commonly used to display out and take input respectively. Let us consider an example:

#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

Explanation:
 Every program starts from main() function.
 printf() is a library function to display output which only works if #include<stdio.h>is included at the beginning.
 Here, stdio.h is a header file (standard input output header file) and #include is command to paste the code from the header file when necessary. When compiler encounters printf()function and doesn’t find stdio.h header file, compiler shows error.
 return 0; indicates the successful execution of the program.

A program to print integer on screen:

#include<stdio.h>
int main()
{
int c=5;

printf(“Number=%d“,c);
return 0;
}
Output
Number=5

Inside quotation of printf() there, is a conversion format string “%d” (for integer). If this conversion format string matches with remaining argument, i.e, c in this case, value of c is displayed.

2. Write a program to accept an integer from the user and display it on the screen.

#include<stdio.h>
int main()
{ int c;
printf(“Enter a number\n”);

scanf(“%d”,&c);

printf(“Number=%d”,c);
return 0;
}
Output
Enter a number
4  <—————– ( entered by the user )
Number=4

The scanf() function is used to take input from user. In this program, the user is asked an input and value is stored in variable c. Note the ‘&’ sign before c. &c denotes the address of c and value is stored in that address.

3. Write a program to accept and display floating number.

#include <stdio.h> int main()
{
float a;
printf(“Enter value: “);

scanf(“%f”,&a);
printf(“Value=%f”,a); //%f is used for floats instead of %d return 0;
}

Output
Enter value: 23.45
Value=23.450000
Conversion format string “%f” is used for floats to take input and to display floating value of a variable.

4. Write a program to accept and display a character in C

#include <stdio.h>
int main()
{
char var1;
printf(“Enter character: “);

scanf(“%c”,&var1);

printf(“You entered %c.”,var1);
return 0;
}
Output
Enter character: g
You entered g.
Conversion format string “%c” is used in case of characters.

5. Write a program to accept character from user and display its ASCII value.

#include <stdio.h>
int main()
{
char var1;
printf(“Enter character: “); scanf(“%c”,&var1);
printf(“You entered %c.\n”,var1);
/* \n prints the next line(performs work of enter). */

printf(“ASCII value of %d”,var1);

return 0;
}
Output:

Enter character

g

You entered

g

ASCII value of 103
When, ‘g’ is entered, ASCII value 103 is stored instead of g.

6. Write a program to accept ASCII value from user and print character

#include <stdio.h> int main()
{
int var1=69;
printf(“Character of ASCII value 69: %c”,var1); return 0;
}
Output
Character of ASCII value 69: E

The ASCII value of ‘A’ is 65, ‘B’ is 66 and so on to ‘Z’ is 90. Similarly ASCII value of ‘a’ is 97, ‘b’ is 98 and so on to ‘z’ is 122.

7. How to format input and output in C ?

Data can be entered & displayed in a particular format. Through format specifications, better presentation of results can be obtained.

Variations in Output for integer & floats:
#include<stdio.h>
int main()
{
printf(“Case 1:%6d\n”,9876);
/* Prints the number right justified within 6 columns */

printf(“Case 2:%3d\n”,9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified */
printf(“Case 3:%.2f\n”,987.6543);
/* Prints the number rounded to two decimal places */ printf(“Case 4:%.f\n”,987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */

printf(“Case 5:%e\n”,987.6543);
/* Prints the number in exponential notation (scientific notation) */

return 0;
}

Output
Case 1: 9876
Case 2:9876
Case 3:987.65
Case 4:988
Case 5:9.876543e+002

Variations in Input for integer and floats:
#include <stdio.h> int main()
{
int a,b;
float c,d;
printf(“Enter two intgers: “);
/*Two integers can be taken from user at once as below*/

scanf(“%d%d”,&a,&b);
printf(“Enter integer and floating point numbers: “);
/*Integer and floating point number can be taken at once from user as below*/ scanf(“%d%f”,&a,&c);
return 0;
}

Similarly, any number of inputs can be taken at once from user.

 

—————– EXERCISE: —————————
1. To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h> float a=3.14;
double b=3.14;
A. printf(“%f %lf”, a, b);
B. printf(“%Lf %f”, a, b);
C. printf(“%Lf %Lf”, a, b);
D. printf(“%f %Lf”, a, b);
2. To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h> float a;
double b;
A. scanf(“%f %f”, &a, &b);
B. scanf(“%Lf %Lf”, &a, &b);
C. scanf(“%f %Lf”, &a, &b);
D. scanf(“%f %lf”, &a, &b);

3. For a typical program, the input is taken using.
A. scanf
B. Files
C. Command-line
D. None of the mentioned

4. What is the output of this C code?
#include <stdio.h>
int main()
{ int i = 10, j = 2;
printf(“%d\n”, printf(“%d %d “, i, j));
}
A. Compile time error
B. 10 2 4
C. 10 2 2
D. 10 2 5

5. What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 10, j = 3; printf(“%d %d %d”, i, j);
}
A. Compile time error
B. 10 3
C. 10 3 some garbage value
D. Undefined behavior

6. What is the output of this C code?
#include <stdio.h>
int main()
{ int i = 10, j = 3, k = 3;
printf(“%d %d “, i, j, k);
}
A. Compile time error
B. 10 3 3
C. 10 3
D. 10 3 somegarbage value

7. The syntax to print a % using printf statement can be done by.
A. %
B. %
C. ‘%’
D. %%

8. What is the output of this C code?
#include <stdio.h> int main()
{ int n;
scanf(“%d”, n);
printf(“%d\n”, n);
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. Depends on the standard

9. What is the output of this C code?
#include <stdio.h>
int main()
{ short int i;
scanf(“%hd”, &i);
printf(“%hd”, i);
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. None of the mentioned

10. In a call to printf() function the format specifier %b can be used to print binary equivalent of an integer.
A. True
B. False

11. Point out the error in the program?
#include<stdio.h>
int main()
{
char ch;
int i;
scanf(“%c”, &i);
scanf(“%d”, &ch);
printf(“%c %d”, ch, i);
return 0;
}
A. Error: suspicious char to in conversion in scanf()
B. Error: we may not get input for second scanf() statement
C. No error
D. None of above

12. Which of the following is NOT a delimiter for an input in scanf?
A. Enter
B. Space
C. Tab
D. None of the mentioned

 

 

 

2 comments to “Chapter 7. INPUT-OUTPUT IN C”

You can leave a reply or Trackback this post.

  1. Gaurav Singh Bisht - 14/02/2017 at 6:41 am Reply

    Where we can check the answers of Multiple choice questions.

    • admin - 15/02/2017 at 2:32 pm Reply

      Thanks for writing to us.

      We have not yet stated with live quiz this is only practice set but soon consider your suggestion.

Leave a Reply

Your email address will not be published.