Input in C

by | Aug 14, 2021 | C

Home » C » Input in C

Introduction

We have seen how to write the programs with default inputs without taking inputs from users. But what if a programmer wants the user to enter the data with their wish? In this article we will explore various ways to take input in C.

Taking Input in C

Let’s write some code for better understanding.

#include<stdio.h>
int main ( )
{
   int z;
   printf (“enter an integer value”);
   scanf (“%d”, &z);
   printf (“Integral value entered by the user is: %d\n”, a);
   return 0;
}

Explanation of Code

int z; It is a declaration statement that declares that the variable ‘z’ is an integer. This declaration allows our computer to allocate some space in its memory for ‘z’.

Space in C Memory

The next statement will get printed on the screen since it is written inside the printf function.

scanf (“%d”, &z);  scanf is also a predefined function like printf, which takes values from the user through the keyboard. Like printf, the definition of scanf function is also stored in the ‘stdio.h’ library.

%d is used for integers in C. It is written inside scanf so that the scanf function will take an integer value from the keyboard.

&z means at the address of ‘z’. As already stated, z is allocated with some space in the memory. This memory also has an address value, and &z is pointing to that address.

address in c memory

So, we have successfully stored the value given as input by the user at the address of ‘a’. Thus, variable ‘a’ will store the value entered by the keyboard.

value stored in c variable

‘2’ is the value stored in variable z that is given as input by the user.

In printf (“Integral value entered by the user is: %d\n”, z); everything got printed on the screen as it is written inside double quotes except %d.

As we know, %d is used for integers, so the compiler will search for a value for ‘%d’, which is stored in variable ‘z’ written after comma (,). So, the value of ‘z’ will be printed in place of ‘%d’.

Example

We now know how to take input from users and print accordingly on the screen. Try to make some changes to the codes for better understanding.

Let us practice one more program:

#include<stdio.h>
int main()
{
            printf("enter two values:");
            int a;
            int b;
            scanf("%d %d",&a,&b);
            printf("Entered values are: %d and %d\n",a,b);
            int s;
            s=a+b;
            printf("sum of above values is %d",s);
            return 0;
}

Explanation of Example

In the above code, we have printed a statement giving a message to the user asking for some values.

After that, two variables, a and b, are declared as integers.

We are taking values from the user by ‘scanf’.

scanf(“%d %d”,&a,&b); There are two ‘%d’ inside ‘scanf’ to take two integers and store them at the address of ‘a’ and ‘b’, respectively.

printf(“Entered values are: %d and %d\n”, a,b); two %d are used in this statement, and their values will get printed on the screen that is stored in variables ‘a’ and ‘b’ respectively written after comma(,).

Moreover, the use of \n in this print statement will create a new line after the execution of this statement.

s=a+b; We have declared a new integer variable‘s’, equal to the sum of the values of ‘a’ and ‘b,’ i.e. a+b.

printf(“sum of above values is %d”,s); This statement will get printed as it is, and the value of s will replace ‘%d’ (i.e. the sum of a and b).

Output

And here we have the output of the above code:

Input in C

Thus, we have studied how to take user input and perform some basic operations on the inputs entered.

Do practice questions related to these topics to get a good command of these concepts.

Note: While writing the program, make sure to run the code from time to time instead of compiling it in the end. This will help to find errors if any are made.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author