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

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
   */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.