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

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -