Output data type of sizeof() operator





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







9















I am using Ubuntu 16.04.5 and GCC version 5.4.0.



I was playing with sizeof() operator, wrote the code below:



#include <stdio.h>

int main(int argc, char *argv){

long int mylint = 31331313131.1313;

printf("size of long int is %dn", sizeof(mylint));
printf("size of long int is %dn", sizeof(long int));

return 0;
}


I tried to compile using gcc -o ... ... command and was expecting:



size of long int is 8
size of long int is 8


But I got the following error:



fl_double_lint.c: In function ‘main’:
fl_double_lint.c:11:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]

printf("size of long int is %dn", sizeof(mylint));
^
fl_double_lint.c:12:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("size of long int is %dn", sizeof(long int));


When I use %ld instead it works as expected. Why sizeof() is not working with %d? (Why 'long unsigned int' but not 'int'?)



Edit: I know that many questions were asked regarding output of sizeof() operator (as suggested in comments). However, they do not answer the question of why using %d does not work (i.e. does not give any result). I know it is not correct format, but whenever we have a char type variable using %d we can get equivalent int result, this is also the case for short, uint8_t, uint16_t, uint32_t (generally for types with equal or less than 32 bit). The following code works:



#include <stdio.h>
#include <stdint.h>

int main(int argc, char *argv){

char mychar = 'd';
uint32_t myuint32 = 32;
uint16_t myuint16 = 16;
uint8_t myuint8 = 8;
short myshort = 26945;
int myint = 100;

printf("integer equivalent of mychar is %dn", mychar);
printf("integer equivalent of myuint8 is %dn", myuint8);
printf("integer equivalent of myuint16 is %dn", myuint16);
printf("integer equivalent of myuint32 is %dn", myuint32);
printf("character equivalent of myint is %cn", myint);
printf("integer equivalent of myshort is %dn", myshort);


return 0;
}


The result is:



integer equivalent of mychar is 100
integer equivalent of myuint8 is 8
integer equivalent of myuint16 is 16
integer equivalent of myuint32 is 32
character equivalent of myint is d
integer equivalent of myshort is 26945


And now I discovered that %d does not work for any variable that needs larger than 32 bit to be stored. After considering this question I have some idea about implementation dependence of size_t, maybe in my system it was unsigned long (%ld also giving the result proves it). So maybe if size_t was an unsigned int in my system I would get a result, is it true?



As it can be seen from the code above, %c decodes the last 8 bits of int as a character, why %d does not do the same (i.e. decode the last 32 bits of content of size_t variable as it was int? I believe if it would do so we could get the same result for small enough numbers, and this was what I meant when I initially asked the question).










share|improve this question




















  • 11





    sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

    – Eugene Sh.
    Jan 31 at 15:17






  • 5





    Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

    – Some programmer dude
    Jan 31 at 15:17








  • 3





    Possible duplicate of How sizeof operator works in C?

    – YSC
    Jan 31 at 16:48






  • 3





    This has been asked a zillion times. C gold badges: why is it still open???

    – YSC
    Jan 31 at 16:48






  • 4





    Possible duplicate of What's the correct way to use printf to print a size_t?

    – phuclv
    Feb 1 at 1:54


















9















I am using Ubuntu 16.04.5 and GCC version 5.4.0.



I was playing with sizeof() operator, wrote the code below:



#include <stdio.h>

int main(int argc, char *argv){

long int mylint = 31331313131.1313;

printf("size of long int is %dn", sizeof(mylint));
printf("size of long int is %dn", sizeof(long int));

return 0;
}


I tried to compile using gcc -o ... ... command and was expecting:



size of long int is 8
size of long int is 8


But I got the following error:



fl_double_lint.c: In function ‘main’:
fl_double_lint.c:11:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]

printf("size of long int is %dn", sizeof(mylint));
^
fl_double_lint.c:12:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("size of long int is %dn", sizeof(long int));


