char str[]="S\065AB";
printf("\n%d", sizeof(str));

explain the output.
i am getting 5.

Edited 4 Years Ago by shanki himanshu

this is what it means fifth is null terminator 065 in oct is '5' check ascii table

#include <stdio.h>

int main()
   char str[]="S\065AB";
   char strSame[5] = { 'S','\065', 'A', 'B', '\0'};

   printf("str 1 %d \n", sizeof(str));
   printf("str 2 %d\n", sizeof(strSame));
   printf("same ? %d [%s] [%s]",strcmp(str, strSame),str, strSame);   

   return 0;
nice :)

use strlen

Maybe, but maybe not. Depends on what you want, strlen() and sizeof() may return different values such as when the buffer contains embedded '\0' characters.

Edited 4 Years Ago by Ancient Dragon

strlen and sizeof should always return different values because strlen never includes the trailing '\0'. In a compleatly normal string such as "Hello World" with no embedded terminators strlen will return a value 1 less than sizeof returns.

It they don't return different values you have a buffer overrun in progress.

This question has already been answered. Start a new discussion instead.