-1

I'm a newbie in programming. It is confusing me when I taking input of a char array after scanning an integer. It is not working properly. The code is following:

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

int main()
{
    char a[30];
    int x,y;
    scanf("%d",&x);
    scanf("%[^\n]",a);
    scanf("%d",&y);
    printf("%d\n%s\n%d",x,a,y);
    return 0;
}

Output is following: enter image description here

  • 4
    And this is why you check the return value of `scanf()`. – EOF Jun 09 '16 at 11:25
  • I can't understand this. – Md. Ikramul Murad Jun 09 '16 at 11:27
  • C11 draft standard n1570: *7.21.6.2 The fscanf function Returns 16 The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.* – EOF Jun 09 '16 at 11:35
  • Possible duplicate of [Scanf is not scanning %c character but skips the statement, why is that?](http://stackoverflow.com/questions/32643397/scanf-is-not-scanning-c-character-but-skips-the-statement-why-is-that) – Cherubim Jun 09 '16 at 11:57
  • Unless an answer recommends checking the return value from `scanf()`, it is not that good an answer. Save yourself time 1) always check the return value of an input function. Read documents to know what return value is expected. 2) better to use `fgets()` than `scanf()`. – chux - Reinstate Monica Jun 09 '16 at 13:56

4 Answers4

2

The problem is due to the white spaces.After scanf("%d",&x); the last entered '\n' character is taken and saved the string a of scanf("%[^\n]",a).


To avoid this give space in scanf() statement

scanf(" %[^\n]",a);//give a space

Why to give a space?

By giving a space,the compiler consumes the '\n' character or any other white space ('\0','\t' or ' ' ) from the previous scanf()


your code :

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

int main()
{
    char a[30];
    int x,y;
    scanf("%d",&x);
    scanf(" %[^\n]",a);//give a space
    scanf("%d",&y);
    printf("%d\n%s\n%d",x,a,y);
    return 0;
}
Cherubim
  • 5,287
  • 3
  • 20
  • 37
1

Replace scanf("%[^\n]",a); with scanf(" %99[^\n]", a);

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

int main()
{
    char a[30];
    int x,y;

    scanf("%d",&x);
    scanf("%s",a);            // get char array without inputing space
    scanf(" %99[^\n]", a);    // get char array, allowing inputing space
    scanf("%d",&y);
    printf("%d\n%s\n%d\n",x,a,y);
    return 0;
}
Cherubim
  • 5,287
  • 3
  • 20
  • 37
1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[30];
    int x,y;
    scanf("%d",&x);
    fflush(stdin);
    scanf("%[^\n]",a);
    fflush(stdin);
    scanf("%d",&y);
    printf("%d\n%s\n%d",x,a,y);
    return 0;
}

This also works. The same goes here, /0 at the end adds up to the character scan and interferes. Using fflush(stdin) will discard any unnecessary input data including the /0.

Correct me if I am wrong as I too am a newbie at coding. :p

Bsal
  • 11
  • 4
0

Instead of %d use %d\n to consume the newline, so that the following command will not just read nothing:

scanf("%d\n",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jan Christoph Terasa
  • 5,781
  • 24
  • 34