Thread: FALSE & TRUE undeclared???

  1. #1
    Registered User Makara's Avatar
    Join Date
    Dec 2011
    Posts
    24

    FALSE & TRUE undeclared???

    Why it says TRUE and FALSE are undeclared?? How to declare?? What I'm doing wrong?
    Code:
    #define s 256
    main()
    {
        char answer, t[s];
        char stop;
    
        stop = FALSE;
        while (stop = FALSE)
        {
            printf("Enter: ");
            gets(t);
            while (answer != 'Y'||answer != 'N')
            {
                printf("Continue?");
                answer = getchar();
            }
            if (answer = 'N')
            {
                stop = TRUE;
            }
        }
        system("PAUSE");
    18 `FALSE' undeclared (first use in this function)
    33 `TRUE' undeclared (first use in this function)
    Last edited by Makara; 02-21-2012 at 10:42 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    TRUE and FALSE are not keywords in C. It is common for people to declare them to make code more readable however. Remember, in C, zero is false and anything else is true, so you can just #define FALSE as 0 and TRUE as 1.

    EDIT: If you have a C99 compliant compiler, and you #include <stdbool.h>, you will actually have a bool type with true and false (lowercase).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Other nits:
    Code:
    while (stop = FALSE)
    ...
    if (answer = 'N')
    Be aware there is a difference between = (assignment) and == (equality test).





    Code:
    char answer, t[s];
    ...
    answer = getchar();
    Note that getchar returns an int but in this case it does not look too critical that it be so. In other cases it does become important to use the proper type and as such you should get into the habit.





    Code:
    gets(t);
    Avoid gets as it is susceptible to buffer overruns, prefer to use the fgets function instead which has protections against this.





    Code:
    main()
    You should explicitly state main as returning an int and do so at the end of the function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    By the way, this:

    Code:
    while (stop = FALSE)
    and this:

    Code:
    if (answer = 'N')
    are *assignments*, not *comparisons". You use == to compare two (non-array) variables.

    And get rid of gets(), it should never be used. Check the FAQ.

    Edit: ninja'd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. true or false
    By johngav in forum C Programming
    Replies: 4
    Last Post: 03-19-2009, 02:25 PM
  2. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 10:10 AM
  3. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  4. Help with True, False, NOT, AND, OR...
    By cgsarebeast in forum C++ Programming
    Replies: 22
    Last Post: 05-09-2006, 07:59 PM
  5. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM