1

I just know about the %i format specifier from this link

Difference between format specifiers %i and %d in printf

and I tried to implement it with this program.

#include <stdio.h>

int main(){

    long long a,b;

    printf("Input: ");

    scanf("%i %lld",&b,&a);

    printf("Output: %i %lld",b,a);
}

%i worked properly but %lld stores a garbage value in variable a.

This is the output of this program.

Input : 033 033

Output : 27 141733920846

Process returned 0 (0x0) execution time : 4.443 s Press any key to continue.

Can anyone explain, why I am getting the garbage value in variable a?

Community
  • 1
  • 1
Mayur Kharche
  • 717
  • 1
  • 8
  • 27
  • you didn't input a %11d , you've input 033\n(and some garbage from previous executions), that were interpreted by the scanf, the faulty here is the formatting in the scanf, and the scanf itself. – Gar Jun 24 '16 at 07:20
  • Passing invalid things to scanf is undefined behavior, and %lld is not accepting octal literals. – Magisch Jun 24 '16 at 07:21
  • Possible duplicate of [Wrong format specifiers in scanf (or) printf](http://stackoverflow.com/questions/12830052/wrong-format-specifiers-in-scanf-or-printf) – phuclv Jun 24 '16 at 07:21
  • Using the wrong format specifier invokes undefined behavior http://stackoverflow.com/q/4231891/995714, http://stackoverflow.com/q/16864552/995714 – phuclv Jun 24 '16 at 07:26

2 Answers2

5

scanf %i takes an int *, but you're passing &b, which is a long long int *. This has undefined behavior.

You should be using %lli.

The same problem occurs in printf: Use %lli to print b, not %i.

You should also check scanf's return value to make sure two values were successfully read.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Yeah, I forgot to fix it in the printf too. I updated my answer with the fixed code and live demo. – Magisch Jun 24 '16 at 07:39
1

First of all, using %i for a long long int is undefined behavior, so use %lli instead.

The same issue persists in the printf statement, too.

Fixed code:

#include <stdio.h>


int main(){

    long long a,b;
    int retval;


    printf("Input: \n");

    retval = scanf("%lli %lld",&b,&a);

    printf("Output: %lli %lld",b,a);
    printf("\nRetval: %d",retval);
    return 1;
}

Input:

033 033

Output:

Input: Output: 27 33 Retval: 2

Live Demo

Note: Always check the return value of scanf. It returns the number of scanned items, which you should test against your expectations.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • 1
    I'm pretty sure `%d` is supposed to read `"033"` as `33`, like `atoi("033")`. – melpomene Jun 24 '16 at 07:22
  • @melpomene Can you provide a source for this? I'm pretty sure that its just not a valid input. – Magisch Jun 24 '16 at 07:23
  • 1
    http://port70.net/~nsz/c/c99/n1256.html#7.19.6.2p12 says `%d` works like `strtol(..., 10)`. http://port70.net/~nsz/c/c99/n1256.html#7.20.1.4 says digits less than the base (i.e. 0 .. 9) are accepted. It doesn't prohibit leading zeroes. – melpomene Jun 24 '16 at 07:26
  • @melpomene I stand corrected then. Interestingly, changing it to %lli only will not make the garbage value go away. currently investigating further on this. – Magisch Jun 24 '16 at 07:29