-1

I tried to reverse this char array with null characters in the middle and the end, without using string length. (original code)

#include <stdio.h>
#include <stdlib.h>

    int main()
    {
        char string[4] ={'c', '\0', 's', '\0'};
        printf("What do we love?\n");
        printf("Yes, we love:");
        for(int i=3; i>=0; i--){
                    printf("%d", string[i]);
        }
        return 0;
    }

I expected the output to display nothing. But I got the reverse of the array with whitespaces at the places where I’m guessing are the null characters? (output)

Bcoz I have tried using %d too instead of %c and found that those spaces apparently do have the ascii value of 0. (code with slight change + output + ascii table)

So, does this mean that a loop will not always treat a null character in a char array as an indicator of termination? Does this also mean null characters, which automatically get appended on the empty spaces of a char array actually, get printed as spaces in display, but we just say that it prints nothing in the output after it encounters null character only coz we see 'nothing' on display with most codes?

Petrichor
  • 1
  • 2
  • A *string* is an imaginary concept, as far as C is concerned. C has arrays of `char` that are absolutely equal to arrays of `int` (or `double` or any `struct`) except that its elements have type `char`. Arrays have a definite length, all its elements have a definite value (maybe a garbage value causing UB if accessed). What happens with *strings* is **a convention**. The creators of the C library **conventionated** (*sorry, English is not my first language*) that a `char` with `0` value would terminate the string (even if the underlying array could hold more data). – pmg Sep 25 '22 at 14:20
  • Other language **conventions** may be different!! I remember something about Pascal having a count value associated with a string (not sure how that works though). – pmg Sep 25 '22 at 14:24
  • If you're not using the Standard Library (no `strcpy()`, no `printf()`, ...) you do not need to worry about that **convention**. – pmg Sep 25 '22 at 14:25
  • But i did use printf tho. So does that count? – Petrichor Sep 26 '22 at 06:51
  • You only need to worry about the NUL byte when using `"%s"` in `printf()`. Your code uses only `"%d"` so the convention does not apply. – pmg Sep 26 '22 at 07:26
  • I wish i could upvote this lol – Petrichor Sep 26 '22 at 09:20
  • I see. I have tried using %s for char array, which I initialized in the code, and the printf did actually stop printing when it encountered a null character in the middle of the string. So, a null character only affects printf if the string format specifier is used. Curiously, if I take the input in the console however, the printf prints the whole string along with the null character in the middle inspite of using %s. I suppose in the console they treat '\0' as separate characters '\' and '0'. – Petrichor Sep 26 '22 at 09:31
  • You cannot easily enter a NUL byte from the console... see here: https://pastebin.com/V1qhcym0 – pmg Sep 26 '22 at 09:38
  • Makes sense. Thank You so much. The way you conveyed this information was very easy to understand. – Petrichor Sep 26 '22 at 10:14
  • You cannot input a `'\0'` to scanf from the console. at least not easily. You certainly cannot type a `'\0'` on the keyboard (`""` [two characters] is easy to type though). There is no key or key combination for NUL!! – pmg Sep 26 '22 at 11:45

1 Answers1

0

A null byte is used in a char array to designate the end of a string. Functions that operate on strings such as strcpy, strcmp, and the %s format specifier for printf, will look for a null byte to find the end of a string.

You're not treating string as a string, but as just an array of char. So it doesn't matter whether or not a particular element of the array has the value 0 as you're not treating that value as special in any way. You're just printing the decimal value of each of the elements of the array.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • But if printf is looking for a null character to end the string, why doesn't the loop stop after encountering a null character at its first iteration, hence printing absolutely nothing on display? – Petrichor Sep 25 '22 at 13:34
  • @Petrichor Your loop doesn't stop till `i` is -1. The contents of the array have no relevance to that. – Shawn Sep 25 '22 at 13:47
  • wait, do mean 'not treating string as a string' as in the the string data type in java. Does this mean the loop or printf in java stops after they encounter a null character in a string, but the char array in C, which we treat as a string by word of mouth, doesn't behave the same way? Is that why it prints the null characters as spaces? – Petrichor Sep 25 '22 at 13:59
  • @Petrichor: Re “if printf is looking for a null character to end the string”: It is not. `%c` or `%d` tell `printf` to accept an `int` argument and print it as a character or a decimal numeral, respectively, and `string[i]` passes `printf` a single value from the array. `printf` operates on a string when you use the format `%s`, not when you use `%c` or `%d`, and then you pass it a whole string (by pointer to the first element), not a single element. – Eric Postpischil Sep 25 '22 at 14:21
  • So, if a null character is passed as a single character in an array, printf treats it as such? But if a whole string was passed using %s and char* with a null character at the end of the string, it would terminate upon encountering it? – Petrichor Sep 26 '22 at 06:58