0

I am learning linked lists, when I use scanf to input a character, the code compiles fine but at run time it does not ask for input and skips the scanf statement.

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *ptr;
};
struct node* allocate();
struct node* create();
void display(struct node*);
int main()
{
    struct node *new;
    new=create();
    display(new);
    return 0;
}
struct node* allocate()
{
    struct node *temp;
    temp=(struct node*)malloc(sizeof(struct node));
    return temp;
}
struct node* create()
{
    struct node *start,*next;
    char ch;
    start=next=allocate();
    printf("Enter data:\n");
    scanf("%d",&start->data);
    perror("store data");
    start->ptr=NULL;
R1: printf("Do you want to enter more data? y or n::    ");
    scanf("%c", &ch); //Check for error here
    if(ch=='y'||ch=='Y')
    {
        while(ch=='y'||ch=='Y')
        {
            next->ptr=allocate();
            next=next->ptr;
            printf("Enter data:\n");
            scanf("%d",&next->data);
            next->ptr=NULL;
            printf("Do you want to enter more data? y or n::    ");
            scanf(" %c",&ch);
        }
    }    
    if(ch=='n'||ch=='N')
    {
        return start;
    }
    else
    {
        printf("Please enter correct option.\n");
        goto R1;
    }
}
void display(struct node* temp)
{
    printf("%d\n",temp->data);
    while(temp->ptr!=NULL)
    {
        temp=temp->ptr;
        printf("%d\n",temp->data);
    }      
}

Please Refer to the comment

Check for error here

in the code to know the statement i am referring to.

  • Now if i add a space before the the format specifier i.e. space before %c in scanf statement then my code runs fine. .

    scanf(" %c",&ch);
    

I face the same problem when I use getchar instead of scanf

ch=getchar();

when I run my code without using a space before the format specifier in scanf statement or I use getchar() statement, my program does not ask for input. It stores nothing in ch. Can anyone please explain me the reason behind it? why does scanf behave so differently with character data types?

Additional info:

  • Using GCC
  • Linux Kernel 3.6.11-4
  • OS Fedora 16 (64 bit)
  • intel i5 processor.

2 Answers2

2

Why does scanf behave so differently with character data types?

scanf() behaves differently because the types are different.

With number format specifiers like %i %u %e %f, scanf() discards leading white space. So " 123" and "123" both are read as 123.

With %c, scanf() takes 1 byte of input, any 1 byte, and returns it, including white space and \0.

With %s scanf() works like scanning numbers by ignoring leading white space. It scans in chars until another whitespace is found.

The format specifier %[...] works like %s in that it scans in multiple char, but the "..." part tells what to look for. It does not toss leading whitespace.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1
 why does scanf behave so differently with character data types?

That's because scanf is made to behave like that , in fact its a very complex function and one should try to avoid it until necessary . The main reason however , lies in the fact that a space in the format string means to skip over any whitespace before the next input item, except for %c.

So , scanf("%d%d", &n, &m) behaves the same as scanf("%d %d", &n, &m). For %c, adding a space character to the format string does make a difference. For example, if %c is preceded by a space in the format string, scanf() does skip to the first non-whitespace character. That is, the command scanf("%c", &ch) reads the first character encountered in input, and scanf(" %c",&ch) reads the first non-whitespace character encountered.

Please note that whitespace is also a character.

According to the C11 standard (7.21.6.2 The fscanf function, section #8)

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier

0decimal0
  • 3,884
  • 2
  • 24
  • 39