When I use %ld instead it works as expected. Why sizeof() is not working with %d? (Why 'long unsigned int' but not 'int'?)



Edit: I know that many questions were asked regarding output of sizeof() operator (as suggested in comments). However, they do not answer the question of why using %d does not work (i.e. does not give any result). I know it is not correct format, but whenever we have a char type variable using %d we can get equivalent int result, this is also the case for short, uint8_t, uint16_t, uint32_t (generally for types with equal or less than 32 bit). The following code works:



#include <stdio.h>
#include <stdint.h>

int main(int argc, char *argv){

char mychar = 'd';
uint32_t myuint32 = 32;
uint16_t myuint16 = 16;
uint8_t myuint8 = 8;
short myshort = 26945;
int myint = 100;

printf("integer equivalent of mychar is %dn", mychar);
printf("integer equivalent of myuint8 is %dn", myuint8);
printf("integer equivalent of myuint16 is %dn", myuint16);
printf("integer equivalent of myuint32 is %dn", myuint32);
printf("character equivalent of myint is %cn", myint);
printf("integer equivalent of myshort is %dn", myshort);


return 0;
}


The result is:



integer equivalent of mychar is 100
integer equivalent of myuint8 is 8
integer equivalent of myuint16 is 16
integer equivalent of myuint32 is 32
character equivalent of myint is d
integer equivalent of myshort is 26945


And now I discovered that %d does not work for any variable that needs larger than 32 bit to be stored. After considering this question I have some idea about implementation dependence of size_t, maybe in my system it was unsigned long (%ld also giving the result proves it). So maybe if size_t was an unsigned int in my system I would get a result, is it true?



As it can be seen from the code above, %c decodes the last 8 bits of int as a character, why %d does not do the same (i.e. decode the last 32 bits of content of size_t variable as it was int? I believe if it would do so we could get the same result for small enough numbers, and this was what I meant when I initially asked the question).










share|improve this question




















  • 11





    sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

    – Eugene Sh.
    Jan 31 at 15:17






  • 5





    Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

    – Some programmer dude
    Jan 31 at 15:17








  • 3





    Possible duplicate of How sizeof operator works in C?

    – YSC
    Jan 31 at 16:48






  • 3





    This has been asked a zillion times. C gold badges: why is it still open???

    – YSC
    Jan 31 at 16:48






  • 4





    Possible duplicate of What's the correct way to use printf to print a size_t?

    – phuclv
    Feb 1 at 1:54














9












9








9


1






I am using Ubuntu 16.04.5 and GCC version 5.4.0.



I was playing with sizeof() operator, wrote the code below:



#include <stdio.h>

int main(int argc, char *argv){

long int mylint = 31331313131.1313;

printf("size of long int is %dn", sizeof(mylint));
printf("size of long int is %dn", sizeof(long int));

return 0;
}


I tried to compile using gcc -o ... ... command and was expecting:



size of long int is 8
size of long int is 8


But I got the following error:



fl_double_lint.c: In function ‘main’:
fl_double_lint.c:11:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]

printf("size of long int is %dn", sizeof(mylint));
^
fl_double_lint.c:12:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("size of long int is %dn", sizeof(long int));


When I use %ld instead it works as expected. Why sizeof() is not working with %d? (Why 'long unsigned int' but not 'int'?)



Edit: I know that many questions were asked regarding output of sizeof() operator (as suggested in comments). However, they do not answer the question of why using %d does not work (i.e. does not give any result). I know it is not correct format, but whenever we have a char type variable using %d we can get equivalent int result, this is also the case for short, uint8_t, uint16_t, uint32_t (generally for types with equal or less than 32 bit). The following code works:



#include <stdio.h>
#include <stdint.h>

