mercredi 2 décembre 2020

defining variables inside if-statement error C programming

  • the code idea is to get a char (letter) from the user y / n and check if the letter that the user enter is "Y" then it prints "YES!" but if the user enter the letter "N" so I'm trying to use #define and use it inside if statement , in that code (you can get the idea from code if it was hard understanding by my explanation) :
#include <stdio.h>
#include <stdbool.h>

//defines :
#define YES y 
#define NO n

int main()
{
    char letter = ' ';
    printf("for Yes enter : y\nfor No enter : n\n");
    letter = getchar();
    if(YES == letter)
    {
        printf("YES!");
    }
    else if(NO == letter)
    {
        printf("NO!");
    }
    else
    {
        printf("this option is not available");
    }
    printf("FUZZY");
    
    getchar();
    return 0;
}

Error :

Ex1.c: In function 'main':
Ex1.c:5:13: error: 'y' undeclared (first use in this function)
 #define YES y
             ^
Ex1.c:13:5: note: in expansion of macro 'YES'
  if(YES == letter)
     ^~~
Ex1.c:5:13: note: each undeclared identifier is reported only once for each function it appears in
 #define YES y
             ^
Ex1.c:13:5: note: in expansion of macro 'YES'
  if(YES == letter)
     ^~~
Ex1.c:6:12: error: 'n' undeclared (first use in this function)
 #define NO n
            ^
Ex1.c:17:10: note: in expansion of macro 'NO'
  else if(NO == letter)

what to do and make this possible (using defines inside C code) ??

Aucun commentaire:

Enregistrer un commentaire