Function of SizeOf function for char Array

void main() { char s[]="\12345s\n"; printf("%d",sizeof(s)); } 

When I compile it, it gives 6. I don't understand why it gives 6 insted of 8. Like {'\1','2','3','4','5','s','\n'}

Please can someone explain the reason for this, I want to get a deep and clear explanation. I will be grateful to them.

+4
source share
3 answers

Since \123 is considered a single character, this is an escape sequence (octal). So, sizeof computes 5 characters '\123' , '4' , '5' , 's' , '\n' and the ending '\0' .

+12
source
 char s[]="\12345s\n"; 

\ indicates escape sequence This is the octal default value if numbers are used up to 3 places. So, \ 123 will be evaluated in octal -> 8 * 8 * 1 + 8 * 2 + 3 = 83 Therefore, if you print this, you will find it S45s When S → Ascii is equivalent to 83 Therefore, the size is also 4.

+2
source
  char s [] = "\ 12345s \ n" 

equally:

  char s [] = {'\ 123', '4', '5', 's', '\ n', 0} 

so there are only six elements.

+1
source

Source: https://habr.com/ru/post/1382679/


All Articles