Rayssen Zaouali
Rayssen Zaouali

Reputation: 1

How to get a whole sentence as input using scanf()

I want to get a character and a string and a sentence then write each one of them in a new line. I don't know what is wrong with my code.

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

int main(){

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    const int MAX_LEN=100;
    char ch;
    char s[MAX_LEN];
    char sentence[MAX_LEN];
    scanf ("%c",&ch);
    scanf("%s",&s);
    scanf("%[^\n]%*c", &sentence);

    printf("%c\n",ch);
    printf("%s\n", s);
    printf("%s\n",sentence);


    return 0;
}

Upvotes: 0

Views: 2506

Answers (1)

ryyker
ryyker

Reputation: 23226

Regarding

char s[MAX_LEN];
...
scanf("%s",&s);

Use of the & in this case is wrong. s is created here as an array type, so its symbol by itself already points to the location of the first element, (its address) and has type char *, which agrees with the "%s" format specifier used in the scanf statement. Prepending s with the & changes its type to char(*)[100], which is no longer compatible with "%s". Using mismatched format specifiers with scanf() invokes undefined behavior, which is far worse than your program simply being mal-formed, it is unpredictable.

Note that with warnings turned on (eg, in GCC by compiling with -Wall) it should result in a message describing all of this. On my system (not GCC) the following message is issued:

enter image description here

Likewise, when in debug mode with appropriate settings during runtime I see the following assert:

"Parameter type mismatch, expecting pointer to char but found pointer to aggregate or user defined."

By the way, when I allowed execution to continue after this warning message the code appeared to work normally, i.e. it output the content entered into stdin. But that is the nature of undefined behavior, anything can happen..

The fix here is to simply replace this:

scanf("%s",&s);

With either this:

scanf("%s",s);  //okay

or this;

fgets(s, sizeof(s), stdin);//better
s[strcspn(s, "\n")] = 0; // optional, if need to eliminate the newline

either approach works as a replacement for those places in your code where you use an & on an array.

Aside: It is also a good practice to use variable names that self describe themselves. Consider using something more descriptive than s, eg: char string[MAX_LEN]; or char string_array[MAX_LEN];.

Upvotes: 1

Related Questions