C - A question of sizeof() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C - A question of sizeof()

I understand a char[] is "\0" terminated so it has a size of 4, but why the second sizeof() gives 8, since "programmer" has 10 chars? #include <stdio.h> int main() { const char firstname[] = "dev"; const char* lastname = "programmer"; printf("%lu\n", sizeof(firstname)); printf("%lu\n", sizeof(lastname)); return 0; }

17th Sep 2019, 10:34 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
2 Answers
+ 11
lastname is a pointer variable. lastname does not contain the string "programmer", it has the address of that string. 8 bytes are used to store address value. even if you assign 1000 characters long string to lastname, the size of lastname will remain unchanged - 8 bytes.
17th Sep 2019, 10:45 AM
nidhi
+ 7
I think u might be running the above code in 64 bit system. In 64 bit system, memory address is 64 bits=8 byte. Also, ur printing the address of a pointer, size of pointer remains same irrespective of data type it points too... U can know the system size by printing sizeof(int). If answer is 4,then it is 32 bit system. If answer is 8,then it is 64 bit system.
17th Sep 2019, 11:15 AM
Kuri
Kuri - avatar