int main(int argc, char *argv){

char mychar = 'd';
uint32_t myuint32 = 32;
uint16_t myuint16 = 16;
uint8_t myuint8 = 8;
short myshort = 26945;
int myint = 100;

printf("integer equivalent of mychar is %dn", mychar);
printf("integer equivalent of myuint8 is %dn", myuint8);
printf("integer equivalent of myuint16 is %dn", myuint16);
printf("integer equivalent of myuint32 is %dn", myuint32);
printf("character equivalent of myint is %cn", myint);
printf("integer equivalent of myshort is %dn", myshort);


return 0;
}


The result is:



integer equivalent of mychar is 100
integer equivalent of myuint8 is 8
integer equivalent of myuint16 is 16
integer equivalent of myuint32 is 32
character equivalent of myint is d
integer equivalent of myshort is 26945


And now I discovered that %d does not work for any variable that needs larger than 32 bit to be stored. After considering this question I have some idea about implementation dependence of size_t, maybe in my system it was unsigned long (%ld also giving the result proves it). So maybe if size_t was an unsigned int in my system I would get a result, is it true?



As it can be seen from the code above, %c decodes the last 8 bits of int as a character, why %d does not do the same (i.e. decode the last 32 bits of content of size_t variable as it was int? I believe if it would do so we could get the same result for small enough numbers, and this was what I meant when I initially asked the question).










share|improve this question
















I am using Ubuntu 16.04.5 and GCC version 5.4.0.



I was playing with sizeof() operator, wrote the code below:



#include <stdio.h>

int main(int argc, char *argv){

long int mylint = 31331313131.1313;

printf("size of long int is %dn", sizeof(mylint));
printf("size of long int is %dn", sizeof(long int));

return 0;
}


I tried to compile using gcc -o ... ... command and was expecting:



size of long int is 8
size of long int is 8


But I got the following error:



fl_double_lint.c: In function ‘main’:
fl_double_lint.c:11:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]

printf("size of long int is %dn", sizeof(mylint));
^
fl_double_lint.c:12:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("size of long int is %dn", sizeof(long int));


When I use %ld instead it works as expected. Why sizeof() is not working with %d? (Why 'long unsigned int' but not 'int'?)



Edit: I know that many questions were asked regarding output of sizeof() operator (as suggested in comments). However, they do not answer the question of why using %d does not work (i.e. does not give any result). I know it is not correct format, but whenever we have a char type variable using %d we can get equivalent int result, this is also the case for short, uint8_t, uint16_t, uint32_t (generally for types with equal or less than 32 bit). The following code works:



#include <stdio.h>
#include <stdint.h>

int main(int argc, char *argv){

char mychar = 'd';
uint32_t myuint32 = 32;
uint16_t myuint16 = 16;
uint8_t myuint8 = 8;
short myshort = 26945;
int myint = 100;

printf("integer equivalent of mychar is %dn", mychar);
printf("integer equivalent of myuint8 is %dn", myuint8);
printf("integer equivalent of myuint16 is %dn", myuint16);
printf("integer equivalent of myuint32 is %dn", myuint32);
printf("character equivalent of myint is %cn", myint);
printf("integer equivalent of myshort is %dn", myshort);


return 0;
}


The result is:



integer equivalent of mychar is 100
integer equivalent of myuint8 is 8
integer equivalent of myuint16 is 16
integer equivalent of myuint32 is 32
character equivalent of myint is d
integer equivalent of myshort is 26945


And now I discovered that %d does not work for any variable that needs larger than 32 bit to be stored. After considering this question I have some idea about implementation dependence of size_t, maybe in my system it was unsigned long (%ld also giving the result proves it). So maybe if size_t was an unsigned int in my system I would get a result, is it true?



As it can be seen from the code above, %c decodes the last 8 bits of int as a character, why %d does not do the same (i.e. decode the last 32 bits of content of size_t variable as it was int? I believe if it would do so we could get the same result for small enough numbers, and this was what I meant when I initially asked the question).







c ubuntu ubuntu-16.04 sizeof gcc5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 at 8:56







Adhamzhon Shukurov

















asked Jan 31 at 15:15









Adhamzhon ShukurovAdhamzhon Shukurov

434414




