-1

Can I take 64 bit numbers input in C with %lld?

If not how to? Please give an example when you answer.

Thank you

1 Answers1

2

You can read integers of long long with %lld specifier via scanf() if your compiler and standard library supports that, but it is not guaranteed to be 64bit. (long long can store at least numbers between -9223372036854775807 (-(2**63 - 1)) and +9223372036854775807 (2**63 - 1) (both inclusive), so it should be at least 64bit)

You can use int64_t type and "%" SCNd64 format specifier from inttypes.h if it is supported in your environment. int64_t is a signed integer type with exactly 64bit. You can use uint64_t type and "%" SCNu64 format specifier for unsigned 64-bit integer.

Example:

#include <stdio.h>
#include <inttypes.h>

int main(void) {
    int64_t num;
    if (scanf("%" SCNd64, &num) != 1) { /* read 64-bit number */
        puts("read error");
        return 1;
    }
    printf("%" PRId64 "\n", num); /* print the number read */
    return 0;
}
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • But why you used void in int main ( void) ? I am new to programming and donot know about that. – Pronay Sarker Apr 30 '21 at 23:56
  • 1
    Because it it defined so in the standard and I don't use arguments here. [What should main() return in C and C++? - Stack Overflow](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – MikeCAT Apr 30 '21 at 23:57