2

I am trying to read a key hit and then stop a code. In C.

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

bool starting()
{
  char c;
    if (kbhit())
    {
        c=getch();
        if (c=="S"||c=="s")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
      return false;
    }
}

int main()
{
  while(!starting)
  {
    printf("line 1");
    delay(100);
  }
  return 0;
}

Without stdbool.h, it says errors like

syntax error: identifier 'starting', 
syntax error: ";"
syntax error: ")"
'starting': undeclared identifier

With stdbool.h, it says file not found. My compiler is the one that comes with Visual Studio 2010.

Any suggestion how to remove this? How can I still use a function that returns a boolean value?

ADDED sorry! for the short comment added. resolved mostly. Thanks all

Added More Errors: After Compiling: it reads:

filename.obj unresolved external symbol _delay referenced in function _main.

What should I do?

user2178841
  • 849
  • 2
  • 13
  • 26
  • 1
    `typedef unsigned char bool; #define false 0 #define true 1` –  Jun 22 '13 at 09:57
  • 5
    Or just write it in C++. The C compiler in VS is crap. Even crappier than the C++ one (which also is). –  Jun 22 '13 at 09:57
  • 1
    If you're trying to call the function `starting` inside that while, you're missing a `()`. – Mat Jun 22 '13 at 09:59
  • @Mat, care to explain? – David Ranieri Jun 22 '13 at 10:04
  • 1
    @DavidRF: `while(!starting)` tests the function pointer, not the function's return. – Mat Jun 22 '13 at 10:04
  • This is in no way a compiler issue, so why is it "crap" here? It's a stdlib issue if you want, but the compiler is working perfectly fine here. – Mario Jun 22 '13 at 10:06
  • @Mat, are you sure? if `starting` returns false there is no infinite loop – David Ranieri Jun 22 '13 at 10:06
  • 1
    @DavidRF: `starting` is a function identifier. `starting()` is a function call. – Mat Jun 22 '13 at 10:08
  • possible duplicate of [Using boolean values in C](http://stackoverflow.com/questions/1921539/using-boolean-values-in-c) – awesoon Jun 22 '13 at 10:08
  • See http://stackoverflow.com/questions/1656874/where-is-stdbool-h and http://stackoverflow.com/questions/8548521/trying-to-use-include-stdbool-h-in-vs-2010. – Mihai8 Jun 22 '13 at 10:20

2 Answers2

6

stdbool.h is introduced in C99, and Visual Studio doesn't support C99. You can define the types yourself. One possible way is:

typedef int bool;
#define true 1
#define false 0
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
-1

Three problems at once:

  • C doesn't know bool as a type of its own, but you can define it (e.g. through stdbool.h or just by using a typedef to any other integral type (usually unsigned char or int; this might be a question of memory usage vs. performance based).
  • MSVC is known for not having all the std**.h headers, especially older versions. So you most likely just don't have stdbool.h in VS 2010 (the reason for the file not found error).
  • You're missing brackets in this expression: while(!starting).
Mario
  • 35,726
  • 5
  • 62
  • 78