#include<stdio.h>
#include<conio.h>
#include<string.h>
int x;
char name[20];

main()
{
      printf("Exercise no. 10 ESN 211-9A MWF 7:30-9;30\n\n");
      printf("Enter your age:\n");
      scanf("%d",&x);
      if (x<18)
         printf("UNDERAGE!\n\n");
      else if (x==18)
         printf("STUDENT LICENSE POSSIBLE\n\n");
      else 
         printf("PROFESSIONAL LICENSE AVAILABLE\n\n");
      printf("Enter the result of your drug test: \n");
      scanf("%s",&name);
      if ( name == "positive" )
         printf("HOLD LICENSE");
      else 
           printf("RELEASE LICENSE");
      getch();
      }

The program is working but the part about the drug test is wrong. When i input positive it outputs RELEASE LICENSE instead of HOLD LICENSE. What seems to be the problem of my program? am i missing something? or is it just simply wrong? :D please help me. thank you! :)

Recommended Answers

All 8 Replies

#include<stdio.h>
#include<conio.h>
#include<string.h>
int x;
char name[20];

main()
{
      printf("Exercise no. 10 ESN 211-9A MWF 7:30-9;30\n\n");
      printf("Enter your age:\n");
      scanf("%d",&x);
      if (x<18)
         printf("UNDERAGE!\n\n");
      else if (x==18)
         printf("STUDENT LICENSE POSSIBLE\n\n");
      else 
         printf("PROFESSIONAL LICENSE AVAILABLE\n\n");
      printf("Enter the result of your drug test: \n");
      scanf("%s",&name);

      /* C doesn't support strings as basic data types, so you can't compare them
         directly. Use strcmp() (from string.h), to compare strings. Strcmp returns
         either a value < 0 (less than), 0 (equal strings), or > 0 (greater than). 

         Practice with it, and see which results you get with different comparisons.

      */

      if((strcmp( name, "positive" )) == 0) //note 6 parenthesis
         printf("HOLD LICENSE");
      else 
           printf("RELEASE LICENSE");
      getch();
      }

The return from strcmp(string1, string2) may indicate the number of letters difference, between the two strings, but that is not guaranteed by the C standard. Only < 0, 8, and > 0 are guaranteed. Another way to do the above if() statement would be this:

int result;
result = strcmp(string1, string2);
if(result == 0)
  printf("Strings are equal\n");

YES,as ADAK siad, strcmp() fn should be used for string comparision and I think there should not be & sign in scanf() when we are taking string as input.
it should be:-

scanf("%s",name);

Adak:

if((strcmp( name, "positive" )) == 0); //note 6 parenthesis
if( strcmp( name, "positive" )  == 0); //note 4 parenthesis

Since strcmp is not a macro, there is no compile difference between those two lines. Why do you add the extra parenthesis?

YES,as ADAK siad, strcmp() fn should be used for string comparision and I think there should not be & sign in scanf() when we are taking string as input.
it should be:-
C Syntax (Toggle Plain Text)

scanf("%s",name);

why removed the & sign in the name? :D is the & sign only applicable when you are inputting integers and such?

C Syntax (Toggle Plain Text)

#include<stdio.h>
#include<conio.h>
#include<string.h>
int x;
char name[20];

main()
{
printf("Exercise no. 10 ESN 211-9A MWF 7:30-9;30\n\n");
printf("Enter your age:\n");
scanf("%d",&x);
if (x<18)
printf("UNDERAGE!\n\n");
else if (x==18)
printf("STUDENT LICENSE POSSIBLE\n\n");
else
printf("PROFESSIONAL LICENSE AVAILABLE\n\n");
printf("Enter the result of your drug test: \n");
scanf("%s",&name);

/* C doesn't support strings as basic data types, so you can't compare them
directly. Use strcmp() (from string.h), to compare strings. Strcmp returns
either a value < 0 (less than), 0 (equal strings), or > 0 (greater than).

Practice with it, and see which results you get with different comparisons.

*/

if((strcmp( name, "positive" )) == 0) //note 6 parenthesis
printf("HOLD LICENSE");
else
printf("RELEASE LICENSE");
getch();
}


