Thread: Function calling curiosity

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Function calling curiosity

    Hello, im new to these forums and i have not exactly a problem, more of a curiosity...

    Basically, i designed a function in c that searches a double linked list for a certain element (given as argument to the function), and the very same function outputs text suitable to the situation (element doesnt exist, element is at x position, etc), anyway, later on i wanted to use this function in another function just to see if the element was present,
    which works just fine, but i wanted to know if theres a way to somehow "suppress" the text output of the search function when i use it in this other function...

    I know i could just adjust the function to not output text and do that in the main program, but the situation made me curious about the possibility of "suppressing" text outputs...

    Anyway, thanks in advance and sorry if it was a long read...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just don't print any output. When you need to print output, call the function, and depending on the result, output what is necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Like i said, i know i could do that, i was just curious if there is a c equivalent to batch file`s ECHO which disables the displaying of messages.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lucas.lotr View Post
    I know i could just adjust the function to not output text and do that in the main program, but the situation made me curious about the possibility of "suppressing" text outputs...
    You could do something like this:

    Code:
    void myfunc ([your existing args), int print) {
           [...blah blah...]
           if (print) ....
    Depending on context, that may or may not be a better solution than optionally doing the output separately afterward. Eg, doing the output separately afterward might offer you greater flexibility, but at the expense of more coding.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to use MK27's suggestion, then you might as well pass a FILE* instead of an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lucas.lotr View Post
    Like i said, i know i could do that, i was just curious if there is a c equivalent to batch file`s ECHO which disables the displaying of messages.
    You can temporarily dup() stdout or stderr, close them, then restore them again later:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, const char *argv[]) {
    	int stdout_copy = dup(1);
    
    	printf("hello world\n");
    	close(1);
    	printf("[suppressed]\n");
    	dup2(stdout_copy, 1);
    	close(stdout_copy);
    	printf("hello again\n");
    
    	return 0;
    }
    I don't know how portable that is because of unistd.h, but I'm sure all platforms must offer similar functionality.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Thanks for the answers, my program is simple enough to alter as first suggested by MK27, i was just curious, but i dont quite get the idea of dup() and dup2(), i tried running your code and i suppose "hello again" should be printed too, however only "hello world" appeared...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. calling function & called function
    By Sajal in forum C Programming
    Replies: 4
    Last Post: 06-05-2009, 09:37 AM
  3. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 01:40 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM