开发者

Can you define a function returning bool in C?

开发者 https://www.devze.com 2023-03-27 16:10 出处:网络
I have very little experience in C or C++, so I don\'t understand some of the various shortcuts and common tasks in the language.

I have very little experience in C or C++, so I don't understand some of the various shortcuts and common tasks in the language.

I'm looking at game code that looks similar to this:

bool
update_frame (void)
{
  // Various bits of code
  return TRUE;
}

This is the main loop of the game. I'm thinking that it's similar to something like:

while (true) {
  // do stuff
开发者_如何学JAVA}

Which I would use in C# or Java. Is this what's going on here?


This code is likely using a game engine. The engine will call update_frame 30/60/X times per second. In between, however, it might perform other tasks.

It probably functions as a while(true) loop, though.


No tricks in the function you mentioned, its just a simple function.... the loop is probably in some other function that is calling this function.

tip: try locating that calling function for better understanding.


bool is just one data type just like int, float....etc

In c you can not give name of function same as any data types or keywords.

In your code i think bool is not part of your function name...it is just return type of that function


Whatever is it what you're asking, I can say that your pieces of code mean absolutely the same in C, Java and C#: the first one is a function returning bool, the second one is an infinite loop.

What is the question, again?


It is just a simple function which returns a bool value. But, as you say it was for a game, I think somewhere in the code(probably main function) it sets this function as an update function, with a command like this:

setUpdateFrameFunc(update_frame);

And after this line, it will make a thread and run this function on that thread over and over. Finally, It seems that the code on the function is running in a while(true) loop.


I think (at least the title implies) that you wonder how it is possible to specify true and bool in C, yes?

ANSI C (C89/C90) does not provide a special data type for booleans; boolean functionality is expressed using integers, where 0 stands for "false" and everything else is interpreted as "true".

C99 provides "built-in support", though, by defining appropriate macros as you can see in this answer.

So, either the code you are looking at is using C99 (check whether the header is included somewhere) or bool is a typedef macro, so look out whether there's a

typedef int bool;

or something similar to be found in the code. The mere fact that it says "return TRUE" would imply the latter, since TRUE is very common in ANSI C and most often defined as

#define TRUE 1

So again you may look out for that.

As to your other question what that function might do, it could be implemented similar to a "game loop"/while(true) loop, like this:

while (update_frame()) { /* render current state to screen */
  /* do game logic */
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消