printf(), scanf() and comments in C with example

Hello everybody, I hope you want to have done the sensible take a look at of our previous programs. bear in mind sensible information is utmost vital in learning c language.

Anyways until we’ve lined the fundamental use of printf() operate by that we are able to print values on the screen. nowadays we’ll find out how to require values from the user.

scanf() in C

scanf() is employed to require knowledge from the user. until currently we’ve wrote programs within which we have a tendency to declared variables with some values. however in follow we’d like those programs that are general enough to form computations.

So with the assistance of scanf() operate currently we’ll build a general program for multiplication of 2 numbers. during this program, we’ll raise the user to enter the values.

#include<stdio.h>
 
void main()
{
 int a,b,c;
 printf("Enter two values to do multiplication");
 scanf("%d%d",&a,&b);
 c=a*b;
 printf("Your answer is %d",c);
}

Output

Inter two values to do multiplication5 
our answer is 30.

Now let’s try and perceive this program.

initial 2 directions are same like our previous programs.

within the third instruction, we have a tendency to are declaring 3 variables of number sort.

within the fourth instruction, we have a tendency to are printing the statement victimization print() operate.

within the fifth instruction, we have a tendency to are taking input from the user through scanf() operate.

In this scanf() operate we’ve done 2 things.

a. we’ve given the format intellect you must instruct the compiler that we would like to input number worth.
b. we’ve used punctuation (&) that is additionally known as “address of operator”. By victimization this we have a tendency to instruct the compiler we would like to store that input therein variable (a and b).

Why can we use punctuation operator (&)?

As I even have same already it’s an “address of operator”. By victimization this operator we have a tendency to specify the address of a variable to the compiler.

A bit of confusion..? Ok, check out the instance below.
Suppose we’ve used &a. currently compiler can receive the input and move to the address of a (which maybe something like 7635). afterward it’ll store that worth on its specific address. That’s it.
Let’s write another program that is slightly difficult i.e. program to calculate interest.

C Program to Calculate interest

In this program, I’m presumptuous that you simply should grasp the formula and deal of straightforward interest in arithmetic. therefore i’ll not make a case for that formula to you.

/*Program to calculate simple interest
JustTechReview date 21/12/14*/
 
#include<stdio.h>
 
void main()
{
 int p,n; //Here p is principle amount and n is number of years
 float r,si; //Here r is rate of interest and si is simple interest
 printf("Enter the values of p,n and r");
 scanf("%d%d%f",&p,&n,&r);
 si=(p*n*r)/100;
 printf("Simple interest is %f",si);
}

Output

Inter the values of p,n and ri 1000
2
2.5
simple intrest is 50.000000 

Let’s try and perceive this program step by step.

initial 2 statements are comments.

Comments in C

Comments are usually accustomed to increase the readability of the program. at this time we have a tendency to are creating terribly little programs.

however after we develop massive programs then the program must bear an extended method of testing and debugging. Comments don’t seem to be a part of program code and are not scanned by the compiler.

It is important to jot down comments in programs. in order that different programmers also can scan and perceive your program simply.

Writing comments is additionally honest programming follow. begin writing comments within the programs from the start itself.

C permits 2 forms of comments

a. Single line comment: // initial form of comment
b. Multiline comment: /* second form of comment/ Single line comment is employed to jot down comments in one line solely. Multiline comment is employed to jot down comments in multiple lines. All things that comes in between / and */ is taken into account as comment. we are able to use anyone in keeping with demand.

afterward next 3 directions are same which has C preprocessor directives, main() operate, declaration of number variables.

within the fourth instruction, we’ve declared float variable r and si. as a result of the rate of interest may be a floating purpose range. And to remain on safe facet we have a tendency to additionally declared si variable as a float. because the answer could are available in floating purpose.

afterward victimization printf() operate we have a tendency to print a message to instruct the user to insert the values of p, n and r.

victimization scanf() we have a tendency to are taking input from the user. Checkout we’ve used you look after format intellect for r variable. we’ve declared r as float variable.

therefore we’ve to use you look after format intellect to print further as receive values in r variable.

currently within the next statement, we’ve calculated the easy interest victimization the formula.

within the last we’ve print the solution victimization printf() operate. Notice we’ve used you look after format intellect there.

As si is additionally a float variable.

So these are the fundamental use of printf() and scanf() functions. These are one in all the foremost used functions in C language. therefore you’ll be able to estimate the importance of them.

currently you’ll be able to build 100s of programs by victimization these 2 functions.

Try creating these programs yourself (take values from the user)

build a program to feature 2 numbers.

build a program which is able to convert distance in klick to the meter.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

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

9 − two =

Recent Content