0

Why can't I use "long long int" with "int" in my C code?

int main(void) {
    long long int a;
    int b;
    a = 1000000000;
    b = 3200;
    printf("long long int a = %d\n int b = %d", a, b);
    return 0;
}

long long int a = 1000000000

int b = 0

Puck
  • 2,080
  • 4
  • 19
  • 30
  • 8
    %d refers to a regular int, not a long long. – teppic Mar 31 '15 at 14:30
  • 1
    Hmm I think VS might using a different specifier. See http://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c – chux - Reinstate Monica Mar 31 '15 at 14:33
  • Voting to close as "simple typographic error" as the answer is likely found in chapter 1 of most C programming books. – Lundin Mar 31 '15 at 14:44
  • `printf` is a vararg function (`..` -> variable arguments). It'll use the format specifiers to interpret/make sense of the arguments you pass to it. If you specify `%d`, but pass a `long long int`, the result will be _undefined behavior_ – Elias Van Ootegem Mar 31 '15 at 14:47

5 Answers5

2

You have to use the correct format specifier in the printf function

printf("long long int a = %lld\n int b = %d", a, b);

Otherwise the function behaviour is undefined.

It seems that in the given situation the function considers the value of the long long int object pushed on the stack of the function as an argument like two objects of type int.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You can try this:

printf("long long int a = %lld\n int b = %d", a, b);

%d is used to refer int. If you want to refer long long int then you have to use %lld

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Using wrong format specifier leads to undefined behavior the right format specifier to print out a long long int is %lld

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

C99 standard says: If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined The type of a is long long int but you use %d to print it.

icecity96
  • 1,177
  • 12
  • 26
  • 1
    Does visual-studio-2012 follow C99? – chux - Reinstate Monica Mar 31 '15 at 14:35
  • @chux Partly...I only want to say when we code we should try to avoid something that may cause error. – icecity96 Mar 31 '15 at 14:43
  • @chux Visual Studio doesn't follow any standard, least of all the C standards. To even call it a C compiler is questionable. – Lundin Mar 31 '15 at 14:45
  • @Lundin OP tagged this post `visual-studio-2012` so answers should reflect that. Discussing C99 without addressing OP's version of code is less helpful. – chux - Reinstate Monica Mar 31 '15 at 14:49
  • 1
    @Lundin: VS _does_ have a C89 compliant compiler, but they never bothered to implement the C99 standard. VS _does_ implement some C99 features (variadic macro's, `long long`) as a superset of C89, but they did not implement VLA's and the like – Elias Van Ootegem Mar 31 '15 at 14:50
  • @chux Yes but it is also tagged C, making the tags contradict each other. Referring to C99 in a C post is perfectly fine, as it is probably the most used C standard still. – Lundin Mar 31 '15 at 14:51
  • @EliasVanOotegem It contains so many oddities though, I very much doubt it is a strictly conforming compiler even by C90 standards. – Lundin Mar 31 '15 at 14:51
  • 1
    @Lundin: As always, MS has added some _"features"_ and _"safer alternatives"_ throughout, but AFAIK, it is (theoretically) possible to compile standard C89 using VS (although, It'd probably be a lot easier, faster, and all-round better to just use a different compiler :-P) – Elias Van Ootegem Mar 31 '15 at 14:54
0

There are two possible reasons.

  • Firstly and most importantly, I see no evidence of your compiler and/or standard library supporting C99, and hence no evidence of your compiler and/or standard library supporting long long int and the corresponding format specifier %lld.
  • Secondly, you're lying to printf about the type of a; %d tells printf that the argument is of type int, even though it isn't (it's long long int). You should be using %lld...

Technically both of these would invoke undefined behaviour; using %d when your argument isn't an int is UB and even if you were to correctly use %lld you'd be using an invalid format specifier in C89's scanf (%lld is a C99 format specifier) and invoking UB as a result.

Have you considered using Clang in Visual Studio?

autistic
  • 1
  • 3
  • 35
  • 80