ขนาดของ string constant จะเท่ากับขนาดของจำนวน character constant ที่มีอยู่ บวกกับ null character ที่ compiler เติมปิดท้ายให้ ส่วนความยาวของ string ได้จากการนับจำนวน character จนถึง null character ไม่รวม null นะครับ ขนาดของ string constant หาได้จาก sizeof ส่วนความยาวของ string ได้จากการเรียกใช้ function strlen ใน standard C library
string
sizeof
value
strlen
value
""
sizeof ""
1
strlen("")
0
"xyz"
sizeof "xyz"
4
strlen("xyz")
3
"xyz\0"
sizeof "xyz\0"
5
strlen("xyz\0")
3
"xyz\0" จะมี null character ปิดท้ายสองตัว ตัวแรกมาจากการกำหนด \0 ส่วนตัวที่สอง compiler เติมให้
string constant ที่เหมือนกัน ไม่จำเป็นต้องอยู่ใน memory ที่เดียวกัน ถือว่าเป็น implementation defined ครับ โปรแกรมด้านล่างถ้าไป compile และรันด้วย compiler ต่างกันก็อาจได้ผลไม่เหมือนกัน #include <stdio.h> int main() { char *s1 = "abcxyz"; char *s2 = "abcxyz"; if (s1 == s2) printf("s1 and s2 point to the same string\n"); else printf("s1 and s2 point to the different string\n"); return 0; }