Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int main()
    char array[5];
    char * ans;                  // this alone doesnt work without an array
    ans = array;                    
    printf(" type yes or no:\n");
    scanf("%s", &ans);         // address of  ans me tw khud array ka address he aur array me array[0] ka
    ans = toupper(ans);
       if(ans == "YES")
       { printf(" yes works");}
       else if( ans == "NO")
       {printf(" NO works!");}
       {printf(" None");}

    return 0;

What I have tried:

Its a part of a bigger program only to check why its not working. It doesnt give any errors it just paused. can you identify the problem?
Updated 30-Sep-22 20:26pm
Member 15627495 1-Oct-22 2:02am    
char array[5];
    char * ans = &array;

There are a few problems. These three lines all have issues :
scanf("%s", &ans);
ans = toupper(ans);
if(ans == "YES")
For the first line, you are passing the address of a pointer and that is incorrect. You should pass just the pointer. For the second line, toupper operates on just one character so you can't pass the pointer. That means you have to loop through the string and process each character one at a time. In the third line, you can not compare strings like that in C. You have to use strcmp to do that. Here are those three lines corrected :
scanf( "%s", ans );
while( * ans )   // you must dereference the pointer to access the character
{                // loop while character is not a null.
    * ans = toupper( * ans );
if( strcmp( array, "YES" ) == 0 )   // compare the strings
Your character array is a bit small. What do you think would happen if a user entered eight characters? I would expect bad things so you might want to make it bigger.

Anyway, that should fix those three lines of code.

ETA: merano99 made a good point. Calling fgets would be a better option - : fgets[^]. Another option is to use a width specifier in your format string. This is described in the docs - : scanf[^]. For your program, you have a character with 5 characters so you can accept input of 4. A better way to do this is :
#define ARRAY_SIZE 4
    char array[ ARRAY_SIZE + 1 ];  // +1 for the null terminator
    char format[ 16 ];
    sprintf( format, "%%%ds", ARRAY_SIZE );
    scanf( format, ans );
Now you can change the amount of input you accept in just one place. The call to sprintf will write a format string that will be "%4s" for ARRAY_SIZE of 4. Here is the documentation on sprintf - : sprintf[^]. The triple percents in the format string is a bit unusual. What happens is the first two result in a singe percent and then the third one along with the 'd' handle the integer value and the 's' is a literal value so the result is "%4s". In the call to scanf that width specifier will limit input to 4 characters.
Share this answer
merano99 1-Oct-22 13:03pm    
"you might want to make it bigger." That wouldn't be a good solution either. Much better would be to limit the input to the length, e.g. with fgets(). But otherwise good explanation 5+ for it.
Rick York 1-Oct-22 16:11pm    
That would be my preference. I never, ever use scanf but then I never acquire input from a console. I rarely call sscanf either. I prefer to tokenize the input and then deal with each token on its own.
You can't compare a string literal with a variable string, a char array - they will not be the same because the comparison compares pointers not content - and even is the user types "YES" the pointers are not the same because the version teh user typed can be changed:
ans[1] = '?';
The literal version you compare it to is a const char * - it cannot be changed.

To compare two strings, you use the C strcmp() - C Standard Library[^] function:
if (strcmp(ans, "YES"))
Do note though, that "YES" will only match "YES" - it doesn't match "yes", "Yes", "yEs", "YES." or any other string the user might enter.
Share this answer
Shao Voon Wong 1-Oct-22 2:26am    
strcmp() returns zero if the strings are the same.

if (strcmp(ans, "YES") == 0) {...}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900