C - sizeof(string) result is less than it should be -
i know string should n+1 in length, reason program printing sizeof(string) n-2.
#include <stdio.h> #include <stdlib.h> int main() { char name [] = "tom"; int x = sizeof(name); int i; printf("sizeof(name) = %d\n", i); for(i = 0; < x; i++) { printf("character @ %d %c\n", i, name[i]); } return 0; } can explain why?
you're printing i, not x.
i never initialized, undefined behavior.
Comments
Post a Comment