what happens in following code exactly? [modified]
-
what happens with if condition in following code?
int function(int i)
{
printf("inside function");
return 0;
}int main()
{
if(function)
printf("hi");
else
printf("bye");
}and output is only hi why compiler doesn't complaints about wrong function call? :confused:
modified on Thursday, July 23, 2009 12:08 PM
-
what happens with if condition in following code?
int function(int i)
{
printf("inside function");
return 0;
}int main()
{
if(function)
printf("hi");
else
printf("bye");
}and output is only hi why compiler doesn't complaints about wrong function call? :confused:
modified on Thursday, July 23, 2009 12:08 PM
What is
fun
in your code? Is that suposed ot be:if (function) ...
? If so, then the answer is simple, in your
if
the address of the function will be checked against zero, and since it is not zero, it will come out as TRUE thus, you get "hi".> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
What is
fun
in your code? Is that suposed ot be:if (function) ...
? If so, then the answer is simple, in your
if
the address of the function will be checked against zero, and since it is not zero, it will come out as TRUE thus, you get "hi".> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
yes.. it's
if(function)
and thanx ..I got it.
-
what happens with if condition in following code?
int function(int i)
{
printf("inside function");
return 0;
}int main()
{
if(function)
printf("hi");
else
printf("bye");
}and output is only hi why compiler doesn't complaints about wrong function call? :confused:
modified on Thursday, July 23, 2009 12:08 PM
Your function doesn't get called. I think you mean:
if (function (1234))
...But instead you're checking if the pointer to function is not 0, which it isn't, so it counts as true, so you get "hi". Iain.
I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[^]
-
what happens with if condition in following code?
int function(int i)
{
printf("inside function");
return 0;
}int main()
{
if(function)
printf("hi");
else
printf("bye");
}and output is only hi why compiler doesn't complaints about wrong function call? :confused:
modified on Thursday, July 23, 2009 12:08 PM
In more detail, a procedural function name in C/C++ is a pointer to a function (a memory address). This is how you can pass one function to another function (like with the CRT qsort() function.) So in your if statement, you are checking whether function exists, which it does. To call a function, you have to use the parentheses syntax, i.e.
function(1)
.