[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

void value..

Posted on 2005-04-07
19
Medium Priority
?
448 Views
Last Modified: 2010-04-15
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,
0
Comment
Question by:venkateshwarr
  • 6
  • 4
  • 3
  • +3
19 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13733441
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
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13733463
I understand that...
but I want to execute func() and printf() in a single line.

Is there any other way?
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 600 total points
ID: 13733489
Hi venkateshwarr,

Use a comma operator

func(),printf("hello");

Cheers!
sunnycoder
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:venkateshwarr
ID: 13733509
I think that works for me,
thanks a lot sunnycoder..
0
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13733518
I will keep this question open for a couple of days... just to see if there are any other alternatives.....

Thanks
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 13734389
I've got to ask - why on earth do you 'need' to do this ?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13734613
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
 
LVL 22

Expert Comment

by:grg99
ID: 13735161
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13735212
>> 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
 
LVL 12

Expert Comment

by:andrewjb
ID: 13735253
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
 
LVL 22

Expert Comment

by:grg99
ID: 13737158
>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
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13737190
Why not just use a semicolon?

func(); printf("done");

The questioner only asked for them on one line, not in one statement.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13737205
> And plz dont tell me to execute them as different statements...
perhaps I was too hasty...
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13737922
>>You have to watch out...
Good catch as ever greg.

Paul
0
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13741896
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
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13741908
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
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 200 total points
ID: 13742246
#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
 
LVL 12

Author Comment

by:venkateshwarr
ID: 13742267
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
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 200 total points
ID: 13751087
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question