434414








  • 11





    sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

    – Eugene Sh.
    Jan 31 at 15:17






  • 5





    Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

    – Some programmer dude
    Jan 31 at 15:17








  • 3





    Possible duplicate of How sizeof operator works in C?

    – YSC
    Jan 31 at 16:48






  • 3





    This has been asked a zillion times. C gold badges: why is it still open???

    – YSC
    Jan 31 at 16:48






  • 4





    Possible duplicate of What's the correct way to use printf to print a size_t?

    – phuclv
    Feb 1 at 1:54














  • 11





    sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

    – Eugene Sh.
    Jan 31 at 15:17






  • 5





    Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

    – Some programmer dude
    Jan 31 at 15:17








  • 3





    Possible duplicate of How sizeof operator works in C?

    – YSC
    Jan 31 at 16:48






  • 3





    This has been asked a zillion times. C gold badges: why is it still open???

    – YSC
    Jan 31 at 16:48






  • 4





    Possible duplicate of What's the correct way to use printf to print a size_t?

    – phuclv
    Feb 1 at 1:54








11




11





sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

– Eugene Sh.
Jan 31 at 15:17





sizeof is an operator, not a function. It's result is of type size_t and the correct format specifier is %zu.

– Eugene Sh.
Jan 31 at 15:17




5




5





Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

– Some programmer dude
Jan 31 at 15:17







Actually "%ld" is also wrong. The sizeof operator returns a value of type size_t, and to print it with printf (and family) you should use "%zu". Or since you also tagged this question with the very different C++ language, just use std::cout << sizeof(something).

– Some programmer dude
Jan 31 at 15:17






3




3





Possible duplicate of How sizeof operator works in C?

– YSC
Jan 31 at 16:48





Possible duplicate of How sizeof operator works in C?

– YSC
Jan 31 at 16:48




3




3





This has been asked a zillion times. C gold badges: why is it still open???

– YSC
Jan 31 at 16:48





This has been asked a zillion times. C gold badges: why is it still open???

– YSC
Jan 31 at 16:48




4




4





Possible duplicate of What's the correct way to use printf to print a size_t?

– phuclv
Feb 1 at 1:54





Possible duplicate of What's the correct way to use printf to print a size_t?

– phuclv
Feb 1 at 1:54












1 Answer
1






active

oldest

votes


















24














The sizeof operator evaluates to a value of type size_t. This type is unsigned and typically larger than an int, which is why you get the warning.



Using the wrong format specifier to printf invokes undefined behavior. You can get away with this however for types smaller than int due to the rules of integer promotions in section 6.3.1.1p2 of the C standard:




The following may be used in an expression wherever an int or unsigned
int may be used:




  • An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than
    or equal to the rank of int and unsigned int .

  • A bit-field of type
    _Bool , int , signed int ,or unsigned int .


If an int can represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int ; otherwise,
it is converted to an unsigned int . These are called the
integer promotions . All other types are unchanged by the
integer promotions.




So as long this doesn't result in a change from unsigned to signed, types smaller than int can be printed with %d.



The proper type modifier for size_t is %zu, as per section 7.21.6.1p7 of the C standard regarding length modifiers for the fprintf function (and by extension, printf):




z



Specifies that a following d , i , o , u , x ,or X conversion
specifier applies to a size_t or the corresponding signed integer
type argument; or that a following n conversion specifier applies
to a pointer to a signed integer type corresponding to size_t
argument.




So what you want is:



printf("size of long int is %zun", sizeof(mylint));





share|improve this answer





















  • 5





    I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

    – YSC
    Jan 31 at 16:49








  • 3





    @YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

    – dbush
    Jan 31 at 17:31






  • 5





    I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

    – YSC
    Jan 31 at 17:33






  • 3





    "I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

    – Cris Luengo
    Jan 31 at 23:46






  • 5





    Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

    – Ilmari Karonen
    Feb 1 at 1:41












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54463692%2foutput-data-type-of-sizeof-operator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









24














