-3

I have the program in c

int main() {
    char str[1];
    gets(str);
    return 0;
}

I have tried to give the input '\0' to cause the str[0] to represent the null character but it's actually store the character \.

Is there an input that will cause that?. Thanks.

RishabhHardas
  • 495
  • 1
  • 5
  • 25
ylev
  • 27
  • 5
  • 6
    To begin with, never ***ever*** use `gets`. It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function, that have even been removed from the C specification. Secondly, that's not how input work. You could try to press `Ctrl-A` (followed by `Enter`) and hope for the best. – Some programmer dude Nov 01 '18 at 07:57
  • 1
    Also, what is the *real* problem you want to solve by giving the null-terminator as input? Why do you need to do that? – Some programmer dude Nov 01 '18 at 07:59
  • 1
    You could try `echo -n "" | myprog`, or you could use Control-D to terminate the input if you're at the beginning of a line. An input of any length other than zero will cause undefined behaviour, because `str` is only large enough to hold an empty string. If you want to catch null bytes in the input, perhaps you could use `getchar()` instead. – r3mainer Nov 01 '18 at 08:03
  • On which operating system are you running your program? – Basile Starynkevitch Nov 01 '18 at 08:10
  • 1
    What do you mean with `give the input '\0'`? Did you enter these four characters? In that case you would expect that your program interprets the entered text and converts it to the ASCII-null character. But there's no code for it. `gets` only reads a string into a buffer. That buffer is too small in your example program. – harper Nov 01 '18 at 09:57
  • 1
    @Someprogrammerdude `Ctrl-A` is usually `'\1'`, not `'\0'`. Some keyboards allow `Ctrl-@` for `'\0'`. – chux - Reinstate Monica Nov 01 '18 at 12:08
  • @harper "gets only reads a string into a buffer" --> In detail, `gets()` does not read a null character terminated _string_, but a `'\n'` terminated _line_ - which it then saves as a _string_. – chux - Reinstate Monica Nov 01 '18 at 12:14

3 Answers3

1

A NUL character cannot be inside a C string, since by definition it is ending a C string.

(The NUL character is '\0' -practically, a zero byte- and is of course different of the NULL pointer).

And your str is probably too small. Consider declaring it with char str[32]; and clearing it with memset(str, 0, sizeof(str)); before reading into it.

So what your user would input is not a string, but simply a sequence of bytes (you might read character by character a stream of bytes using getchar). Hence you cannot use fgets for reading (of course, forget about gets, it is obsolete, dangerous, and removed from the C11 standard. Read n1570 to check).

A different question is what should your user do to enter such a NUL character on the keyboard. Such details are operating system specific (on some systems there might be no way to type a NUL character, on other systems you might have to press several keys simultaneously, including the Ctrl key). You could consider some redirection from some binary file containing NUL bytes.

It is surprising (and very unusual) that you want to enter a NUL; I am guessing that you are mis-undestanding your homework.

Perhaps you want to handle the empty input case. For that, you need to explicitly detect the end-of-file condition (e.g. when getchar gives EOF, which is not a character). Beware that feof is only valid after some (failed) read operation.

Be sure to compile with all warnings and debug info (with GCC compiler, use gcc -Wall -Wextra -g). Read How to debug small programs

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • you're right there is no reason for a user to press NUL, I wanted the memory to hold the number 0 (which 0 is the null character ascii) which letter I could convert the 4 chars to integer (and get 0 in that integer) – ylev Nov 01 '18 at 10:00
1

How to receive a null character in gets function from the user input?

The obsolete, since c11, function gets() reads a line of text and then saves it (without the '\n') with an appended null character insuring a string.

gets()

The gets function reads characters from the input stream pointed to by stdin, into the array pointed to by s, until end-of-file is encountered or a new-line character is read. Any new-line character is discarded, and a null character is written immediately after the last character read into the array. C99 §7.19.7.7 2

line

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. C99dr §7.19.2 2

string

A string is a contiguous sequence of characters terminated by and including the first null character. C99dr §§7.1.1 1


Is there an input that will cause that?

Yes. Entering a null character on various keyboards is not easy. Sometimes a Ctrl Shift 2 (Ctrl @) will work. Some keyboards lack or obscure any way to input a null character. Usually a re-directed input from a file that contains a null character into stdin will suffice.

Entering \ 0 Enter is not entering a null character.


An important consideration is that with gets() or fgets(), the input of a null character is not special in its reception. A following Enter ('\n') or end-of-file-signal is still needed. After which a null character is appended.

With gets(), the size of the input buffer needs to be at least 2 to read in any input more than an empty line of just Enter, so char str[1]; is too small to save an input of a null character with the appended null character.

If the input was Enter, gets(str) would save no input in str[0] and then append a null character to str[0].

If the input was Ctrl @, Enter, gets(str) would save the user inputted null character to str[0] and then append a null character to str[1].


Let us try an example with fgets().

int main(void) {
  // Form a file of 1 byte
  FILE *f = fopen("zero.txt", "w");
  if (f == NULL) {
    return EXIT_FAILURE;
  }
  fputc('\0', f);
  fclose(f);

  f = fopen("zero.txt", "r");
  if (f == NULL) {
    return EXIT_FAILURE;
  }
  char buf[80];
  memset(buf, 42, sizeof buf);
  fgets(buf, sizeof buf, f);
  for (int i = 0; i < 5; i++) {
    printf("%d\n", buf[i]);
  }
  fclose(f);
  return 0;
}

Output

0  // from the file zero.txt
0  // the appended null character
42
42
42

Text processing functions like fgets(), scanfs(), fscanfs() and the obsolete gets() having to cope with a user inputed null character is a corner case. Such input is either a mistake or, more likely, a hacker attempt to break the program.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

when we declare char str[1],we give instruction to compiler that str is string having 1 byte,'\' is also one charactor. string defination also states that char array ending with '\0' is called string.

As per your declaration we can give only one char which '\'.

Yash Shah
  • 158
  • 2
  • 13