-3

hii everyone i want to ask a question that what happen if '&' operator is not included in scanf.

 #include <stdio.h>
 int main() 
 {
     int x;
     scanf("%d", x); 
   /* than what type of value will scanf store in x if there is                  
       not address of operator. */
     printf("%d", x);
     return 0;
  } 

either program crash or some garbage value prints out. I m little bit confused.

Aman Warudkar
  • 125
  • 1
  • 1
  • 12
  • 1
    The program has undefined behaviour, that's what happens. – Kerrek SB Jun 05 '17 at 18:51
  • Possible duplicate [How does the scans function work in C?](https://stackoverflow.com/questions/2062648/how-does-the-scanf-function-work-in-c) – Miket25 Jun 05 '17 at 18:52
  • r u sure its undefined behaviour. – Aman Warudkar Jun 05 '17 at 18:52
  • 4
    It's **Undefined Behaviour**. Program can print 0, 42, or the lyrics to the national anthem. – pmg Jun 05 '17 at 18:52
  • Yes it's undefined behavior. You should get a warning from your compiler about this. – Kevin Jun 05 '17 at 18:53
  • 1
    `scanf()` expects an address, so it will treat `x` value (which isn't even initialized in your case) as an address. This address may be valid or invalid, therefore unexpected behavior. – SHG Jun 05 '17 at 18:53
  • I don't get any warning – Aman Warudkar Jun 05 '17 at 18:56
  • The question tells us that you *know* it should be `&x`. Your program has not initialised `x` so passing `x` is undefined behaviour. Even with `x` initialised, it is wrong. – Weather Vane Jun 05 '17 at 18:56
  • 2
    Then turn up your warnings to pedantic levels. Or use a compiler developer in the last decade; preferably *both*. Unless the expressions provided as arguments match the requirements of the [format specifier(s)](http://en.cppreference.com/w/c/io/fscanf) in the format string, it's *undefined behavior*. – WhozCraig Jun 05 '17 at 18:57
  • MSVC gives 2 warnings. 1) 'scanf' : format string '%d' requires an argument of type 'int *', but variadic argument 1 has type 'int', and 2) uninitialized local variable 'x' used. – Weather Vane Jun 05 '17 at 19:03
  • i m using mingw compiler. And it doesn't matter whether x is initialized or not but important is behavior of scanf in absence of &. – Aman Warudkar Jun 05 '17 at 19:06
  • 1
    It is **not** important how it behaves without `&`. It is wrong, and exploring that might give you an understanding but otherwise is a [wild goose chase](http://www.thefreedictionary.com/wild-goose+chase). – Weather Vane Jun 05 '17 at 19:24

2 Answers2

2

scanf will try to write the value it read to the address stored in x. When you use the & operator, it will write the value to the address of x.

In this case x is a variable on the stack. Variables on the stack are (in C) not automatically initialized, so initially x will probably contain a garbage value. scanf will thus try to write the input value to a garbage address in memory.

This may either cause corruption of the program code or data (which may crash the program or result in unexpected behavior), or cause a segmentation fault (which will cause the OS to abort the program) if the value in x points to memory to which the program is not allowed to write (invalid or read-only memory). What exactly will happen depends on the value of x, which may be a value left over from another program, and this may be different each time you run the program.

Ivo Smits
  • 237
  • 1
  • 10
  • so printf will print out garbage VALUE of x. – Aman Warudkar Jun 05 '17 at 19:04
  • @AmanWarudkar that is correct. If the program does not crash on the scanf, printf will print out the (garbage) value of x. Note how this is different from scanf - printf only has inputs so it takes values for its arguments, scanf has outputs so it takes pointers as its arguments to which it can write the result. – Ivo Smits Jun 05 '17 at 19:13
  • hey one more question as you above say if scanf has address operator than it will write at ADDRESS of x AND in absence of '&' operator it will write the Value to the ADDRESS stored in x which contain garbage address whether it is valid or not, is I am RIGHT – Aman Warudkar Jun 05 '17 at 19:25
  • 1
    If the address in `x` is garbage, the system might object. Or it might do it (if the address is allowable) but then create bugs because you are throwing tomatoes at the window. – Weather Vane Jun 05 '17 at 19:27
0

When you call scanf with the %d conversion specifier, the corresponding argument needs to be an expression that evaluates to the location of an integer object into which scanf will write the input value:

scanf( "%d", location-expression );

location-expression can take on multiple forms. To read into a single scalar object like x, you could use the expression &x.

scanf( "%d", &x ); // &x evaluates to the location of x

Or, you could use a pointer variable that's initialized to point to x:

int *p = &x;      // p stores the address of x
scanf( "%d", p ); // scanf writes to x *through* p; p == &x

The expression p is equivalent to the expression &x in this case. We don't use the & operator on p, because we want write to the address stored in p, not the address of p.

When you write

scanf( "%d", x );

you're telling scanf to store input to the address stored in x. There are several problems with that:

  1. x is not explicitly initialized to anything; its value is indeterminate;
  2. Because of 1, the odds that x contains a value that corresponds to a valid pointer value (that is, the location of an object in your program) are extremely low;
  3. The type of the expression x is int, but %d expects its corresponding argument to have type int * - any modern compiler should yell at you.
John Bode
  • 119,563
  • 19
  • 122
  • 198