• C

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,
LVL 12
venkateshwarrAsked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
Hi venkateshwarr,

Use a comma operator

func(),printf("hello");

Cheers!
sunnycoder
0
 
sunnycoderCommented:
Hi venkateshwarr,

>     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
0
 
venkateshwarrAuthor Commented:
I understand that...
but I want to execute func() and printf() in a single line.

Is there any other way?
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

 
venkateshwarrAuthor Commented:
I think that works for me,
thanks a lot sunnycoder..
0
 
venkateshwarrAuthor Commented:
I will keep this question open for a couple of days... just to see if there are any other alternatives.....

Thanks
0
 
andrewjbCommented:
I've got to ask - why on earth do you 'need' to do this ?
0
 
PaulCaswellCommented:
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
0
 
grg99Commented:
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!

0
 
PaulCaswellCommented:
>> 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
0
 
andrewjbCommented:
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 :-)
0
 
grg99Commented:
>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.

0
 
NovaDenizenCommented:
Why not just use a semicolon?

func(); printf("done");

The questioner only asked for them on one line, not in one statement.
0
 
NovaDenizenCommented:
> And plz dont tell me to execute them as different statements...
perhaps I was too hasty...
0
 
PaulCaswellCommented:
>>You have to watch out...
Good catch as ever greg.

Paul
0
 
venkateshwarrAuthor Commented:
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.

0
 
venkateshwarrAuthor Commented:
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.?
0
 
NovaDenizenConnect With a Mentor Commented:
#define return printf("%s is done!! \n",__func__), return

You could define it like this:
#define return {printf("%s is done!!\n",__func__); return; }
That wouldn't let you return function values, though.

#if DEBUG_RETURN
#define RETURN_ARG(x) {printf("%s is done!!\n",__func__); return x; }
#define RETURN_NOARG {printf("%s is done!!\n",__func__); return; }
#else
#define RETURN_ARG(x) return x;
#define RETURN_NOARG return;
#endif

0
 
venkateshwarrAuthor Commented:
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....

0
 
PaulCaswellConnect With a Mentor Commented:
In the specific case of 'return' you may actually have to use a process like NovaDenizen. Th eproblem alises because there are actually two diffrent forms of 'return':

if ( ... )
{
  return;
}

and:

...
 return(1);
}

you will not be able to consistently change this using #defines. Since you therefore must perform a search/replace throughout, why not do it in a reliable and reversable fashion such as:

search for 'return' replace with 'x("return");return'

then use:

#ifdef _DEBUG
#define x(msg) printf("%s\t%d\t%s\n",__FILE__,__LINE__,msg)
#else
#define x
#endif

You have the benefits of at least:

1. You can always do another search/replace to remove them.
2. You can remove those you are not interested in.
3. You can change the #define to direct the log to a file.
4. The code has zero footprint on the release version.
5. Nothing is hidden.

Paul
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.