Input and Output Statements in C

In our earlier post we introduced you to C language.Today we shall learn about input and output statements in C.These statements are used for intake of data from the user and display the output after processing to the user.

First we shall learn about the output statements present in C language.The main and important output statement present in C is “printf”.This is a keyword and we include the stdio.h header file in our program.The syntax of the printf statement is as follows:

printf(“———“);

In the place of dashed line you can place your message for the user.There are other types of input statements in C as substitute for “printf” statement.The other output statements are:
1. puts().
2. putchar().
1.When we need to display strings then we use puts() function to display.
2.When we need to display character by character then we use putchar() method to display.

Let us write a program to demonstrate the use of printf().

#include<stdio.h>
void main()
{
  printf("Welcome to the world of Programmming");
}

OUTPUT:
Welcome to the world of Programming
So….we have learnt how to display our message to the user.Let us go to the next part of our today’s tutorial….i.e about input statements.
The main and most important input statement used in C programming is “scanf()” statement.The syntatx of the “scanf()” statement is:

scanf(“**”,&var_name);

In the above syntax…… in the place of ** we need to enter a suitable format specifier.Format specifier is different for each of the datatype present in C.
1.int —- %d
2.char —- %c
3.string —- %s
4.Float —- %f.
These are format specifier and we need to remember them till the extinct of C language. Let us consider that a user is giving a number is input and we are going to store it a variable(named as num).Then the scanf() statement is as follows:
scanf(“%d”,&num);
This is how we write a scanf() statement.The other types of input statements are:
1.gets()
2.getchar().

These functions work similar to puts() and putchar().

Write a C Program such that we take a number as an input from the user and display the same number to him.

#include<stdio.h>
void main()
{
  int num;  //declaration of a variable
  printf("Please enter any number:") // using printf() to display the message
  scanf("%d",&num); // using scanf() statement and format specifiers
  printf("Given number by the user is:%d",num);

In the last printf() statement we again used format specifier to display the number present in the variable num……..From this we can say that we can use format specifiers in both printf() and scanf() statements.

OUTPUT:
Please enter any number:6
Given number by the user is:6
So,this is the end of today’s tutorial and next tutorial we shall learn about data types.

Advertisements

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in C and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s