Input Output Statement in C

Welcome Folks, in this particular module we are going to discuss the Input Output Statement in C, till now we have seen a lot in this C Programming series, and also in the last module we have constructed a basic structure of the C program also, in this we will move one step higher and step towards the more practical approach.

So, gear up with your energies, and let’s move into the depth of this module,

Input Output Statement in C

Input Output Statement in C

Input-Output simply means to give some data or information to the computer so that it can perform various types of actions and produces the desired result, and this result is termed as output. Recall the previous module Hello World Program in C, in that the program was simply printing Hello World on the screen without taking any input from the user, so in that hello world was the output.

Now suppose you make a coffee, in that you give milk, coffee powder, sugar, 5 minutes in a gas stove, so all these are the inputs and the output is your coffee ready. Simply if you want to perform any action in life that you have to produce some input from your side then only you will get the desired output.

scanf() and printf() are the two functions that help in taking input from the user and produce the desired result as output respectively. scanf sends the formatted input to the respective variable and stores there, whereas printf sends the formatted output statement to the screen.

Let’s see some example code to get a clearer idea:

#include <stdio.h>   
int main()
{
    printf("Welcome to the usemynotes.com");
    return 0;
}

The output of the above program:

Welcome to the usemynotes.com

How does the above Program work?

We have written a simple program that just simply prints the given sentence as the output. Let’s see the working of the above program.

  • We know that all the execution of all valid c Programs starts from the main function. Therefore, the program must contain the main function.
  • printf() is the function that gives the output in the screen, so in this program, it simply prints the string or sentence inside double quotations.
  • To use this printf() function, we need to include stdio.h header file, as it stands from the standard input-output library.
  • return 0; statement indicates the exit status of the program and also tells that the program is executed successfully, it’s optional as if you are using int main() then you have to give return 0 at the last, and if you are using void main() then you don’t have to use return 0 statement.

Let’s take another example:

In this example, we will be using the scanf() function which will take the integer as input from the user and will print the same integer as output with the help of printf() function.

#include <stdio.h>   
int main()
{
    int num ;
    printf("Enter the Integer: ") ;
    scanf(“%d”, &num) ;
    printf(“The Entered Integer is: %d”, num );
    return 0 ;
}

The output of the above program:

Enter the Integer: 30
The Entered Integer is: 30

In the above program, we are asking the user to enter an integer, and then the user will enter the integer that integer will be stored in the variable num, after that it will print the same integer which the user integer.

You all must be wondering it as a little complicated program as we have used many different declarations but that declaration stands for data types and variables which we will be seeing in the next module, as far now you should know the use of scanf() and printf() function and their working.

In the above examples, you all must have noticed one thing and i.e., “%d, %f, %c” and all. So what is this and why we have used this…

Now comes the concept of Format Specifier.

Format Specifier

These are all the format specifiers (%d, %f, etc.). The format specifiers are used in C Programming for performing different input and output operations. Using this concept the compiler can understand that what type of data i.e., integer, character, string, decimal numbers, etc., is in a variable during taking input using the scanf() function and printing using printf() function.

Let’s see the list of Format specifier available:

Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar to %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i Unsigned integer
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character

These are the list of format specifiers available in C, which are used with different data types, and will see them more in the upcoming modules.

Hope you all are really excited about the next modules and learned a lot from this one. Stay connected.

Until then, Enjoy Coding and Happy Learning!