Difference between %c and %s in scanf()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akk003
    New Member
    • Aug 2006
    • 11

    Difference between %c and %s in scanf()

    Hello

    I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:

    Code:
    int loop = 1;
    char userIN;
    while(loop)
    {
    printf("\nIf you want to retrieve tuples prompt y/n: \t");
    //items_read = scanf("%s",&userIN);
    scanf("%s",&userIN);
    if(userIN=='n'||userIN=='N')
    {
    loop = 0;
    printf("\n user prompt: %c \n",userIN);
    break;
    }
    else if((userIN=='y')||(userIN=='Y'))
    {
    loop = 1;
    printf("\n user prompt: %c \n",userIN);
    }
    else
    {
    printf("Invalid Response, exiting:\t");
    printf("\n user prompt: %c \n",userIN);
    break;
    } 
    }

    and why does it break in this code(only after the first prompt):
    Code:
    int loop = 1;
    char userIN;
    while(loop)
    {
    printf("\nIf you want to retrieve tuples prompt y/n: \t");
    //items_read = scanf("%s",&userIN);
    scanf("%c",&userIN);
    if(userIN=='n'||userIN=='N')
    {
    loop = 0;
    printf("\n user prompt: %c \n",userIN);
    break;
    }
    else if((userIN=='y')||(userIN=='Y'))
    {
    loop = 1;
    printf("\n user prompt: %c \n",userIN);
    }
    else
    {
    printf("Invalid Response, exiting:\t");
    printf("\n user prompt: %c \n",userIN);
    break;
    } 
    }
    The only difference between the two codes is in the scanf().

    Thanks!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    If you try to read a string with a character then what happens is array overwrite.
    Since you are passing the character variables address you wont even get a compiler error for that.

    Raghuram

    Comment

    • romcab
      New Member
      • Sep 2007
      • 108

      #3
      %c - accepts a character
      %s - accepts a string

      Hope this helps.

      Originally posted by akk003
      Hello

      I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:

      Code:
      int loop = 1;
      char userIN;
      while(loop)
      {
      printf("\nIf you want to retrieve tuples prompt y/n: \t");
      //items_read = scanf("%s",&userIN);
      scanf("%s",&userIN);
      if(userIN=='n'||userIN=='N')
      {
      loop = 0;
      printf("\n user prompt: %c \n",userIN);
      break;
      }
      else if((userIN=='y')||(userIN=='Y'))
      {
      loop = 1;
      printf("\n user prompt: %c \n",userIN);
      }
      else
      {
      printf("Invalid Response, exiting:\t");
      printf("\n user prompt: %c \n",userIN);
      break;
      } 
      }

      and why does it break in this code(only after the first prompt):
      Code:
      int loop = 1;
      char userIN;
      while(loop)
      {
      printf("\nIf you want to retrieve tuples prompt y/n: \t");
      //items_read = scanf("%s",&userIN);
      scanf("%c",&userIN);
      if(userIN=='n'||userIN=='N')
      {
      loop = 0;
      printf("\n user prompt: %c \n",userIN);
      break;
      }
      else if((userIN=='y')||(userIN=='Y'))
      {
      loop = 1;
      printf("\n user prompt: %c \n",userIN);
      }
      else
      {
      printf("Invalid Response, exiting:\t");
      printf("\n user prompt: %c \n",userIN);
      break;
      } 
      }
      The only difference between the two codes is in the scanf().

      Thanks!

      Comment

      • akk003
        New Member
        • Aug 2006
        • 11

        #4
        Originally posted by gpraghuram
        If you try to read a string with a character then what happens is array overwrite.
        Since you are passing the character variables address you wont even get a compiler error for that.

        Raghuram
        Thankyou for your prompt reply. But userIN happens to be a char variable and it isn't an array.

        Comment

        • Parul Bagadia
          New Member
          • Mar 2008
          • 188

          #5
          Even i had got the same problem ; when i had used do-while loop;
          actually what happens is; many times when we say %c; a value is taken from the buffer; any garbagde value which is already stored there.
          Where as a new value is accepted in case of %s..

          Comment

          • akk003
            New Member
            • Aug 2006
            • 11

            #6
            thankyou all for the quick responses !!!
            I now understand the problem which Parul just explained.

            Comment

            • worldwidegeneration
              New Member
              • Mar 2008
              • 4

              #7
              Hey, you could try using gets() for a string or getch() for a character, and getche() to echo the reply back to the user(so they see what they typed in). Also, for those functions you might need to
              Code:
               #include <conio.h>
              i tend to only use scanf() when reading in number data.

              goodluck

              Comment

              Working...