Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working of SizeOf Function for char Array

Tags:

arrays

c

sizeof

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

When i compile it it giving 6. I am not geting why it is giving 6 insted of 8. Like {'\1','2','3','4','5','s','\n'}

Please can anybody tell the reason for this, I want some deep and clear explanation. I will be thankful to them.

like image 871
Nishant Kumar Avatar asked Dec 07 '22 18:12

Nishant Kumar


1 Answers

Because \123 is considered one character, it's an escape sequence (octal). So sizeof calculates 5 characters '\123', '4', '5', 's', '\n', and the ending '\0'.

like image 137
AndersK Avatar answered Dec 20 '22 09:12

AndersK