-1

How do you format a (Signed) long long int using printf?

#include <stdio.h>
int main() {
    long long int num = 123456766666666890; //FYI: fits in 29 bits
    int RegularInteger = 4;
    printf("My number is %d bytes wide and its value is %d. A normal number is %d.\n", sizeof(num), num, RegularInteger);
    return 0;
}

Output:

My number is 8 bytes wide and its value is 1917383562. A normal number is 4.                                                                                         

I am not getting desired output.

  • 2
    You may want to read this question and its answers: [ULL suffix on a numeric literal](https://stackoverflow.com/questions/8809292/ull-suffix-on-a-numeric-literal) – CiaPan Mar 30 '21 at 16:10
  • My compiler complains: `a.c:4:25: error: integer literal is too large to be represented in any integer type long long int num = 1234567666666666666666890; //FYI: fits in 29 bits` – William Pursell Mar 30 '21 at 16:10
  • @WilliamPursell I changed the question a bit. –  Mar 30 '21 at 16:12
  • 3
    123456766666666890 == 0x1B69B467248F38A requires more than 29 bits to represent! – William Pursell Mar 30 '21 at 16:13
  • You can use `%lld` depending on the compiler. – Spiros Gkogkas Mar 30 '21 at 16:17
  • @SpirosGkogkas I am using online c compiler. Don't know about the rest https://www.onlinegdb.com/online_c_compiler –  Mar 30 '21 at 16:18
  • @RetiredNinja Ok, Sorry I am new., I have deleted those comments. –  Mar 30 '21 at 16:20
  • @SpirosGkogkas Are there any non-C99 compilers that support `long long int` but do not use `%lld` as the corresponding printf/scanf specifier? – Ian Abbott Mar 30 '21 at 16:28
  • 1
    @IanAbbott avr-gcc for example, many stdlib nano implementations (ARM, RISC-V) etc etc – 0___________ Mar 30 '21 at 16:29
  • @IanAbbott compiler does not care about formats, implementation in the standard library does – 0___________ Mar 30 '21 at 16:30
  • 1
    @0___________ "compiler does not care about formats, implementation in the standard library does" is not completely true these days. – Ian Abbott Mar 30 '21 at 16:34
  • @IanAbbott if you write about compiler printf format check then yes - compiler will checkit, consider OK, no warning, but the implementation will not print it correctly. BTW the same situation is with floats. Embedded libraries do not print floats by default. – 0___________ Mar 30 '21 at 16:40
  • 2
    @0___________ The compiler could also optimize based on the format string, such as replacing `printf("%s\n", string);` with `puts(string);`. – Ian Abbott Mar 30 '21 at 16:46
  • @IanAbbott it is not related to the topic – 0___________ Mar 30 '21 at 16:47

2 Answers2

3

Enable all warnings, A good compiler will warn about mis-matched specifiers and save time.

Use matching specifiers.

With C99 or later

#include <stdio.h>
int main() {
  long long int num = 123456766666666890; // A 57 bit number
  int RegularInteger = 4;

  // printf("My number is %d bytes wide",  sizeof(num));
  printf("My number is %zu bytes wide",  sizeof(num));

  // printf("and its value is %d.", num);
  printf("and its value is %lld.", num);

  printf(" A normal number is %d.\n", RegularInteger);
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0
#include <stdio.h>
int main() {
    long long int num = 123456766666666890; //FYI: fits in 29 bits
    int RegularInteger = 4;
    printf("My number is %zu bytes wide and its value is %lld. A normal number is %d.\n", sizeof(num), num, RegularInteger);
    return 0;
}

https://godbolt.org/z/eMvjGnr7Y

Source of image

enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74