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

explain the output.
i am getting 5.

Edited by shanki himanshu

5
Contributors
5
Replies
8
Views
5 Years
Discussion Span
Last Post by Banfa
1

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;
}
Votes + Comments
nice :)
0

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 by Ancient Dragon

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.