I am little bit confuse in the scanning of numbers and string in c programming. I wrote program to scan numbers from user
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
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 ?
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 ?