The sizeof operator evaluates to a value of type size_t. This type is unsigned and typically larger than an int, which is why you get the warning.



Using the wrong format specifier to printf invokes undefined behavior. You can get away with this however for types smaller than int due to the rules of integer promotions in section 6.3.1.1p2 of the C standard:




The following may be used in an expression wherever an int or unsigned
int may be used:




  • An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than
    or equal to the rank of int and unsigned int .

  • A bit-field of type
    _Bool , int , signed int ,or unsigned int .


If an int can represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int ; otherwise,
it is converted to an unsigned int . These are called the
integer promotions . All other types are unchanged by the
integer promotions.




So as long this doesn't result in a change from unsigned to signed, types smaller than int can be printed with %d.



The proper type modifier for size_t is %zu, as per section 7.21.6.1p7 of the C standard regarding length modifiers for the fprintf function (and by extension, printf):




z



Specifies that a following d , i , o , u , x ,or X conversion
specifier applies to a size_t or the corresponding signed integer
type argument; or that a following n conversion specifier applies
to a pointer to a signed integer type corresponding to size_t
argument.




So what you want is:



printf("size of long int is %zun", sizeof(mylint));





share|improve this answer





















  • 5





    I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

    – YSC
    Jan 31 at 16:49








  • 3





    @YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

    – dbush
    Jan 31 at 17:31






  • 5





    I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

    – YSC
    Jan 31 at 17:33






  • 3





    "I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

    – Cris Luengo
    Jan 31 at 23:46






  • 5





    Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

    – Ilmari Karonen
    Feb 1 at 1:41
















24














The sizeof operator evaluates to a value of type size_t. This type is unsigned and typically larger than an int, which is why you get the warning.



Using the wrong format specifier to printf invokes undefined behavior. You can get away with this however for types smaller than int due to the rules of integer promotions in section 6.3.1.1p2 of the C standard:




The following may be used in an expression wherever an int or unsigned
int may be used:




  • An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than
    or equal to the rank of int and unsigned int .

  • A bit-field of type
    _Bool , int , signed int ,or unsigned int .


If an int can represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int ; otherwise,
it is converted to an unsigned int . These are called the
integer promotions . All other types are unchanged by the
integer promotions.




So as long this doesn't result in a change from unsigned to signed, types smaller than int can be printed with %d.



The proper type modifier for size_t is %zu, as per section 7.21.6.1p7 of the C standard regarding length modifiers for the fprintf function (and by extension, printf):




z



Specifies that a following d , i , o , u , x ,or X conversion
specifier applies to a size_t or the corresponding signed integer
type argument; or that a following n conversion specifier applies
to a pointer to a signed integer type corresponding to size_t
argument.




So what you want is:



printf("size of long int is %zun", sizeof(mylint));





share|improve this answer





















  • 5





    I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

    – YSC
    Jan 31 at 16:49








  • 3





    @YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

    – dbush
    Jan 31 at 17:31






  • 5





    I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

    – YSC
    Jan 31 at 17:33






  • 3





    "I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

    – Cris Luengo
    Jan 31 at 23:46






  • 5





    Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

    – Ilmari Karonen
    Feb 1 at 1:41














24












24








24







The sizeof operator evaluates to a value of type size_t. This type is unsigned and typically larger than an int, which is why you get the warning.



Using the wrong format specifier to printf invokes undefined behavior. You can get away with this however for types smaller than int due to the rules of integer promotions in section 6.3.1.1p2 of the C standard:




The following may be used in an expression wherever an int or unsigned
int may be used:




  • An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than
    or equal to the rank of int and unsigned int .

  • A bit-field of type
    _Bool , int , signed int ,or unsigned int .


If an int can represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int ; otherwise,
it is converted to an unsigned int . These are called the
integer promotions . All other types are unchanged by the
integer promotions.




So as long this doesn't result in a change from unsigned to signed, types smaller than int can be printed with %d.



