0
  • happy is a function called in abc.
  • How can I silence the happy function?
/*------Prints debug statements------*/
void d_printf(char *str)
{
  debug_printf("\n -------------------------------------------------");
  debug_printf(" \t \n \n %s \t ",str);
  debug_printf("\n -------------------------------------------------");
  return;
}

int abc(arg1, arg2)
{
 d_printf("bleh bleh");
  happy (a, b); 
printf("Hello world");
}

int happy(arg1, arg2)
{
 //do something
 //assuming this contains no print statments.
}

I want the happy function to execute quietly and the rest of the things should be printed ? (The happy function should execute quietly in the background, i.e, whatever happens in that function should not be seen on terminal.)

code_debug
  • 25
  • 4
  • Does this answer your question? [How to disable printf function?](https://stackoverflow.com/questions/13816994/how-to-disable-printf-function) – moooeeeep Mar 03 '21 at 12:11
  • What you mean `in the background`? Do you mean to execute in separate thread/process or when it's called from the same flow not to print – jordanvrtanoski Mar 03 '21 at 12:14
  • You'd need to call some system-specific console API to temporarily shut it off. Better yet, design your functions so that they keep UI and algorithms separated, then you'll never have a need for strange things like this. – Lundin Mar 03 '21 at 12:15
  • @moooeeeep Those answers seem to be Unix-only. `fclose(stdout)` doesn't work portably and is therefore not good advise. – Lundin Mar 03 '21 at 13:32
  • @Lundin , I tried something from here https://stackoverflow.com/questions/16498364/how-to-prevent-function-from-printing , however fclode(stdout), or using the silence feature doesn't seem to work? Any better example by using macros in UNIX/LINUX? – code_debug Mar 03 '21 at 14:51
  • @code_debug Try the `dup2` example from here: https://stackoverflow.com/questions/54094127/redirecting-stdout-in-win32-does-not-redirect-stdout/54096218 – Lundin Mar 03 '21 at 15:56
  • @Lundin , the dup2 example doesn't work as well. Is there any chat forum where I could chat about the same if possible please? – code_debug Mar 04 '21 at 05:53
  • You could try the C or C++ chat rooms https://chat.stackoverflow.com/. I only know how to do this on Windows myself (but it seems it should be easier on Linux actually). Console stuff in general kind of turned a thing of the past some 20 years back. – Lundin Mar 04 '21 at 07:23

1 Answers1

1

As long as your happy function is not calling any functions that write to standard output, then it shouldn't be writing anything to your terminal.

I'm assuming you still want your other functions to write to the terminal.

You also seem to be calling the happy function with the a and b variables as arguments, but it doesn't seem like you've instantiated those anywhere in your code. You're also missing the type specifiers for the arg1 and arg2 parameters in your happy and abc functions.

You could also redirect standard output as @Lundin suggested in his comment. I believe that there are OS specific calls for doing so in C, like freopen_s, and _wfreopen_s to reassign the stdout file pointer in Windows and dup2 to reassign the stdout file descriptor in Linux.

Alex Riveron
  • 389
  • 1
  • 10
  • 1
    Here's a nice answer addressing multiple platforms: [Redirecting stdout in win32 does not redirect stdout](https://stackoverflow.com/questions/54094127/redirecting-stdout-in-win32-does-not-redirect-stdout/54096218). – Lundin Mar 03 '21 at 12:54