Author Topic: why they use the word modifies in c  (Read 4531 times)

0 Members and 1 Guest are viewing this topic.

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2217
  • Country: 00
Re: why they use the word modifies in c
« Reply #50 on: May 04, 2020, 05:42:17 pm »
I started an attempt to use the standardized datatypes and I got the following problem when
changing a "long long int" to int64_t related to printf():

Code: [Select]
warning: format ‘%lli’ expects argument of type ‘long long int’, but argument 4 has type ‘int64_t {aka long int}’ [-Wformat=]
I don't want to disable -Wformat but I do want to get rid from the warning. Is there a solution?

 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7765
  • Country: de
  • A qualified hobbyist ;)
Re: why they use the word modifies in c
« Reply #51 on: May 04, 2020, 06:19:54 pm »
(long long int)my_variable
 

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2217
  • Country: 00
Re: why they use the word modifies in c
« Reply #52 on: May 04, 2020, 06:25:16 pm »
(long long int)my_variable

You are right ofcourse but it doesn't look very elegant. Why is int64_t a typedef from long int?
Why not long long int instead?
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: why they use the word modifies in c
« Reply #53 on: May 05, 2020, 06:26:35 am »
Code: [Select]
warning: format ‘%lli’ expects argument of type ‘long long int’, but argument 4 has type ‘int64_t {aka long int}’ [-Wformat=]I don't want to disable -Wformat but I do want to get rid from the warning. Is there a solution?

Yes, there is.
As the actual type underlying (u)int??_t is not generally known (apart from (u)int8_t, that is forcibly a char type as discussed above), the standard provides, in its slightly less than infinite wisdom  ::), type specific conversion specifiers as macros in chapter 7.8.1 "Macros for format specifiers" (same chapter for C99, C11 and C18).

They are defined as character string literals, and have a quite regular form.
In your case for an uint64_T it would be PRIu64 for printf and SCNu64 for scanf.

Being string literals, they will be concatenated with adjacent string literals in translation phase 6, and only contain the right conversion specifier, so it's up to you to provide the % and the format width and precision, e.g:
Code: [Select]
  printf("Here's my uint64_t: %20" PRIu64 "\n", my_uint64);
Edit: changed slightly imprecise language
« Last Edit: May 05, 2020, 06:41:02 am by newbrain »
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2217
  • Country: 00
Re: why they use the word modifies in c
« Reply #54 on: May 05, 2020, 07:16:07 am »
Code: [Select]
warning: format ‘%lli’ expects argument of type ‘long long int’, but argument 4 has type ‘int64_t {aka long int}’ [-Wformat=]I don't want to disable -Wformat but I do want to get rid from the warning. Is there a solution?

Yes, there is.
As the actual type underlying (u)int??_t is not generally known (apart from (u)int8_t, that is forcibly a char type as discussed above), the standard provides, in its slightly less than infinite wisdom  ::), type specific conversion specifiers as macros in chapter 7.8.1 "Macros for format specifiers" (same chapter for C99, C11 and C18).

They are defined as character string literals, and have a quite regular form.
In your case for an uint64_T it would be PRIu64 for printf and SCNu64 for scanf.

Being string literals, they will be concatenated with adjacent string literals in translation phase 6, and only contain the right conversion specifier, so it's up to you to provide the % and the format width and precision, e.g:
Code: [Select]
  printf("Here's my uint64_t: %20" PRIu64 "\n", my_uint64);
Edit: changed slightly imprecise language

Thanks newbrain,  but that looks even less elegant and it's makes the code less readable (at least to me).
Then I prefer the type cast as proposed by madires.
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: why they use the word modifies in c
« Reply #55 on: May 05, 2020, 07:57:29 am »
Thanks newbrain,  but that looks even less elegant and it's makes the code less readable (at least to me).
Then I prefer the type cast as proposed by madires.
Suit yourself  ;D - of course the cast will work as long as you pick the longest type available.

I, personally, find cast less appealing and elegant and use them very sparingly - especially if a correct standard solution exists.
You are not alone in this: I've seen a lot of professionally written code eschewing PRIxxx in favour of casts.
Some of the coders were not even aware and changed it when shown, some did not.



Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Mp3

  • Frequent Contributor
  • **
  • Posts: 361
  • Country: us
Re: why they use the word modifies in c
« Reply #56 on: May 05, 2020, 11:18:58 am »
Thanks newbrain,  but that looks even less elegant and it's makes the code less readable (at least to me).
Then I prefer the type cast as proposed by madires.

You don't have to keep trying to prove your coding practices to us. I think this thread ran its course.
High school graduate
 

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2217
  • Country: 00
Re: why they use the word modifies in c
« Reply #57 on: May 05, 2020, 11:40:15 am »
Thanks newbrain,  but that looks even less elegant and it's makes the code less readable (at least to me).
Then I prefer the type cast as proposed by madires.

You don't have to keep trying to prove your coding practices to us. I think this thread ran its course.

No, I don't have to. But I'm not hurting anybody either.
Honestly, I tried to "convert" a project by using the standard datatypes and I ran into all kinds of issues and that's the reason I bring it up.
I sincerely wanted to give it try and I know I can be stubborn but, and please believe me, I'm also open to better ideas.
 

Offline coppice

  • Super Contributor
  • ***
  • Posts: 8646
  • Country: gb
Re: why they use the word modifies in c
« Reply #58 on: May 05, 2020, 11:54:12 am »
Code: [Select]
warning: format ‘%lli’ expects argument of type ‘long long int’, but argument 4 has type ‘int64_t {aka long int}’ [-Wformat=]I don't want to disable -Wformat but I do want to get rid from the warning. Is there a solution?

Yes, there is.
As the actual type underlying (u)int??_t is not generally known (apart from (u)int8_t, that is forcibly a char type as discussed above), the standard provides, in its slightly less than infinite wisdom  ::), type specific conversion specifiers as macros in chapter 7.8.1 "Macros for format specifiers" (same chapter for C99, C11 and C18).

They are defined as character string literals, and have a quite regular form.
In your case for an uint64_T it would be PRIu64 for printf and SCNu64 for scanf.

Being string literals, they will be concatenated with adjacent string literals in translation phase 6, and only contain the right conversion specifier, so it's up to you to provide the % and the format width and precision, e.g:
Code: [Select]
  printf("Here's my uint64_t: %20" PRIu64 "\n", my_uint64);
Edit: changed slightly imprecise language

Thanks newbrain,  but that looks even less elegant and it's makes the code less readable (at least to me).
Then I prefer the type cast as proposed by madires.
That PRIu64 stuff is definitely a PITA for readability, but it is robust and portable - at least as portable as anything from the era before UTF8 can be in 2020.
 
The following users thanked this post: newbrain


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf