0

I have noticed that when you try to print a null character in C, nothing will get printed.

printf("trying to print null\n");
printf("%c", '\0');

However, I am trying to print the characters in the following array one by one, up to the sixth character which is the null character.

char s[] = "Hello\0Bye";
int i;
for(i = 0; i < 7; i++) {
    printf("%c", s[i]);
}
printf("\n");

I was expecting "Hello" to be printed, as since the sixth character is null nothing will be printed. However my output was: "HelloB". It seems like printf skipped the null character and just went to the next character. I am not sure why the output is "HelloB" instead of "Hello".

Any insights would be really appreciated.

4
  • This applies for determing the length of the array i guess. Commented Sep 5, 2018 at 12:11
  • Which character is really at index 6? Draw out the string on a squared paper, one character per square, and count if you're unsure. Commented Sep 5, 2018 at 12:16
  • It certainly does not skip the character. Try piping your programs output to sed 's/\x0/\n/g', see what gets printed. Commented Sep 5, 2018 at 12:18
  • Sorry, my bad. It should be 'i < 6' not 'i < 7'. Commented Sep 5, 2018 at 12:27

4 Answers 4

1

The construction '\0' is commonly used to represent the null character. Here

printf("%c", '\0');

it prints nothing.

And in the decalaration of s

char s[] = "Hello\0Bye"; 

when you print like

for(i = 0; i < 7; i++) {
    printf("%c", s[i]); 
}

printf() prints upto 0<7(h), 1<7(e)..5<7(nothing on console),6<7(B) iterations only and 6th charactar is B hence its prints HelloB.

I was expecting "Hello" to be printed ? For that you should rotate loop until \0 encountered. For e.g

for(i = 0; s[i] != '\0'; i++) { /* rotate upto \0 not 7 or random no of times */ 
   printf("%c", s[i]); 
}

Or even you no need to check s[i] != '\0'

for(i = 0; s[i]; i++) { /* loop terminates automatically when \0 encounters */ 
     printf("%c", s[i]); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use below two options
1. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
2.Iterate through each character and print it.

Comments

0

int i; for(i=0; i<7; i++) generates 7, not 6 iterations. Printing \0 generates no pixels on the console (character 6) and then the 7th character (B follows).

Comments

-1

You could simply use %s format specifier instead of running loops.

%s accepts an address(which is usually the base address of the character array, i.e. string) and prints all the characters from that address until NULL i.e. \0 is encountered.

base address simply means the address of the first element of an array.

Let me demonstrate:

char s[] = "Hello\0Bye";
printf("%s", s);

Will print Hello

Explanation: here s represents the base address of the string and %s prints all characters from that address until \0 is encountered.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.