What are the differences between | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What are the differences between

scanf(a) , scanf("%d",a) and scanf("%d",&a) ???

6th Jul 2018, 6:45 AM
[No Name]
[No Name] - avatar
10 Answers
+ 6
Saw Mee Tha Gay All the three working scanf(a) scanf("%d",a) scanf("%d",&a) can be explained in very simple way. First of all, scanf is defined in stdio.h. The only third one scanf("%d",&a) is valid in C.Though other two scanf(a) and scanf("%d",a) will work in program without any compilation error here. Its explained below here how. For examples, Consider now this , Example 1:- For scanf(a) and scanf("%d",&b) #include <stdio.h> int main(){ /* here i'm considering 'a' to be of int type.One can consider any type but needs to be defined it with right format specifier inside scanf */ int a,b; scanf(a); scanf("%d",&b); printf("The address a=%d b=%d",&a,&b); printf("\n\nThe value of a=%d b=%d",a,b); return 0; }
7th Jul 2018, 9:47 AM
D-Key
D-Key - avatar
+ 6
Saw Mee Tha Gay Input in console:- 45 54 Output:- The address a=1765439 b=1765487 The value of a=0 b=54 [Note:- The allocation of memoey will be random for addresses of both a and b during runtime] Example 2:- For scanf("%d",a) #include <stdio.h> int main() { int a; scanf("%d",a); printf("\n\n I am executed"); return 0; } input in console:- 67 Output:- No output WORKING EXPLANATION:- FUNCTION PROTOTYPE:- int scanf(const char*format,...); THE THREE DOTS ABOVE:- Now, the return of scanf is always integer and scanf is a variadic function which means it can take variable number of inputs against the variadic arguments (those three dots in scanf function prototype (...) ). WORKING OF SCANF:- scanf takes line of inputs from stdin.The stdin is input stream of bytes to buffer to read data from input console.
7th Jul 2018, 10:16 AM
D-Key
D-Key - avatar
+ 6
Saw Mee Tha Gay Now, The scanf read data from input console in string format because of the constant character format pointer and it stops at "%" implicitly or explicitly or \n or \t implicitly. It then converts the string format to required datatype variable as defined by user in scanf and reads value of from user input.Like scanf("%c",&a); EXAMPLE 1:- scanf(a) is of integer return type and since format pointer doesnot find any format specifier inside its string literal " ".It equates the scanf(a) to 0 and its return is 0.So scanf(a) is equivalent to scanf(0).And these both will work. So in simple words scanf will not scan any user input but if declared this way its takes the scanf to be valid with a return of 0 during runtime. Hence it doesnot turn out to be a compilation error but provides warning to user as its meaningless to use in program. Second, scanf("%d",&b) will point the character format pointer to the format type of the value inside address b.So & address operator is needs to be used.
7th Jul 2018, 10:38 AM
D-Key
D-Key - avatar
+ 6
Saw Mee Tha Gay 👉 The & operator in scanf points the current pointer to the address b. And inside printf the value of b is derefernced by the format pointer to give back the value of b of its defined type and so b=54 gets printed on screen 👉 So printf and scanf are just reverse of each other in terms of working. ▶️ EXAMPLE 2:- 💻 Here the scanf("%d",a) will be equivalent to scanf(1). For understanding purpose, consider this pointer example, int *p; int a=5; *p=a; // not valid 👉 So,same thing goes inside scanf also. When the format pointer for %d points to variable a without its address operator, it becomes illegal in C because pointer holds the address of variable only.And without address operator (&) it directly tends to dereference the address and not the value in it.So segmentation fault. 🔍 And so no ouput is generated and you see No Output. and printf doesnot work. The compiler doesnot generate any error because the handle is passed and is executed by the system interrupts for termination.
7th Jul 2018, 10:56 AM
D-Key
D-Key - avatar
+ 6
Saw Mee Tha Gay 📜 FINAL CONCLUSION:- The valid in C programming is scanf("%d",&a) and only this should be used. ❔📃 CONTRADICTION TO COMPILATION:- The other two scanf(a) and scanf("%d",a), doesnot generate any compilation error as explained all above and if used inside program it also gets executed.So in reference to no compilation error both three of them will get executed but programmatically only scanf("%d",&a) is correct and valid in C . There may be semantic error for scanf(a) scanf("%d",a) but at compilation it doesnot generate any compilation error and code executes. And so as explained above in all details and its working . while(0xff) "Hope clarified on how it works"
7th Jul 2018, 11:10 AM
D-Key
D-Key - avatar
+ 4
Saw Mee Tha Gay well, the first one doesn't even know what type to scan for and will always put 0 into the variable, and the second one doesn't work because you aren't passing the variable by reference.
6th Jul 2018, 7:38 AM
hinanawi
hinanawi - avatar
+ 3
How???
6th Jul 2018, 7:37 AM
[No Name]
[No Name] - avatar
+ 3
hinanawi actually we don't even know what's the type of a, if it's a pointer to integer the second will work and not the last one :)
6th Jul 2018, 4:07 PM
darrencweid
darrencweid - avatar
+ 2
the differences are that only the last one will actually work
6th Jul 2018, 7:26 AM
hinanawi
hinanawi - avatar
+ 2
Saw Mee Tha Gay the first one cannot work since the prototype for scanf is : int scanf(const char *format, ...); and requires at least two arguments including the format string first
6th Jul 2018, 4:11 PM
darrencweid
darrencweid - avatar