Maker Pro
Maker Pro

Scanning of numbers and string in c

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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 ?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,886
Joined
Nov 17, 2011
Messages
12,886
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

Moderator
Moderator
Nov 17, 2011
12,886
Joined
Nov 17, 2011
Messages
12,886
What's wrong happening in my program. I just want scan string without using any library, How to scan string without using any library ?
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.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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
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

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.
I don't want to use any other then #include<stdio.h>
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,886
Joined
Nov 17, 2011
Messages
12,886
hello.c:10:10: warning: comparison between pointer and integer
if(name == "root")
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.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,886
Joined
Nov 17, 2011
Messages
12,886
many compiler does not have inbuilt string.h library
Th elibrary is not built into the compiler, it is a separate file which is compiled when required.

I didn't understand how to use with my program
???
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.
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
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
 
Top