1

So I'm trying to get a scanf to accept strings and store it which will not carry into the next scanf. I'm quite new to C.

For example:

#include <stdio.h>
#include <stdlib.h>

int main(){

    char word1[100];
    char word2[100];

    printf("Enter a sentence:");
    scanf("%[^\n]s",&word1);
    printf("Enter a second sentence:");
    scanf("%[^\n]s",&word2);
    printf("%s %s",word1,word2);

    return 0;
}

But it'll display this:

Enter a sentence:I am 
Enter a second sentence:I am P)Γ
Press Enter to return to Quincy...

What I want it to display is:

Enter a sentence:I am
Enter a second sentence: bread !
I am bread!

Any advice?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
diggledoot
  • 691
  • 8
  • 22
  • 3
    `"%[^\n]s"` is wrong. When you specify a *character class* (e.g. `[...]`) you do NOT follow with a `'s'`. Further, a character class does not skip leading whitespace, so you will need to remove the `'\n'` that remains in your input buffer before your second input or you will suffer an input failure as the read terminates on the `'\n'` without any character read. `"%99[^\n]%*c"` would work -- and check the return to validate the conversion. (which is why new C programmers are encouraged to use `fgets` of POSIX `getline` for line oriented input as they consume the trialing newline.) – David C. Rankin Mar 08 '18 at 04:32
  • Take a look at [I need a explanation of fscanf parameter](https://stackoverflow.com/questions/49165131/i-need-a-explanation-of-fscanf-parameter) answered just a few minutes ago for further explanation. – David C. Rankin Mar 08 '18 at 04:37
  • If you want to read a line, why not use a function that reads lines? – David Schwartz Mar 08 '18 at 04:40
  • 1
    That's always the great mystery for new C programmers, why use a *formatted input* function to do *line oriented input*... – David C. Rankin Mar 08 '18 at 04:41
  • If obliged to use `scanf()`, to well use it, you need to defined what should happen 1) when a line of user input consists of only a `"\n"` and 2) what should code do if more than 99 characters entered? else the question is too broad. – chux - Reinstate Monica Mar 08 '18 at 04:55
  • Any advice? --> Always check the return value from input functions and do not use `scanf()`. Use `fgets()`. – chux - Reinstate Monica Mar 08 '18 at 04:56

3 Answers3

0

You have to use a escaping "\" inside a string

#include <stdio.h>
#include <stdlib.h>

int main(){

char word1[100];
char word2[100];
printf("Enter a sentence:");
scanf("%[^\\n]s",&word1);
printf("Enter a second sentence:");
scanf("%[^\\n]s",&word2);
printf("%s %s",word1,word2);
return 0;
}
Bashar
  • 453
  • 5
  • 14
  • 1
    When you specify a *character class* (e.g. `[...]`) you do NOT follow with a `'s'`... and you do NOT need to escape within the character class, not on Unix and not on Windoze. – David C. Rankin Mar 08 '18 at 04:39
0

There are 2 issues

  1. Use "%[^\n]%*c".
  2. use word1 and not &word1.

    //test.c
    #include < stdio.h >
    #include < stdlib.h >
    
    int main(){
    
        char word1[100];
        char word2[100];
        printf("Enter a sentence:");
        scanf("%[^\n]%*c",word1);
        printf("Enter a second sentence:");
        scanf("%[^\n]%*c",word2);
        printf("%s %s",word1,word2);
        return 0;
    }
    
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Syed Waris
  • 1,056
  • 1
  • 11
  • 16
  • 1
    Add 4-spaces before your code to format properly `:)` – David C. Rankin Mar 08 '18 at 04:41
  • @DavidC.Rankin. Thank You – Syed Waris Mar 08 '18 at 04:45
  • Sure. Now if you check the return of `scanf` (e.g. `if (scanf ("%99[^\n]%*c",word1) != 1) { /* handle error */ }`) it would really start looking good. (you must check the return of every call to `scanf`, and as a bonus trap the manually generated `EOF` separately `:)` – David C. Rankin Mar 08 '18 at 04:47
  • `scanf("%[^\n]%*c",word1);` has 2 short-comings: If the first character is a `'\n'`, `word1` is unchanged and `printf("%s %s",word1,word2);` is undefined behavior. 2) if more than 99 characters read, `scanf("%[^\n]%*c",word1);` will overflow `word1[]` and more undefined behavior. Consider `fgets()`. – chux - Reinstate Monica Mar 08 '18 at 04:48
0
int main(){

    char word1[100];
    char word2[100];

    printf("Enter a sentence:");
    scanf("%[^\n]s",&word1);

    getchar(); // clear the "\n" from stdin

    printf("Enter a second sentence:");
    scanf("%[^\n]s",&word2);
    printf("%s %s",word1,word2);
    return 0;
}