Question:
What's wrong with this C code?
Nosrettap
2011-09-02 22:21:36 UTC
What's wrong with this C code? I want to use booleans of some sort rather than just 1 and 0.


#include

main()
{
bool a = function();
}

bool function()
{
return false;
}
Three answers:
Cubbi
2011-09-04 08:25:10 UTC
It's missing the return type of main and function() is used before it was declared.



Corrected version:



#include

bool function()

{

     return false;

}

int main()

{

     bool a = function();

}



test: https://ideone.com/AmEOP



However, note that this won't work with Visual Studio which ships with an obsolete C compiler. If you're using that, either install a standard C compiler (such as Intel C or gcc), or use one of the many workarounds available (google visual studio stdbool)
modulo_function
2011-09-02 22:45:25 UTC
Your program is basically ok but you need to either move the defintion of function() above main() or use a signature.



#include

#include

bool function();



main()

{

bool a = function();

printf("a: %d",a);

}



bool function()

{

return false;

}



I didn't see a format spec that prints true or false, and so you get 0 for false.
stein
2016-10-03 01:47:45 UTC
void WriteDay(char d[]); That line broadcasts a function prototype. It shall we the compiler be attentive to that there is a function observed as WriteDay and what arguments might desire to be handed to it. It does not tell the compiler how the WriteDay function is carried out. you will possibly be able to desire to have the actually function someplace so as that the compiler knows what that's meant to do.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...