Let’s see how show variable in printf and take input in scanf
scanf();
We take a input from the user using scanf. The input that we got from the user will be stored in a variable.
Example:
int a;printf(“Enter Age : “);scanf(“%d”,&a);
Here, we have to first declare the variable in which we want to store the input. I have used a integer variable as per the need (I mean, I have to get the age, so age is a non decimal numeric value, so integer datatype is best match for it).
We used “%d” in scanf, and it is reffered to integer value. Simply, if you want a one integer value from the user, you can write “%d”. After the comma, we have to write a varible where we have to store the data.
I used “&” before variable name. It is because, we are assigning the address of that variable to that value. Don’t take too load, its like, variable “a” address is 101294, and using &(reference operator) we refer that address to that variable. Now 101294 block in the memory is assigned to “a” and if we write any value in it, it will store it in that address.
Output:
Enter Age : 46
Now ’46’ is stored in ‘a’ in the memory block 101294.
You will not see the memory block, you just have to see that ‘a’ is 46. That’s it.
printf();
printf is kinda same as scanf, the core difference is that it displays the variable, I mean their syntax is same.
Example
int a;printf(“Enter Age”);scanf(“%d”,&a);printf(“You entered %d”,a);
In the last line, we entered “%d” that is reffering to compiler to show the value that with pass after comma. Simple.
The question your minds would be, what if we want to show more than one value?
Answer:
printf(“Your Age : %d, Your Weight :%d”,a,b);
You can write many %d in printf, but to show them in proper manner, you have to write them in proper sequence. If the Age is in variable ‘a’ and you want that in first %d, you have to write it first after comma.
The second question in your mind would be, why not ‘&’.
That’s because, we have already reffered the address of ‘a’ while scanning it and now ‘a’ is the other name for 101294 in memory block, so on ‘&’ in printf statement.
Simple Concept, Still Very Important.
If you have any doubt, you can ask me out. 