How to correctly input a string in C

2019-02-26 01:38发布

问题:

I am currently learning C, and so I wanted to make a program that asks the user to input a string and to output the number of characters that were entered, the code compiles fine, when I enter just 1 character it does fine, but when I enter 2 or more characters, no matter what number of character I enter, it will always say there is just one character and crashes after that. This is my code and I can't figure out what is wrong.

int main(void)
{
    int siz;
    char i[] = "";

    printf("Enter a string.\n");
    scanf("%s", i);

    siz = sizeof(i)/sizeof(char);

    printf("%d", siz);
    getch();
    return 0;
}

I am currently learning to program, so if there is a way to do it using the same scanf() function I will appreciate that since I haven't learned how to use any other function and probably won't understand how it works.

回答1:

Please, FORGET that scanf exists. The problem you are running into, whilst caused mostly by your understandable inexperience, will continue to BITE you even when you have experience - until you stop.

Here is why:

scanf will read the input, and put the result in the char buffer you provided. However, it will make no check to make sure there is enough space. If it needs more space than you provided, it will overwrite other memory locations - often with disastrous consequences.

A safer method uses fgets - this is a function that does broadly the same thing as scanf, but it will only read in as many characters as you created space for (or: as you say you created space for).

Other observation: sizeof can only evaluate the size known at compile time : the number of bytes taken by a primitive type (int, double, etc) or size of a fixed array (like int i[100];). It cannot be used to determine the size during the program (if the "size" is a thing that changes).

Your program would look like this:

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

#define BUFLEN 100          // your buffer length

int main(void)    // <<< for correctness, include 'void'
{
    int siz;
    char i[BUFLEN];    // <<< now you have space for a 99 character string plus the '\0'

    printf("Enter a string.\n");
    fgets(i, BUFLEN, stdin);   // read the input, copy the first BUFLEN characters to i

    siz = sizeof(i)/sizeof(char);  // it turns out that this will give you the answer BUFLEN
                                   // probably not what you wanted. 'sizeof' gives size of array in
                                   // this case, not size of string
                                   // also not

    siz = strlen(i) - 1;           // strlen is a function that is declared in string.h
                                   // it produces the string length
                                   // subtract 1 if you don't want to count \n
    printf("The string length is %d\n", siz);  // don't just print the number, say what it is
                                               // and end with a newline: \n
    printf("hit <return> to exit program\n");    // tell user what to do next!
    getc(stdin);
    return 0;
}

I hope this helps.

update you asked the reasonable follow-up question: "how do I know the string was too long".

See this code snippet for inspiration:

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

#define N 50

int main(void) {
  char a[N];
  char *b;

  printf("enter a string:\n");

  b = fgets(a, N, stdin);

  if(b == NULL) {
    printf("an error occurred reading input!\n"); // can't think how this would happen...
    return 0;
  }

  if (strlen(a) == N-1 && a[N-2] != '\n') {       // used all space, didn't get to end of line
    printf("string is too long!\n");
  }
  else {
    printf("The string is %s which is %d characters long\n", a, strlen(a)-1); // all went according to plan
  }

}

Remember that when you have space for N characters, the last character (at location N-1) must be a '\0' and since fgets includes the '\n' the largest string you can input is really N-2 characters long.



回答2:

This line:

char i[] = "";

is equivalent to:

char i[1] = {'\0'};

The array i has only one element, the program crashes because of buffer overflow.

I suggest you using fgets() to replace scanf() like this:

#include <stdio.h>
#define MAX_LEN 1024

int main(void)
{
    char line[MAX_LEN];
    if (fgets(line, sizeof(line), stdin) != NULL)
        printf("%zu\n", strlen(line) - 1);
    return 0;
}

The length is decremented by 1 because fgets() would store the new line character at the end.



回答3:

The problem is here:

char i[] = "";

You are essentially creating a char array with a size of 1 due to setting it equal to "";

Instead, use a buffer with a larger size:

char i[128]; /* You can also malloc space if you desire. */
scanf("%s", i);

See the link below to a similar question if you want to include spaces in your input string. There is also some good input there regarding scanf alternatives.

How do you allow spaces to be entered using scanf?



回答4:

That's because char i[] = ""; is actually an one element array.

Strings in C are stored as the text which ends with \0 (char of value 0). You should use bigger buffer as others said, for example:

char i[100];
scanf("%s", i);

Then, when calculating length of this string you need to search for the \0 char.

int length = 0;
while (i[length] != '\0')
{
    length++;
}

After running this code length contains length of the specified input.



回答5:

You need to allocate space where it will put the input data. In your program, you can allocate space like:

char i[] = "                                     ";

Which will be ok. But, using malloc is better. Check out the man pages.