The proper type modifier for size_t is %zu, as per section 7.21.6.1p7 of the C standard regarding length modifiers for the fprintf function (and by extension, printf):




z



Specifies that a following d , i , o , u , x ,or X conversion
specifier applies to a size_t or the corresponding signed integer
type argument; or that a following n conversion specifier applies
to a pointer to a signed integer type corresponding to size_t
argument.




So what you want is:



printf("size of long int is %zun", sizeof(mylint));





share|improve this answer















The sizeof operator evaluates to a value of type size_t. This type is unsigned and typically larger than an int, which is why you get the warning.



Using the wrong format specifier to printf invokes undefined behavior. You can get away with this however for types smaller than int due to the rules of integer promotions in section 6.3.1.1p2 of the C standard:




The following may be used in an expression wherever an int or unsigned
int may be used:




  • An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than
    or equal to the rank of int and unsigned int .

  • A bit-field of type
    _Bool , int , signed int ,or unsigned int .


If an int can represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int ; otherwise,
it is converted to an unsigned int . These are called the
integer promotions . All other types are unchanged by the
integer promotions.




So as long this doesn't result in a change from unsigned to signed, types smaller than int can be printed with %d.



The proper type modifier for size_t is %zu, as per section 7.21.6.1p7 of the C standard regarding length modifiers for the fprintf function (and by extension, printf):




z



Specifies that a following d , i , o , u , x ,or X conversion
specifier applies to a size_t or the corresponding signed integer
type argument; or that a following n conversion specifier applies
to a pointer to a signed integer type corresponding to size_t
argument.




So what you want is:



printf("size of long int is %zun", sizeof(mylint));






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 1 at 12:06

























answered Jan 31 at 15:17









dbushdbush

104k14109146




104k14109146








  • 5





    I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

    – YSC
    Jan 31 at 16:49








  • 3





    @YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

    – dbush
    Jan 31 at 17:31






  • 5





    I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

    – YSC
    Jan 31 at 17:33






  • 3





    "I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

    – Cris Luengo
    Jan 31 at 23:46






  • 5





    Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

    – Ilmari Karonen
    Feb 1 at 1:41














  • 5





    I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

    – YSC
    Jan 31 at 16:49








  • 3





    @YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

    – dbush
    Jan 31 at 17:31






  • 5





    I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

    – YSC
    Jan 31 at 17:33






  • 3





    "I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

    – Cris Luengo
    Jan 31 at 23:46






  • 5





    Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

    – Ilmari Karonen
    Feb 1 at 1:41








5




5





I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

– YSC
Jan 31 at 16:49







I downvoted since as a C gold badge I'd expect you to close this question as the duplicate it is rather than answering.

– YSC
Jan 31 at 16:49






3




3





@YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

– dbush
Jan 31 at 17:31





@YSC It's not always easy to find one unless you have a specific one in mind. Given that this one has a title that better reflects the question, is more upvoted, and has an answer with a quote from the standard, I'm reversing the dup.

– dbush
Jan 31 at 17:31




5




5





I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

– YSC
Jan 31 at 17:33





I found the duplicate by searching for "printf format sizeof". First answer. I disagree with you but not strongly enough to do anything.

– YSC
Jan 31 at 17:33




3




3





"I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

– Cris Luengo
Jan 31 at 23:46





"I'm going to close the other question as a dupe of this one because I wrote the answer here". If you think your answer adds something over the answer to the other question, move your answer there. This is not right.

– Cris Luengo
Jan 31 at 23:46




5




5





Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

– Ilmari Karonen
Feb 1 at 1:41





Given that the questions (and their answers) are substantially identical, it might be best to ask a mod to merge them. (I also kind of agree with @CrisLuengo that you probably shouldn't have dupehammered that question yourself. Even if it was objectively the right call -- which I'm not sure about -- it still looks like an attempt to game the system for rep and visibility.)

– Ilmari Karonen
Feb 1 at 1:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54463692%2foutput-data-type-of-sizeof-operator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$