How this code's output is 12? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How this code's output is 12?

(assuming 8 byte pointers) const char firstname[]="dev"; const char* lastname = "programmer"; printf(" %lu\n", sizeof(firstname)+sizeof(lastname));

21st Apr 2020, 7:02 PM
Ashfaq Reshad
Ashfaq Reshad - avatar
3 Answers
+ 1
firstname is equal to 4 bytes 1 byte per char + the invisible end of line character '\0' = 4 lastname is a char pointer which we are assuming has 8 bytes. sizeof() returns the size in bytes as a long unsigned int 4+8 = 12
21st Apr 2020, 7:22 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
firstname is an array of type char with a size of string length + 1. If you change the length of the string the sizeof() will return a number accordingly. Try it. https://www.google.com/amp/s/www.geeksforgeeks.org/whats-difference-between-char-s-and-char-s-in-c/amp/
21st Apr 2020, 7:28 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg ahh...thank you..i .was not aware of that. I'll remove my initial comment as it was incorrect.
21st Apr 2020, 7:42 PM
rodwynnejones
rodwynnejones - avatar