Hello people,
I am using Turbo-C

Can anyone give me a sample program using the bool array

I tried compiling the following code snippet. But it doesnt work.

#include<stdio.h>
#include<conio.h>

int main()
{
 bool a[10]={true};
 clrscr();

 printf("\nHello World");
 
 getch();
}

It gives the error Undefined Symbol bool.

But something peculiar i noticed is that,
when i declare an int (or any other data type for that matter ), its highlighted in white, but bool is not highlighted.
Is it that my Turbo-C is not able to recognize bool ?

Please reply

Dani AI

Generated

Turbo C is too old to know the C99 boolean type. The C language got a built-in boolean (_Bool) and the convenience alias bool (plus true/false) in C99 via the header <stdbool.h> — that is why your compiler reports "Undefined Symbol bool". As explained, the proper modern fix is to use a C99-compliant compiler and #include <stdbool.h> (see stdbool.h reference). 's hint about platform headers is also useful: some legacy libraries expose an all-caps BOOL or similar, but that is not the standard C bool.

If you must keep Turbo C, add a small, guarded fallback so the same source compiles on old and new compilers. A compact option (1 byte per boolean) is:

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
typedef unsigned char bool;
#define true 1
#define false 0
#endif

This avoids redefining bool when a C99 compiler is used. Note: enums (as suggested earlier) work, but many compilers store enum values in int-sized slots, which makes large bool arrays much bigger; choose unsigned char or a bitset if memory is critical.

Other quick points tied to the thread: prefer int main(void) { ... return 0; } (as confirmed). Avoid nonstandard clrscr()/getch() for portable code — use getchar() or run from a terminal. If possible, move off Turbo C to a modern toolchain (GCC/Clang or current IDEs) so you get standard headers and fewer surprises (Turbo C is obsolete — background here).

Recommended Answers

All 7 Replies

Check types.h. Isn't the type BOOL, not bool?

Turbo C doesn't support bool as a data type unless you define it yourself. If you get a C99 compiler you can do what you want:

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool a[10] = {true};

    printf("Hello, world!\n");

    // In C99 omitting the return statement is no longer hideously broken
}

On a side note, you have three bad habits that need to be eliminated straight away:

>clrscr();
Not only is clrscr highly non-portable, it's also either completely unnecessary or antisocial. If running the code from an IDE where the window is created and destroyed along with the program then there's no point in clearing the screen because it starts out cleared. If running the code from a shared console (such as Windows' command prompt) clearing the screen removes the output of all previously run programs. I don't know about you, but I'm none too happy when some programmer with a God complex deletes things that don't belong to him.

>getch();
If the sole purpose of this is to pause the program and keep the window open, you can do that without relying on a non-standard function. getchar() works just as well for the purpose of pausing. Frivolous destruction of code portability is stupid, and you should seriously consider portable options first.

>}
Do you see a return statement? I don't. That means you've invoked undefined behavior and your program isn't bound by any constraints. I can do anything from printing "Hello World" backwards to ordering anchovies on pizza. You need to recognize that undefined behavior is about the worst thing you could have in a program, and it's very easy to invoke in C if you don't know what you're doing.

Check types.h. Isn't the type BOOL, not bool?

No Sir, it still doesnt work :(

If all you care about is making it work on your old as dirt compiler, this should be sufficient:

#include <stdio.h>

typedef enum { true, false } bool;
 
int main(void)
{
    bool a[10] = {true};
 
    printf("Hello, world!\n");
 
    return 0;
}

What are the errors?

Turbo C doesn't support bool as a data type unless you define it yourself. If you get a C99 compiler you can do what you want:

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool a[10] = {true};

    printf("Hello, world!\n");

    // In C99 omitting the return statement is no longer hideously broken
}

On a side note, you have three bad habits that need to be eliminated straight away:

>clrscr();
Not only is clrscr highly non-portable, it's also either completely unnecessary or antisocial. If running the code from an IDE where the window is created and destroyed along with the program then there's no point in clearing the screen because it starts out cleared. If running the code from a shared console (such as Windows' command prompt) clearing the screen removes the output of all previously run programs. I don't know about you, but I'm none too happy when some programmer with a God complex deletes things that don't belong to him.

>getch();
If the sole purpose of this is to pause the program and keep the window open, you can do that without relying on a non-standard function. getchar() works just as well for the purpose of pausing. Frivolous destruction of code portability is stupid, and you should seriously consider portable options first.

>}
Do you see a return statement? I don't. That means you've invoked undefined behavior and your program isn't bound by any constraints. I can do anything from printing "Hello World" backwards to ordering anchovies on pizza. You need to recognize that undefined behavior is about the worst thing you could have in a program, and it's very easy to invoke in C if you don't know what you're doing.

Thank you so much for pointing out mistakes in my programming style Ma'am.
So can i say that this is the best prototype for a main function ?

int main(void)
{
 //initializations

 //code

 return 0;
}

Before answering your question, I'll say that I'm getting tired of adding code tags to your posts. Put them in yourself or you'll attract the ire of the moderators, who will eventually start applying infractions to your account.

So can i say that this is the best prototype for a main function ?

That's a good template, yes.

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.