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

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;
}

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.