venkateshwarr
asked on
void value..
Hi,
I want to do some thing like this...
void func();
void test()
{
if((int)func()+1) printf("done");
}
As you can see I want to execute the func() and after its done I want to print a string..
func() is in a library, I can't change its return type. func() is used in lot of places and I dont want to rename func().
And plz dont tell me to execute them as different statements...
I want to have them in a single line that ends with a semi-colon....
Thanks,
I want to do some thing like this...
void func();
void test()
{
if((int)func()+1) printf("done");
}
As you can see I want to execute the func() and after its done I want to print a string..
func() is in a library, I can't change its return type. func() is used in lot of places and I dont want to rename func().
And plz dont tell me to execute them as different statements...
I want to have them in a single line that ends with a semi-colon....
Thanks,
ASKER
I understand that...
but I want to execute func() and printf() in a single line.
Is there any other way?
but I want to execute func() and printf() in a single line.
Is there any other way?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think that works for me,
thanks a lot sunnycoder..
thanks a lot sunnycoder..
ASKER
I will keep this question open for a couple of days... just to see if there are any other alternatives.....
Thanks
Thanks
I've got to ask - why on earth do you 'need' to do this ?
Andrew,
Its probably for #defines! I use stuff like this:
#if NOSESSIONS
#define gettses(n,s,d) 0;{Clear(*(s));(s)->status =STOROPEN; (s)->flags =CDOK|BFRW ;};
#endif
you then get:
newSess = getsess(1,&structure,3);
either calling the real 'getsess' function or manufacturing a 'fake' return.
Venkateshwarr
If this is the case, you can use:
{func();printf("hello");}
instead.
Paul
Paul
Its probably for #defines! I use stuff like this:
#if NOSESSIONS
#define gettses(n,s,d) 0;{Clear(*(s));(s)->status
#endif
you then get:
newSess = getsess(1,&structure,3);
either calling the real 'getsess' function or manufacturing a 'fake' return.
Venkateshwarr
If this is the case, you can use:
{func();printf("hello");}
instead.
Paul
Paul
You could use most any operator. Coma is the safest, but you could use + or - or * or << or >> or == or > or < or !=.
The order of evaluation might be a problem though!
The order of evaluation might be a problem though!
>> if((int)func()+1) printf("done");
Since 'func' is void, casting it to an 'int' is undefined and may therefore produce a random value. You would be really unlucky if that random value was '-1'.
>>You could use most any operator.
Or, indeed, '|' which would be even better:
if((int)func()|1) printf("done");
Paul
Since 'func' is void, casting it to an 'int' is undefined and may therefore produce a random value. You would be really unlucky if that random value was '-1'.
>>You could use most any operator.
Or, indeed, '|' which would be even better:
if((int)func()|1) printf("done");
Paul
I can't see how that's even slightly useful. What's wrong with just..
func(); printf("done");
I really can't see why you need it as a single statement?
If you're planning to macro-out func() in, say, a release build, then you're release-build version will still return an int, not a void.
C'mon, questioner, please let us know what you're tying to do :-)
func(); printf("done");
I really can't see why you need it as a single statement?
If you're planning to macro-out func() in, say, a release build, then you're release-build version will still return an int, not a void.
C'mon, questioner, please let us know what you're tying to do :-)
>Or, indeed, '|' which would be even better:
>if((int)func()|1) printf("done");
You have to watch out-- many an optimizing compiler will realize the expression is always true, and not generate the function call or the if() statement.
For example microsoft C has for decades now taken the statement:
x |= 0xFF;
... and generates the code:
x = 0xFF;
... which is okay usually, unless x is a device register that you really did want to read and rewrite.
>if((int)func()|1) printf("done");
You have to watch out-- many an optimizing compiler will realize the expression is always true, and not generate the function call or the if() statement.
For example microsoft C has for decades now taken the statement:
x |= 0xFF;
... and generates the code:
x = 0xFF;
... which is okay usually, unless x is a device register that you really did want to read and rewrite.
Why not just use a semicolon?
func(); printf("done");
The questioner only asked for them on one line, not in one statement.
func(); printf("done");
The questioner only asked for them on one line, not in one statement.
> And plz dont tell me to execute them as different statements...
perhaps I was too hasty...
perhaps I was too hasty...
>>You have to watch out...
Good catch as ever greg.
Paul
Good catch as ever greg.
Paul
ASKER
PaulCaswell,
You are right!
I am working on some macros and want to #define some functions according to my convenience...
The problem comes with return statements....
Say I want to trace all the returns
I define
/*** below statement gives syntax error ****/
#define return printf("%s is done!! \n",__func__), return
void main()
{
int k=1;
if(k==1) func(1);
else func(2);
}
void func(int i)
{
printf("test %d\n",i);
return;
}
So you can the problem now, if I use a semi-colon in #define it effects the logic of the code.
>if((int)func()|1) printf("done");
did not work on microsoft VC++;
>>I've got to ask - why on earth do you 'need' to do this ?
andrewjb, I guess you understood now.. :)
So my question is still open how to #define return to avoid this problem....
I will give additional points.. for other alternatives...
Thanks.
You are right!
I am working on some macros and want to #define some functions according to my convenience...
The problem comes with return statements....
Say I want to trace all the returns
I define
/*** below statement gives syntax error ****/
#define return printf("%s is done!! \n",__func__), return
void main()
{
int k=1;
if(k==1) func(1);
else func(2);
}
void func(int i)
{
printf("test %d\n",i);
return;
}
So you can the problem now, if I use a semi-colon in #define it effects the logic of the code.
>if((int)func()|1) printf("done");
did not work on microsoft VC++;
>>I've got to ask - why on earth do you 'need' to do this ?
andrewjb, I guess you understood now.. :)
So my question is still open how to #define return to avoid this problem....
I will give additional points.. for other alternatives...
Thanks.
ASKER
I think it would be great to have a predefined macro to know the name of the caller function.. some thing similar to __func__.
Any thoughts/Ideas on this.?
Any thoughts/Ideas on this.?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
NovaDenizen,
I know I can do that.. there are couple of other ways too.
But I dont want to around changing all the return in the code....
I know I can do that.. there are couple of other ways too.
But I dont want to around changing all the return in the code....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> if((int)func()+1) printf("done");
Does not make sense and is not possible ... func is not returning anything ... what does the compiler add 1 to?
Reconsider what you wish to do and find some other way
Cheers!
sunnycoder