简体   繁体   中英

C - Not printing out the whole string

int main()
{
    //Define Variables

    char studentName;

    //Print instructions to fill the data in the screen
    printf("Please type in the Students name:\n");
    scanf("%s", &studentName);
    printf("\n\n%s", &studentName);

    return 0;
}

Seeing the above code, I am only printing to screen out the first word when I type in a sentence.

I know it is a basic thing, but I am just starting with plain C.

Read scanf(3) documentation. For %s is says

   s      Matches a sequence of non-white-space characters; the next
          pointer must be a pointer to character array that is long
          enough to hold the input sequence and the terminating null
          byte ('\0'), which is added automatically.  The input string
          stops at white space or at the maximum field width, whichever
          occurs first.

So your code is wrong, because it should have an array for studentName ie

char studentName[32];
scanf("%s", studentName);

which is still dangerous because of possible buffer overflow (eg if you type a name of 32 or more letters). Using %32s instead of %s might be safer.

Take also the habit of compiling with all warnings enabled and with debugging information (ie if using GCC with gcc -Wall -g ). Some compilers might have warned you. Learn to use your debugger (such as gdb ).

Also, take the habit of ending -not starting- your printf format string with \\n (or else call fflush , see fflush(3) ).

Learn about undefined behavior . Your program had some! And it misses a #include <stdio.h> directive (as the first non-comment significant line).

BTW, reading existing free software code in C will also teach you many things.

There are three problems with your code:

  • You are writing a string into a block of memory allocated for a single character; this is undefined behavior
  • You are printing a string from a block of memory allocated for a single character - also an undefined behavior
  • You are using scanf to read a string with spaces; %s stops at the first space or end-of-line character.

One way to fix this would be using fgets , like this:

char studentName[100];

//Print instructions to fill the data in the screen
printf("Please type in the Students name:\n");
fgets(studentName, 100, stdin);
printf("\n\n%s", &studentName);

return 0;

Try scanf("%[^\\n]", &studentName); instead of scanf("%s", &studentName);

This is happening because %s stops reading the input as soon as a white space is encountered.

To avoid this what you can do is declare an array of the length required for your string.

Then use this command to input the string:-

scanf("%[^\n]s",arr);

This way scanf will continue to read characters unless a '\\n' is encountered, in other words you press the enter key on your keyboard. This gives a new line signal and the input stops.

int main()
{
    //Define Variables

    char studentName[50];

    //Print instructions to fill the data in the screen
    printf("Please type in the Students name:\n");
    scanf("%[^\n]s", &studentName);
    printf("\n\n%s", &studentName);

    return 0;
}

Alternatively you can also use the gets() and puts() method. This will really ease your work if you are writing a code for a very basic problem .

[EDIT] : As dasblinkenlight has pointed out...I will also not recommend you to use the gets function since it has been deprecated.

int main()
    {
        //Define Variables

        char studentName[50];

        //Print instructions to fill the data in the screen
        printf("Please type in the Students name:\n");
        gets(studentName); printf("\n\n");
        puts(studentName);

        return 0;
    }

Your problem is here

char studentName;

It is a char , not a string.

Try:

  1. Define it as an array of chars like char studenName[SIZE]; .
  2. allocating memory dynamically using malloc :

.

char buffer[MAX_SIZE];
scanf("%s", &buffer);
char * studentName = malloc (sizeof(buffer) + 1);
strcpy (studentName , buffer);

make the changes below and try it. I added [80] after the studentName definition, to tell the compiler that studentName is an array of 80 characters (otherwise the compiler would treat it as only one char). Also, the & symbol before studentName is not necessary, because the name of the array implicitly implies a pointer.

int main()
{
  //Define Variables

  char studentName[80];

  //Print instructions to fill the data in the screen
  printf("Please type in the Students name:\n");
  scanf("%s", studentName);
  printf("\n\n%s", studentName);

  return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM