0 like 0 dislike
109 views
in Computer Science by (1.0m points)
Explain printf() function with examples.

1 Answer

0 like 0 dislike
by (1.0m points)
Hello everyone, I hope you must have done the practical test of our previous programs. Remember practical knowledge is utmost important in learning c language.

Anyways till we have covered the basic use of printf() function by which we can print values on the screen. Today we will learn how to take values from the user.

Note: Read previous article to know more about printf() function: First C Program – Hello World

scanf() in C

scanf() is used to take data from the user. Till now we have wrote programs in which we declared variables with some values. But in practice we need those programs which are general enough to make computations.

So with the help of scanf() function now we will make a general program for multiplication of two numbers. In this program we will ask the user to enter the values.

Now lets try to understand this program.

1. First two instructions are same like our previous programs.

2. In the third instruction we are declaring three variables of integer type.

3. In the fourth instruction we are printing the statement using printf() function.

4. In the fifth instruction we are taking input from the user through scanf() function.

In this scanf() function we have done two things.

a. We have given the format specifier %d to instruct the compiler that we want to input integer value.

b. We have used ampersand (&) which is also called “address of operator”. By using this we instruct the compiler we want to store that input in that variable (a and b).

Why do we use ampersand operator (&)?

As I have said already it is a “address of operator”. By using this operator we specify the address of variable to the compiler.

A bit confusion..? Ok, checkout the example below.

Suppose we have used &a. Now C compiler will receive the input and go to the address of a (which can be anything like 7635). After that it will store that value on that particular address. That’s it.

Lets write another program which is slightly complicated i.e. program to calculate simple interest.

Related questions

0 like 0 dislike
1 answer 61 views
0 like 0 dislike
1 answer 66 views
0 like 0 dislike
1 answer 68 views
0 like 0 dislike
1 answer 66 views
0 like 0 dislike
1 answer 53 views
0 like 0 dislike
1 answer 99 views
0 like 0 dislike
1 answer 78 views
0 like 0 dislike
1 answer 81 views
0 like 0 dislike
1 answer 69 views
0 like 0 dislike
1 answer 84 views
Welcome to Free Homework Help, where you can ask questions and receive answers from other members of the community. Anybody can ask a question. Anybody can answer. The best answers are voted up and rise to the top. Join them; it only takes a minute: School, College, University, Academy Free Homework Help

19.4k questions

18.3k answers

8.7k comments

16.6k users

Free Hit Counters
...