0

Simply put, I have the following code (simplified):

void Example()
{
    struct AuctionVars auction;
    memset(&auction, 0, sizeof(struct AuctionVars));
}

And I have the following struct:

struct AuctionVars
{
    float minBidIncrement;
    int numAuctionItems;
    float* auctionItemValues;
};

When I change it to this:

struct AuctionVars
{
    float minBidIncrement;
    int numAuctionItems;
    float* auctionItemValues;
    bool isAuctionClosed;
};

I get a whole lot of errors everywhere my program references or uses an AuctionVars variable.

I have already found out that the Visual Studio compiler uses C89 and does not support declaring variables anywhere but at the beginning of code blocks, so is this another limitation of C99 where bool types are not supported in structs? Or am I making another one of my foolish mistakes?

Nkosi Dean
  • 227
  • 8
  • 18
  • 1
    *"Visual Studio compiler uses C99"* - You mean C89 – Ed S. Nov 23 '13 at 07:08
  • 1
    "get a whole lot of exceptions" - You mean "compiler errors" :) – Alex I Nov 23 '13 at 07:12
  • 1
    The word "exceptions" usually refers to run-time errors, and they're a feature of C++ not of C. It sounds likely you're getting compile-time errors -- and you should include the text of the errors in your question. – Keith Thompson Nov 23 '13 at 07:12
  • I'm sorry, I meant compiler errors, and I also meant C89. Thank you for the corrections. – Nkosi Dean Nov 23 '13 at 07:14
  • @NkosiDean: You can edit your question. – Keith Thompson Nov 23 '13 at 07:16
  • 1
    `bool` is not a standard type in C89/C90. `_Bool` is a standard type in C99 and C11; you can expose `bool` by including ``. In C89/C90, if you've not provided a definition for type `bool`, you will get compilation errors. Don't forget the MSVC is stuck in a 20-year old time-warp; it only supports the C89/C90 language, not the ancient C99 or modern C11 versions. – Jonathan Leffler Nov 23 '13 at 07:28

2 Answers2

0

I have already found out that the Visual Studio compiler uses C99 and does not support declaring variables anywhere but at the beginning of code blocks, so is this another limitation of C99 where bool types are not supported in structs? Or am I making another one of my foolish mistakes?

Don't know about visual studio but C99 supports Boolean type.

FAQ list · Question 9.1

Traditionally, C did not provide a standard Boolean type, partly and partly to allow the programmer to make the appropriate space/time tradeoff.

C99 provides _Bool type and Boolean variable can be declared as

_Bool var;  

_Bool is an int type.

In addition to defining the _Bool type, C99 also provides a header, <stdbool>, that makes it easier to work with Boolean type. A macro bool is provided in this header. You can use this header and macro bool to define a Boolean type as

#include <stdbool.h>
bool var;  
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Sorry, I made a mistake in my original post which I'll fix now. Visual Studio uses C89, not C99. There is no _Bool type and there is no stdbool.h header file provided either. – Nkosi Dean Nov 23 '13 at 08:31
-1

Ugh, nevermind, I just found out C doesn't support the bool keyword. That was kind of a slap in the face for me.

Nkosi Dean
  • 227
  • 8
  • 18