C Programming Tutorial
About Lesson

C Input/Output (I/O)

In this tutorial, we will learn to use printf() function to display output to the user, and scanf() to take output from the user.


Input and Output

C is a small language, and the “core” of C does not include any Input/Output (I/O) functionality. This is not something unique to C, of course. It’s common for the language core to be agnostic of I/O.

In the case of C, Input/Output is provided to us by the C Standard Library via a set of functions defined in the stdio.h header file.

You can import this library on top of your C file:

#include <stdio.h>

Input means to provide the program with some data to be used in the program and Output means to display data on screen or write the data to a printer or a file.

C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.


This library provides us, among many other functions:

  • printf()
  • scanf()
  • sscanf()
  • fgets()
  • fprintf()

Before describing what those functions do, I want to take a minute to talk about I/O streams.


Standard Files

We have 3 kinds of I/O streams in C:

  • stdin (standard input)
  • stdout (standard output)
  • stderr (standard error)

With I/O functions we always work with streams. A stream is a high level interface that can represent a device or a file. From the C standpoint, we don’t have any difference in reading from a file or reading from the command line: it’s an I/O stream in any case.

That’s one thing to keep in mind.

Some functions are designed to work with a specific stream, like printf(), which we use to print characters to stdout. Using its more general counterpart fprintf(), we can specify the stream to write to.

Since I started talking about printf(), let’s introduce it now.


printf() – Display Output

printf() is one of the first functions you’ll use when learning C programming.

In C programming, printf() function is used to print/display integer, float, character, string, etc. onto the output screen.


Example : Print Statement

#include <stdio.h>

int main() {
  // printf() displays the string inside quotation
  printf("Hello, World!");
  return 0;
}

Output

Hello World !

How does this program work?

  • In the first line we have include a header file named as stdio.h in angle brackets. Here, stdio stands for standard input output which contains the information related to input/output functions.
  • Then we have declare a main() function of int type.
  • Then we have used printf() library function to send/display formatted output to the screen which is written inside double quotations.
  • Then we have write return 0; statement which represent the “Exit status” of the program.

Note : In C programming, it is necessary to declare a main() function because the code execution begins from the start of the main() function.
To use the printf() function, we need to include stdio.h header file.
We have to use ; (semicolon) to terminate each statment in C programming.


#include <stdio.h>

int main() {
  int num = 36;
  //Represent the integer with %d format specifier
  printf("The given number is %d", num);
  return 0;
}

Output

The given number is 36

In the above example, we use %d format specifier to print int types.

Note : We cannot use the variable name directly to represent it in printf() or scan() function, that why format specifier of integer is used to represent integer variable in double quotes and then we have write the variable name which is represented by format specifier.


#include <stdio.h>

int main(){
  float num1 = 5.06;
  double num2 = 9.36;
  printf("The first number is %f \n", num1);
  printf("The second number is %lf", num2);
  return 0;
}

Output

The first number is 5.06 
The second number is 9.36

Note : In C programming, \n is a special format specifier which is used to generate a newline.


#include <stdio.h>

int main(){
  char grade = 'B';
  printf("Your grade is %c", grade);
  return 0;
  }

Output

Your grade is B

scanf() – Take Input

printf() is used as an output function. I want to introduce an input function now, so we can say we can do all the I/O thing: scanf().

In C programming, scanf() function is used to take input of the user read numeric data, character from keyboard.


Example : Take Integer Input

#include <stdio.h>

int main(){
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("Your entered number is %d",num);
  return 0;
}

Output

Enter a number : 12
Your entered number is 12

In the above example, we have used %d format specifier inside the scanf() function to take integer input from the user. When the user enters an integer, it is stored in the “num” variable.

Note : We have to use & with variable name inside scanf() because &variable_name refers to the address of the variable where the input of user is stored.


Example : Take Floating-point Input

#include <stdio.h>

int main(){
  float num1; double num2;
  printf("Enter first floating-point number: ");
  scanf("%f", &num1);
  printf("Enter second floating-point number: ");
  scanf("%lf ", &num2);
  printf("First number = %f \n", num1);
  printf("Second number = %lf", num2);
  return 0;
}

Output

Enter first floating-point number: 32.564
Enter another number: 13.8
First number = 32.564000
Second number = 13.800000

Example : Take Character Input

#include <stdio.h>

int main(){
  char grade;
  printf("Enter Grade: ");
  scanf("%c",&grade);
  printf("Your grade is %c.", grade);
  return 0;
}

Output

Enter Grade : A
You grade is A

Note : When we store a character in variable, then the character itself is not stored. Instead an integer value (ASCII value) is stored.
And if we use %d to display the character, it’s ASCII value is printed.


Example : Print ASCII Value

#include <stdio.h>

int main(){
  char ASCII_value;
  printf("Enter a character: ");
  scanf("%c", &ASCII_value);
  printf("You entered %c\n",ASCII_value);
  printf("ASCII value is %d.", ASCII_value);
  return 0;
}

Output

Enter a character: x
You entered x
ASCII value is 120.

Take Multiple Inputs and Print Multiple Outputs

#include <stdio.h>

int main(){
  int num; char grade;
  printf("Enter Integer and then a Character: ");
  scanf("%d %f", &num, &grade); // Taking multiple inputs
  printf("You have entered %d and %f", num, grade);
  return 0;
}

Output

Enter Integer and then a Character: 76 v 
You have entered 76 and v

getchar and putchar Functions

int getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in a loop in case you want to read more than one character.

int putchar() function puts the passed character on the screen and returns the same character. This function puts only single character at a time. You can use this method in the loop in case you want to display more than one character on the screen.

#include <stdio.h>

void main(){
  int a;
  printf("Enter a Character: ");
  a = getchar();
  putchar(a);
  return 0;
}

Output

Enter a Character: Algbly
A

gets() and puts() Functions

There are two popular library functions gets() and puts() provides to deal with strings in C.

gets: The char *gets(char *str) reads a line from stdin and keeps the string pointed to by the str and is terminated when the new line is read or EOF is reached. The declaration of gets() function is:

Syntax:

char *gets(char *str);

Where str is a pointer to an array of characters where C strings are stored.

puts: The function – int puts(const char *str) is used to write a string to stdout, but it does not include null characters. A new line character needs to be appended to the output. The declaration is:

Syntax:

int puts(const char *str);

where str is the string to be written in C.

#include <stdio.h>
void main(){
  char str[100];
  printf("Enter a String: ");
  a = gets(str);
  puts(str);
  getch();
  return 0;
}

Output

Enter a Character: Algbly
Algbly

Format Specifiers used in C language

List of the given datatypes and their format specifiers used in C programming :

Data Type Format Specifier
int %d
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double %Lf

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!