Scanning of numbers and string in c

Discussion in 'Microcontrollers and Programming' started by vead, Jan 12, 2018 at 12:11 PM.

  1. vead

    vead

    Joined:
    Nov 27, 2011
    Messages:
    394
    Likes Received:
    11
    I am little bit confuse in the scanning of numbers and string in c programming. I wrote program to scan numbers from user
    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int number;
      
        printf("Enter user input :");
        scanf ("%d",&number);
      
        if(number == 1)
        {
            printf("Number is 1");
        }
      
        else if (number == 2)
        {
            printf("Number is 2");
        }
      
        else if (number == 3)
        {
            printf("Number is 3");
        }
        else
        {
            printf("Invalid Number");
        }
    
        return 0;
    }
    compiler output
    Enter user input :1
    Number is 1
    Enter user input :2
    Number is 2
    Enter user input :3
    Number is 3
    Enter user input :4
    Invalid Number

    This program is able to scan input numbers

    After that I tried to scan string but compiler showing error message

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        char name;
      
        printf("Enter user input :");
        scanf ("%s",&name);
      
        if(name == root)
        {
            printf("Name is root");
        }
      
        else if (name == marsh)
        {
            printf("Name is marsh");
        }
      
        else if (name == belly)
        {
            printf("Name is belly");
        }
        else
        {
            printf("Invalid Name");
        }
    
        return 0;
    }
    
    hello.c: In function 'main':
    hello.c:10:13: error: 'root' undeclared (first use in this function)
    if(name == root)
    ^~~~
    hello.c:10:13: note: each undeclared identifier is reported only once for each function it appears in
    hello.c:15:19: error: 'marsh' undeclared (first use in this function)
    else if (name == marsh)
    ^~~~~
    hello.c:20:19: error: 'belly' undeclared (first use in this function)
    else if (name == belly)
    ^~~~~
    What's wrong happening in my program. I just want scan string without using any library, How to scan string without using any library ?
     
    vead, Jan 12, 2018 at 12:11 PM
    #1
    1. Advertisements

  2. vead

    Harald Kapp Moderator Moderator

    Joined:
    Nov 17, 2011
    Messages:
    7,756
    Likes Received:
    1,382
    Location:
    Germany-Europe-Earth-Sol System-Milky Way-Laniakea
    You missed teh quotes.
    root is not a string.
    "root" is a string.

    The same goes for your other strings.

    btw.: for such selections the "select case" statement is much better suited than a long list of if...then...else
     
    Harald Kapp, Jan 12, 2018 at 12:19 PM
    #2
    1. Advertisements

  3. vead

    Harald Kapp Moderator Moderator

    Joined:
    Nov 17, 2011
    Messages:
    7,756
    Likes Received:
    1,382
    Location:
    Germany-Europe-Earth-Sol System-Milky Way-Laniakea
    You do use a library, you may not conscientiously be aware of. The library I'm speaking of is the C standard library, part of which you include by the statement "#include<stdio.h>". printf() and scanf() are the library functions you use from stdio.h.
     
    Harald Kapp, Jan 12, 2018 at 12:22 PM
    #3
  4. vead

    vead

    Joined:
    Nov 27, 2011
    Messages:
    394
    Likes Received:
    11
    hello.c: In function 'main':
    hello.c:10:10: warning: comparison between pointer and integer
    if(name == "root")
    ^~
    hello.c:15:16: warning: comparison between pointer and integer
    else if (name == "marsh")

    I understand the use of switch case statement in c programming

    Code:
    #include <stdio.h>
    
    int main()
     {
      int i;
    
      printf(" \n Enter a number between 1 and 4 : ");
      scanf("%d",&i);
    
      switch (i)
       {
         case 1:
          printf("one");
          break;
         case 2:
          printf("two");
          break;
         case 3:
          printf("three");
          break;
         case 4:
          printf("four");
          break;
         default:
          
          printf(" \n unrecognized number");
       } 
    but again problem is string. my program suppose to do if any user enter string program will scan that string and then string will check if it doesn't match then show invalid name and if it match then display name of string

    I don't want to use any other then #include<stdio.h>
     
    vead, Jan 12, 2018 at 2:07 PM
    #4
  5. vead

    Harald Kapp Moderator Moderator

    Joined:
    Nov 17, 2011
    Messages:
    7,756
    Likes Received:
    1,382
    Location:
    Germany-Europe-Earth-Sol System-Milky Way-Laniakea
    AN oversight of mine: you cannot compare strings this way in C. You need to use strcmp(), but then you need to include string.h. Or you write your own string comparison routine.

    Alternatively you could use a hash function to reduce the string to a number which you then can compare to another number. But this method has f+pitfalls of its own.
     
    Harald Kapp, Jan 12, 2018 at 3:00 PM
    #5
  6. vead

    vead

    Joined:
    Nov 27, 2011
    Messages:
    394
    Likes Received:
    11
    many compiler does not have inbuilt string.h library like keil. I just want to write my own string comparison routine

    I have visited in this link https://fresh2refresh.com/c-programming/c-function/string-h-library-functions/
    There are information about string.h library but I didn't understand how to use with my program
     
    vead, Jan 12, 2018 at 5:14 PM
    #6
  7. vead

    Harald Kapp Moderator Moderator

    Joined:
    Nov 17, 2011
    Messages:
    7,756
    Likes Received:
    1,382
    Location:
    Germany-Europe-Earth-Sol System-Milky Way-Laniakea
    Th elibrary is not built into the compiler, it is a separate file which is compiled when required.

    ???
    Like any other library routne (you already use printf(), for example)) If your develeopment environment doesn't come with the string.h library, you are forced to write your own comparison function, or borrow ot from the library of another development package.
    Have a look here.
     
    Harald Kapp, Jan 13, 2018 at 9:12 AM
    #7
  8. vead

    BobK

    Joined:
    Jan 5, 2010
    Messages:
    6,773
    Likes Received:
    1,374
    Location:
    Worcester, MA
    Also, in your string example you have this declaration:

    char name;

    name is a char, not a string. It can store only a single character, and you scan statement will go out of bounds, probably crashing your program. To define a string you declare an array of char that can store 1 more than the longest string you need to store:

    char name[11];

    would allow strings up to 10 characters in length.

    Bob
     
    BobK, Jan 13, 2018 at 1:12 PM
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.