The return from strcmp(string1, string2) may indicate the number of letters difference, between the two strings, but that is not guaranteed by the C standard. Only < 0, 8, and > 0 are guaranteed. Another way to do the above if() statement would be this:

C Syntax (Toggle Plain Text)

int result;
result = strcmp(string1, string2);
if(result == 0)
printf("Strings are equal\n");

so when it comes to C there is no string comparison? :D and i don't get the latter part the int result part.

why removed the & sign in the name?

The following is a common oversimplification: "Always use & on variables in scanf()". While the intention is fine--don't confuse beginners by introducing pointers too soon--but the simplification is likely to cause just as much confusion. The reality is that because scanf() writes to the variables, it needs a reference to the actual object.

C passes function arguments by value, which means that inside of the function, you're working with a copy of the object. This can be shown quite easily with a simple test program:

#include <stdio.h>

void foo(int obj)
{
    obj = 12345;
}

int main(void)
{
    int obj = 0;
    
    foo(obj);
    printf("%d\n", obj); /* Uh-oh, prints 0 */
    
    return 0;
}

Because foo()'s obj is a completely different object from main()'s obj , the assignment of 12345 doesn't apply to the obj that we're printing. To get a reference to the original object, we would instead pass a pointer (ie. the address of the original object):

#include <stdio.h>

void foo(int *obj)
{
    *obj = 12345;
}

int main(void)
{
    int obj = 0;
    
    foo(&obj);
    printf("%d\n", obj); /* Yay, prints 12345 */
    
    return 0;
}

That's how scanf() works; it accepts a pointer to the original object and assigns to it. But if the object is already a pointer, there's no need to pass its address. In fact, that's a bad thing because even though the address will be the same, the type of the object will be different from what scanf() is expecting.

An array name in value context is already a pointer, and that's why the correct code for scanf("%s", name); doesn't include the & operator. Adding it would change the type from char* to char** , which could potentially cause errors, and technically invokes undefined behavior.

so when it comes to C there is no string comparison?

Strings in C are not first class objects. Rather, strings are an extension of arrays of char by adding an invariant rule that the array always ends with a null character ('\0'). No operator overloading is done on arrays of char, so the same rules apply there as do for arrays of other types.

That is, an array name in value context (such as a comparison expression) will be converted to a pointer to the first element. Then the pointer will be used for the comparison. So string1 == string2 will be interpreted as &string1[0] == &string2[0] . The same goes for string literals, string1 == "test" will be interpreted as &string1[0] == &"test"[0] .

This is why you rely on a library function that honors the string invariant:

int strcmp(const char *a, const char *b)
{
    int diff = 0;

    while ((diff = *(unsigned char*)a - *(unsigned char*)b) == 0 && *a != '\0') {
        ++a;
        ++b;
    }
    
    if (diff == 0)
        return 0;

    return diff < 0 ? -1 : +1;
}

and i don't get the latter part the int result part.

strcmp() returns a comparison, not equality. So you can tell how the two strings relate rather than simply if they're the same or not. A return value of 0 says that the strings are equal, less than zero says that the first string is shorter or lexicographically smaller, and greater than zero says that the first string is longer or lexicographically larger.

<scanf syntav>

int  scanf ( const char * format, ... );

& is needed in case of integer and float to pass the reference but in case of string its an array of characters which represents reference itself.

char a[5];
int b;

a represents the base address of the array.
&b represents the address of b;

void fun1(int *a)
{
    *a=*a+1;
}
void fun2(char *a)
{
    *a='b';
}
main()
{
  int a=5;
  char b='s';
  fun1(&a);
   fun2(b);
}

see the differences.

so when it comes to C there is no string comparison? and i don't get the latter part the int result part.

Why have you post this comment fullarmorzz?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.