Thread: printf vs cout

  1. #1
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82

    printf vs cout

    Greetings everyone,

    I was wondering if anyone can give me suggestions about the pros and cons of using cout over printf. I have found that sometimes its easier to use one, while other times its easier to use the other.

    Thanks,
    Jeff

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are programming in C++, you should use cout and friends - the main advantage is that you can use operator overloading to write out class objects, which allow you to extend the types that are supported by C++ - something printf absolutely can't do. If you are programming in C, then printf is the only reasonable option.

    I'm not aware of anything you can't do with cout (but yes, it's sometimes a bit more difficult to get perfect formatting, compared to printf).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Don't use '*printf*', '*scanf*', and the like.

    If you find the C++ stream manipulators difficult use 'Boost::Format' or similar.

    Or if you want a truly type-safe alternative, search "CodeProject" and the like. I'm sure it is out there somewhere. ('Boost::Format' is type-safe after a fashion, but the format specifier is ignored in such cases of misuse.)

    *shrug*

    Or just write it yourself.

    Soma

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >extend the types that are supported by C++ - something printf absolutely can't do
    Technically it's possible if you realize that printf is a glorified to_string function. If you provide that to_string operation in your class, you can use printf's string specifier. That's not much different from overloading the << operator.
    My best code is written with the delete key.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What?! O_o

    Soma

  6. #6
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Ok so what if I do this?

    Code:
    #include <string>
    #include <cstdio>
    
    class MyClass
    {
    public:
      MyClass() : s("")
      {
    
      }
      virtual ~MyClass()
      {
    
      }
    
      void set(const char *s)
      {
        this->s = s;
      }
    
      const char *get(void) const
      {
        return this->s.c_str();
      }
    
      operator char *()
      {
        return s.c_str();
      }
    
      operator const char *() const
      {
        return s.c_str();
      }
    
    private:
      std::string s;
    };
    
    int main(void)
    {
      MyClass whatever;
    
      whatever.set("Hello world!");
    
      printf("What you say? &#37;s\n", (char *)whatever);
    
      return 0;
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    >extend the types that are supported by C++ - something printf absolutely can't do
    Technically it's possible if you realize that printf is a glorified to_string function. If you provide that to_string operation in your class, you can use printf's string specifier. That's not much different from overloading the << operator.
    Ok, so the word "absolutely" is perhaps not quite right - but it's clearly not actually extending printf() to cope with a new type, but rather creating a function that produces a string from the new type, and print it using %s, right?

    Of course, if we introduce a "to_string" in each class, it's REALLY EASY to make a operator<< using the to_string function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by c++0x View Post
    Ok so what if I do this?

    <snip code>
    Of course, that will print the string value from the char* operator(), but it's still not MODIFYING printf to take the new type - you are just returning a string from the operator. And I fail to see how that is of any benefit over using operator<< (which can of course, like I pointed out in Prelude's example) use the char* operator() if you like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Are the cast operators able to produce lvalues?

  10. #10
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    I want to do something like this

    Code:
    (int)object = 5;
    And half it set a member of object too 5. I guess maybe I should stick to regular eecwals sine operators huh?

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Even versions of '*printf*' that provide extensions to support user defined types on compilers that implement parsing to match types from the format string suffer from mechanical and semantic problems that are trivially solved with C++.

    Soma

  12. #12
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    but dood, if u want to do printf("&#37;*.3s", x, s) it is not as e-z.

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by c++0x View Post
    I want to do something like this

    Code:
    (int)object = 5;
    And half it set a member of object too 5. I guess maybe I should stick to regular eecwals sine operators huh?
    Are you talking about operator overloading. Something like this?
    Code:
    #include <iostream>
    #include <string>
    
    class COverLoaded
    {
    public:
    	COverLoaded() : mNumericValue(0) { }
    public:
    	COverLoaded &operator=(const int value)
    	{
    		this->mNumericValue = value;
    		return *this;
    	}
    	COverLoaded &operator=(const std::string stringValue)
    	{
    		this->mStringValue = stringValue;
    		return *this;
    	}
    public:
    	const int GetNumericValue() const
    	{
    		return this->mNumericValue;
    	}
    	const std::string &GetStringValue() const
    	{
    		return this->mStringValue;
    	}
    private:
    	int mNumericValue;
    	std::string mStringValue;
    };
    
    
    int main()
    {
    	COverLoaded overloadedClass;
    
    	//
    	//Assign the values of the class(Note: I am using = for both a string and an int)
    	//
    	overloadedClass = 50;
    	overloadedClass = "Some string value";
    
    	//
    	//Output the internal values of the class
    	//
    	std::cout<<"Numeric Value = "<<overloadedClass.GetNumericValue()<<std::endl;
    	std::cout<<"String Value = "<<overloadedClass.GetStringValue()<<std::endl;
    
    	std::cin.get();
    
    	return 0;
    }
    Woop?

  14. #14
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Yeah, nvm what I was asking. I don't know why I wanted an lvalue. It doesn't even make sense.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The stream insertion operator makes things convenient and extensible, but I think it's hard to dispute the compactness and clarity of printf-style format specifiers, even if they aren't type-safe. If you want to use printf-style formatting, I'd suggest however that you use sprintf() to print into character arrays and then send the output through iostreams. Don't call printf() directly. The two libraries don't really play nice in terms of buffering or terminal settings.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. (printf VS cout) and (including libraries)
    By \cFallen in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 09:47 PM