printf giving me incorrect output in C

前端 未结 5 1963
长情又很酷
长情又很酷 2021-01-21 09:44

This is probably a very elementary problem, but I cannot find the answer anywhere, and this is the first time I\'ve had the problem after several weeks of programming in C. In e

相关标签:
5条回答
  • 2021-01-21 10:11

    The calling conventions are different in printf, and scanf.

    Printf's arguments are typically by value (no &), but scanf has to have pointers. (&)
    (You can't put a new value to e.g. 1.0, but you can print it...)

    0 讨论(0)
  • 2021-01-21 10:13

    You have to do:

    printf("size is %d", size);
    

    instead. This prints the value of the int object size.

    But

     printf("size is %d", &size);
    

    is undefined behavior.

    0 讨论(0)
  • 2021-01-21 10:18

    You are printing the address of size, i.e., &size.

    Just pass a plain size.

    0 讨论(0)
  • 2021-01-21 10:26

    Try

    printf("size is %d", size);
    

    & gives you the memory location (address) of a variable.

    printf("size is %d", &size);
    

    So, the above will print the memory location(address) of size, not the value stored in size.

    0 讨论(0)
  • 2021-01-21 10:26

    Remove the & in the printf statement as &size --> prints the address.

          printf("size is %d", size);
    
    0 讨论(0)
提交回复
热议问题