-1

When I execute the following code program exits before I get to input any string for word 2. i.e scanf-word2 is not executed. Why? I am using mac and I can't make the 2nd scanf execute in both codelite and terminal(using make)

#include <stdio.h>

int main()
{
    char word1[20], word2[20];

    printf("enter word 1 : ");

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

    printf("enter word 2 : ");

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

}
VatsalSura
  • 926
  • 1
  • 11
  • 27
Uttaran
  • 25
  • 6
  • worked beautifully. thanks. – Uttaran Jul 16 '16 at 18:37
  • Possible duplicate of [scanf: "%\[^\n\]" skips the 2nd input but " %\[^\n\]" does not. why?](http://stackoverflow.com/questions/6083045/scanf-n-skips-the-2nd-input-but-n-does-not-why), [Simple C scanf does not work?](https://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work), [C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf](https://stackoverflow.com/questions/9562218/c-multiple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s)... etc. etc. etc. – Jeff Mercado Jul 16 '16 at 18:45

4 Answers4

4

You are sending wrong argument type into scanf() for scanning strings and not checking for whitespaces... Instead use scanf this way :

scanf("%19[^\n]",word1); 
//and also
scanf(" %19[^\n]",word2); 
// a space in front to consume white spaces and don't put & before string variable 
  • While scanning a string using scanf() the argument to be sent must be of the type char* but not char
  • here, word1 and word2 are of the type char* whereas word1[i] and word2[i] are of the type char
  • %19[^\n] would allow scanning only 19 characters including whitespaces (i.e, '\0', '\n' ' ') and will leave the last index for terminating null character '\0'. Here, 19 iss known as width field (click to know more)

Further reading :

Why does everyone say not to use scanf? What should I use instead?

Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • 1
    19 is a random value you chose? – Uttaran Jul 16 '16 at 18:58
  • Nope... Since 20 is the max size of string... %19[^\n] allow scanning only 19 characters including whitespaces and will leave the last block for terminating null character – Cherubim Jul 16 '16 at 19:00
2

If you read the manual page scanf

Then this bit does not make sense

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

It should be

if (scanf("%19[^\n]",word1) == 1) ....

And ditto for word2

EDIT

For word 2

if (scanf("\n%19[^\n]",word2) == 1) ....
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

Prevent buffer overflow by passing the maximum number of characters to scanf and skip the '\n' with a dummy format:

#include <stdio.h>

int main(void) {
    char word1[20], word2[20];

    printf("Enter word 1: ");
    fflush(stdout);  // make sure prompt is output
    if (scanf("%19[^\n]%*c", word1) < 1)
        return 1;

    printf("Enter word 2: ");
    fflush(stdout);  // make sure prompt is output
    if (scanf("%19[^\n]%*c", word2) < 1)
        return 1;

    return 0;
}

Note that using scanf to read lines has some side effects: you cannot read an empty line this way and you have to hard code the buffer size in the format string in an awkward way.

I would instead suggest using fgets() and stripping the trailing '\n' this way:

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

int main(void) {
    char word1[20], word2[20];

    printf("enter word 1 : ");
    fflush(stdout);  // make sure prompt is output
    if (fgets(word1, sizeof word1, stdin) == NULL)
        return 1;

    word1[strcspn(word1, "\n")] = '\0'; // strip the \n if any

    printf("enter word 2 : ");
    fflush(stdout);  // make sure prompt is output
    if (fgets(word2, sizeof word2, stdin) == NULL)
        return 1;

    word2[strcspn(word2, "\n")] = '\0'; // strip the \n if any

    printf("word1: |%s|, word2: |%s|\n", word1, word2);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Perhaps checking the return values `scanf` you be a good idea – Ed Heal Jul 16 '16 at 18:47
  • @EdHeal: indeed it would be a good idea, I'll update the answer, but it would be good to use `word1` and `word2` too;-) – chqrlie Jul 16 '16 at 19:44
  • Perhaps adding `fflush(stdout);` after the `printf` may be required – Ed Heal Jul 16 '16 at 19:46
  • @EdHeal: Flushing `stdout` is usually not required if both `stdin` and `stdout` are bound to the terminal, but I guess it may be useful on some systems. – chqrlie Jul 16 '16 at 19:52
-1

You need to add getchar() before second scanf(); There is already a '\n' char in the buffer. In the second scanf() operation it reads that character and terminates the program.

Denis
  • 1,219
  • 1
  • 10
  • 15