0
#include <stdio.h>
#include <stdlib.h>

#define timeteller(choose) (choose == 'Y' || choose == 'y') ? __TIME__ :"ok we dont show time now .."

int main (){
    int choose1;
    printf("do u want to learn time ?...\n");
    scanf(" %c",&choose1);

    if (timeteller(choose1) ){
        printf("%s",timeteller(choose1));
    }
    else {
        printf("%s",timeteller(choose1));
    }
    
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Blghn
  • 5
  • 2
  • 1
    What's the point of the `if`? It prints the same thing in both cases. – Barmar May 09 '22 at 21:56
  • 1
    And `timeteller` always returns a true value, since all strings are truthy. – Barmar May 09 '22 at 21:56
  • `choose1` needs to be `char`, not `int`. – Barmar May 09 '22 at 21:58
  • 2
    When I fix that type error, the code compiles with no warnings and it prints the time. – Barmar May 09 '22 at 22:03
  • That macro doesn't fill any other purpose but to confuse _you_, the programmer. It made the program far worse. Get rid of it. Writing function-like macros just for the heck of it is one of the worst things you can do in C. – Lundin May 10 '22 at 06:21

1 Answers1

2

Here's a version that works and is a bit cleaner:

  • Macro name in full caps; common C code convention.
  • Let macro only check the user input.
  • Put macro parameter between ( ): (choose). Not needed here but it's a common convention to avoid issues with more complex expressions as argument.
  • Nice code formatting.
  • Printing newline to get nice program output.

BTW, it would probably have been better to use a function, instead of a macro. But that's a whole other subject.

The reason why it didn't work with int choose1; is that after you define it, choose1 contains 'random' bytes from the stack. Then your scanf() writes only one of these bytes to the entered character value, but the remaining bytes still contain stack garbage.

Just add printf("%d\n", choose1); before and after your scanf() and you'll see.

Because of this, in official terms your program results in 'undefined behaviour'. Your program could have worked if choose1 coincidently got the value 0 from the stack or if your compiler/platform was friendly and initialized it to 0.

    #include <stdio.h>
    #include <stdlib.h>

    #define IS_Y(choose) ((choose) == 'Y' || (choose) == 'y')

    int main()
    {
        char choose;

        printf("do u want to learn time ?...\n");
        scanf(" %c", &choose);

        if (IS_Y(choose))
        {
            printf("%s", __TIME__);
        }
        else
        {
            printf("Ok, we don't show time now ...");
        }

        printf("\n");
        
        return 0;
    }
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 1
    You lost the space in `scanf(" %c", &choose)`. Granted, in this context it doesn't matter much, but if there was prior input, it matters a lot. See [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/q/5240789/15168) for details. – Jonathan Leffler May 10 '22 at 00:32