Click here to Skip to main content
15,885,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, My Wi-Fi's been down and I haven't been on the computer lately. It's good to be back. So, you guys said a 'bool' function does not exist in C. However, upon a google search, I found it does. What exactly do you guys mean? Here's the code that you told me couldn't have a boolean function.

C
#include <stdio.h>
#include <stdbool.h> //I learned that you need to add stdbool.h to make the bool function work - Chris
 
int main(void) {
 printf("You will have life functions. You will get 3 lives.\n");
char life [3];
int aa;
printf("You have 3 lives");
printf("Press 'S' to start");
scanf("%i",&aa);
 
bool PlayerDied( PlayerData * pd )
{
   bool alive = false;
   --pd->Lives;       // player died - one less life
  return pd->Lives <= 0;
       alive = true;  // player still has a life left
   return alive;
}
 return 0;
}


What I have tried:

I read that boolean in C is used to store true and false variables.
Posted
Updated 26-Mar-21 23:35pm
v2

Quote:
Why can't C have a boolean function?

Traditionally in C, booleans are handled as ints.
True is any non 0 integer.
False is 0.
 
Share this answer
 
Comments
ModerateHacker; 26-Mar-21 10:57am    
Danke (German for thanks)
C didn't originally have bool, and maybe it still doesn't. It would simply be defined as some kind of int, possibly unsigned, with #define false 0 and #define true 1. Just like NULL isn't a keyword either, but rather some #define thing. This is now so standard that it looks like bool is part of the language.
 
Share this answer
 
v2
Comments
ModerateHacker; 26-Mar-21 10:57am    
Makes sense. Thanks
C still doesn't have bool/code> as a datatype - <code>_Bool was added at C99, but if you look at the stdbool.h header file, you will find that bool is a #define, as are true and false:
C++
/*===---- stdbool.h - Standard header for booleans -------------------------===
 *
 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 * See https://llvm.org/LICENSE.txt for license information.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 *
 *===-----------------------------------------------------------------------===
 */

#ifndef __STDBOOL_H
#define __STDBOOL_H

/* Don't define bool, true, and false in C++, except as a GNU extension. */
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool as a GNU extension. */
#define _Bool bool
#if __cplusplus < 201103L
/* For C++98, define bool, false, true as a GNU extension. */
#define bool  bool
#define false false
#define true  true
#endif
#endif

#define __bool_true_false_are_defined 1

#endif /* __STDBOOL_H */

There is no "real bool" in C, and any nonzero value is always true as far as any condition text is concerned.
 
Share this answer
 
Comments
ModerateHacker; 27-Mar-21 23:43pm    
Thanks Griff
I see that old function is still alive and still in the wrong place as it was the last time you posted a question.

You still have not learned C does not support nested functions. This was pointed out in your last question. That function was pasted inside your main function and that is not allowed in C. You have to move it outside of the main function.

You also have to define a PlayerData structure for the function to be usable. The function accesses a Lives member of the structure so it should have that item in it along with any other data you want retain about the player. That is something YOU need to do.

Here's something might help : Data structures - C++ Tutorials[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900