Sacnf Function In C/C++ Language
Before we use the scanf function we must be know that:
This function is usually used as an input statement in c language.
The format string must be enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s).
Format string (Format specifier) tell to complier that which type of variable is accepted.
The argument list contains a list of variables each separated by comma.
The number of argument is not fixed; however corresponding to each argument there should be a format specifier. Inside the format string the number of argument should tally with the number of format specifier.
For example you can get more then one value with single scanf statement but for each value a separate format string (Format specifier) is used.
Now we take an example
Example 1:
in this example we get tow value from user at run time with separate sacnf statement and then display on the screen with printf statement.
#include #include int main() { int x,y; sacnf("%d",&x); scanf("%d",&y); printf("Value of x and y is=%d and %d",x,y); return 0; }
Example 1:
in this example we get tow value from user at run time with single sacnf statement and then display on the screen with printf statement.
#include #include int main() { int x,y; sacnf("%d %d",&x,&y); printf("Value of x and y is=%d and %d",x,y); return 0; }
Note : if you are using turbo c editor the don't use this include statement but if you are using any c++ editor the its must be declare before the writing the main function.
Example 3:
we take two number from user and add these two number and then give the result. Result display on the screen.
for this purpose we must declare three variable number1 as integer and number two as integer and result as integer
int number1 , number2 , result ;
Now give the message to user that enter first number
printf("Enter the First Number \n") ; (Remember that \n is used as escape sequences which transfer the control on next line after displaying this message.
Now Get the first number as integer form user and store the number in variable number1.
scanf("%d" , &number1) ;
Now give the Other message to user that enter the Second number
printf("\n Enter the Second Number \n") ; (Remember that \n is used as escape sequences which transfer the control on next line after displaying this message.
Now Get the Second number as integer form user and store the number in variable number2.
scanf("%d" , &number2) ;
Now add these two number and store the result in third variable "Result"
result=number1+number2 ;
Now display the result on screen
printf("Your result is=%d", result) ;
Complete code:
#include #include int main() { int number1 , number2 , result ; printf("Enter the First Number \n") ; scanf("%d" , &number1) ; printf("\n Enter the Second Number \n") ; scanf("%d" , &number2) ; result=number1+number2 ; printf("Your result is=%d", result) ; return 0;
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++