2
#include<stdio.h>
#include<string.h>

int main() {
char a[100]="1234\0567"; //Answer: 6
int len=strlen(a);
printf("%d",len); 
}

This code prints 6 as the answer. Shouldn't it print 4 since strlen returns the count until the null character is encountered?

However when a space is included between \0 and 5. It returns 4 as the answer

char a[100]="1234\0 567"; //Answer: 4

Am I missing something?

2 Answers2

2

You first version has the octal number \056 not the \0 character

Edit: in the similar situations use \000 octal sequence instead:

/* from your example*/ char a[100]="1234\000567";
0___________
  • 60,014
  • 4
  • 34
  • 74
1

The \056 is a single character, with the octal ASCII code (56)8, decimal - (46)10.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
NPE
  • 486,780
  • 108
  • 951
  • 1,012