why the following code gives the first mobile no. irrespective of name

110 views Asked by At

Please explain why the following code gives the first mobile no. irrespective of entered name:

#include<stdio.h>
#include<conio.h>

int mobileno(char[]);

int main(){

 char input[20];
 gets(input); 
 printf("%s",input);          
 int x;
 x = mobileno(input);  
printf("%d",x);
switch(x){
          case 0:printf("7255222319");break;
          case 1:printf("9404521113");break;
          case 2:printf("9326513553");break;
          case 3:printf("9845621611");break;
          case 4:printf("4556312390");break;
          case 5:printf("9245178190");break;
          case 6:printf("9214188214");break;
          case 7:printf("7014773244");break;
          case 8:printf("7898888044");break; 

          };    

    getch(); 
    return 0;
}

int mobileno(char s[20]){


    if( s="katrina" )
    {return 1;}else{
    if( s="shahid" ){return 2;}else{
    if( s="ranbir" ){return 3;}else{
    if( s="sharukh" ){return 4;}else{
    if( s="hema" ){return 5;}else{
    if( s="amitabh" ){return 6;}else{
    if( s="shashnk" ){return 7;}else{
    if( s="raj" ){return 8;}else{
    if( s="aishwarya" ){return 0;}else{

        return 9;}}}}}}}}};

    };
4

There are 4 answers

0
Sourav Ghosh On BEST ANSWER

You cannot compare strings using = (or even by ==, for that matter) operator. You need to use strcmp() for that.

In your code, inside mobileno() function,

if( s="katrina" )

is essentially trying to assign the base address of the string literal "katrina" to s. It is nowhere near a comparison.

That said,

  1. Never use gets(), it suffer from buffer overflow problem. Use the safer alternative, fgets()
  2. Recommended signature of main() is int main(void)
0
Tilman Hausherr On

this code

if( s="katrina" )

assigns "katrina" to s and the result is not 0, i.e. "true". To compare, use strcmp instead. While you can compare values with "==", don't use it for strings as it would only compare the address, and two identical strings could still have different addresses.

0
Ayushi Jha On

Two problems in your if conditions:

if( s="katrina" )

Assignment instead of equality :

a=b means assigning the value of b to a.

a==b' means checking if both a and b have same values.

Char array comparison: Use strcmp() instead:

 if( strcmp(s,"katrina")==0 )
0
Vlad from Moscow On

In this function

int mobileno(char s[20]){


    if( s="katrina" )
    {return 1;}else{
    if( s="shahid" ){return 2;}else{
    if( s="ranbir" ){return 3;}else{
    if( s="sharukh" ){return 4;}else{
    if( s="hema" ){return 5;}else{
    if( s="amitabh" ){return 6;}else{
    if( s="shashnk" ){return 7;}else{
    if( s="raj" ){return 8;}else{
    if( s="aishwarya" ){return 0;}else{

        return 9;}}}}}}}}};

    };

there is used the assignment operator = that assigns the address of the first characters of string literals in if conditions. For example in thsi statement

    if( s="katrina" )

pointer s is assigned to the address of the first character of string literal "katrina" that is evidently is not equal to NULL. So the condition is alway true.:)

If you want to compare to strings you should use standard C function strcmp declared in header <string.h>

Rewrite the function the following way

#include <string.h>

//...

int mobileno( const char *s )
{
    const char *name[] = 
    {
        "aishwarya", "katrina", "shahid", "ranbir", "sharukh",
        "hema", "amitabh", "shashnk", "raj"
    };
    const size_t N = sizeof( name ) / sizeof( *name );

    size_t i = 0;

    while ( i < N && strcmp( s, name[i] ) != 0 ) i++;

    return i;
}

Take into account that function gets is not a C Standard function any more because it is unsafe. Use instead function fgets.