Reading long int using scanf

前端 未结 5 2208
栀梦
栀梦 2021-02-13 00:51

To read an int using scanf we use:

scanf(\"%d\", &i);

What if i is a long not int??

相关标签:
5条回答
  • 2021-02-13 01:30

    Check this, here is the answer: "%I64d"

    0 讨论(0)
  • 2021-02-13 01:33

    Each conversion specifier expects its corresponding argument to be of a specific type; if the argument's type does not match the expected type, then the behavior is undefined. If you want to read into a long with scanf(), you need to use the %ld conversion specifier:

    long i;
    scanf("%ld", &i);
    

    Check the online draft C standard (.pdf file), section 7.19.6.2, paragraph 11 for a complete listing of size modifiers and expected types.

    0 讨论(0)
  • 2021-02-13 01:38
    scanf("%ld", &i);
    

    You can also use "%Ld" for a long long (and depending on your compiler, sometimes also "%lld").

    Take a look at the Conversions section of the scanf man page for more. (Just Google it if your system doesn't have manpages).

    0 讨论(0)
  • 2021-02-13 01:41

    For gods sake:

    long n;
    scanf( "%ld", & n );
    
    0 讨论(0)
  • 2021-02-13 01:50

    Just use

    long l;
    
    scanf("%ld", &l);
    

    it gives me an irritating warning..

    That warning is quite right. This is begging for stack corruption.

    0 讨论(0)
提交回复
热议问题