0

Can someone explain me
how these character or escape sequence are treated while finding string size(through sizeof() operator)

\0 \n \a \1 \2
for example char str[]="\1abc\0\0"

pls explain me in detailed

2
Contributors
1
Reply
3
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

Can someone explain me
how these character or escape sequence are treated while finding string size(through sizeof() operator)

\0 \n \a \1 \2
for example char str[]="\1abc\0\0"

pls explain me in detailed

#include <stdio.h>
   #include <string.h>
   
   void foo(const char *s, size_t size)
   {
   size_t i;
   printf("size = %d\n", (int)size);
   printf("strlen(s) = %d\n", (int)strlen(s));
   for(i = 0; i < size; ++i)
   {
   	  printf("s[%d] = %d\n", (int)i, s[i]);
   }
   }
   
   int main(void)
   {
   char a[] = "\0\n\a\1\2"; /* equivalent to: {0,'\n','\a','\1','\2',0} */
   char b[] = "\1abc\0\0";  /* equivalent to: {'\1','a','b','c',0,0,0} */
   foo(a, sizeof a);
   foo(b, sizeof b);
   return 0;
   }
   
   /* my output
   size = 6
   strlen(s) = 0
   s[0] = 0
   s[1] = 10
   s[2] = 7
   s[3] = 1
   s[4] = 2
   s[5] = 0
   size = 7
   strlen(s) = 4
   s[0] = 1
   s[1] = 97
   s[2] = 98
   s[3] = 99
   s[4] = 0
   s[5] = 0
   s[6] = 0
   */
This topic has been dead for over six months. 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.