C语言中的printf(),scanf()和注释以及示例

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.

C Program to Calculate Simple Interest

In this program I am assuming that you must know the formula and working of simple interest in mathematics. So I will not explain that formula to you.
Lets try to understand this program step by step.
1. First two statements are comments.

Comments in C

Comments are generally used to increase the readability of program. At present we are making very small programs. But when we develop big programs then the program has to go through a long process of testing and debugging. Comments are not the part of program code and are not read by compiler.
It is very important to write comments in programs. So that other programmers can also read and understand your program easily. Writing comments is also a good programming practice. Start writing comments in the programs from the beginning itself.
C allows two types of comments
a. Single line comment: // first type of comment
b. Multiline comment: /* second type of comment*/
Single line comment is used to write comments in one line only. Multiline comment is used to write comments in multiple lines. All things that comes in between /* and */ is considered as comment. We can use anyone according to requirement. 
2. After that next three instructions are same which includes C pre-processor directives, main() function, declaration of integer variables.
3. In the fourth instruction we have declared float variable r and si. Because rate of interest can be a floating point number. And to stay on safe side we also declared si variable as float. As the answer may come in floating point.
4. After that using printf() function we print a message to instruct the user to insert the values of p, n and r.
5. Using scanf() we are taking input from the user. Checkout we have used %f format specifier for r variable. We have declared r as float variable. So we have to use %f format specifier to print as well as receive values in r variable.
6. Now in the next statement we have calculated the simple interest using the formula.
7. In the last we have print the answer using printf() function. Notice we have used %f format specifier there. As si is also a float variable.
So these are the basic use of printf() and scanf() functions. These are one of the most used functions in C language. So you can estimate the importance of them. Now you can make 100s of programs by using these two functions.
Try making these programs yourself (take values from user)
1. Make a program to add two numbers.
2. Make a program which will convert distance in km to meter.

翻译自: https://www.thecrazyprogrammer.com/2014/12/printf-scanf-and-comments-in-c-with-example.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值