Results 1 to 6 of 6

Thread: Output Functions

  1. #1
    Expert Member
    Join Date
    May 2006
    Answers
    114

    Output Functions

    Can someone tell me the differences between cout and printf functions? I want to know which one is better in usage than other and the reason for that.


  2. #2
    Junior Member
    Join Date
    Sep 2006
    Answers
    16

    Re: Output Functions

    cout is an object of the iostream in C++.printf has same function as cout is mainly used in C language.One more difference is that printf returns an integer value (the number of characters actually printed) and cout does not return anything.


  3. #3
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Output Functions

    Well. cout is much more advanced than printf. Cou is implemented using OOPS concepts. So, the "<< " (Outstream) operator is opverloaded for all user defined data types. Also you can overload the operator for any custom data type for printing the output.

    Let us assume if you want to print a string.
    Using printf("The string is %s\n", str);
    using cout<<"The string is "<<str<<endl;

    Note that I didn't need to specify str is of ty pe string by typing "%s" for cout. As it doesn't need programmer to identify what type of data is being outputted. It can take care of these details by itself.

    Obviously cout is better. If you coding in C++, don't even think about printf();

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  4. #4
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    Re: Output Functions

    Though both are used to print something on the output screen,

    printf is a function that returns integer (no. of chars. printed). See its declaration in the header file 'stdio.h'

    int _Cdecl printf(const char *__format, ...);

    it takes arguments and they need to be enclosed in the parenthesis. The first argument is a string to be printed and the rest are variable names.


    cout is a predefined object of type ‘ostream’ (generally a computer monitor) and << is an operator that diverts the string or literals following it into the predefined object ‘cout’. See its declaration in the header file ‘iostream.h’

    extern ostream_withassign _Cdecl cout;

    ***** Please be cautious as the header files (*.h) are system files and any manipulations done in it, knowingly or un-knowingly would spoil the system file and your C/C++ compiler will not work *****

    Any further clarifications? Feel free to ask


  5. #5
    Expert Member
    Join Date
    May 2006
    Answers
    114

    Re: Output Functions

    Friends your replies are really great. I could get a wide knowledge on output functions from your answers. Thanks for the same.


  6. #6
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Output Functions

    In my previous post, I had done a mistake. The oprator << is overloaded to print all in built data types and we can overload it for printing any datatype defined by us.
    (Had written it is overloaded for all user defined datatypes which is wrong. We can overload it for all user defined datatypes though)

    Sorry for the mistake. Had typed it in a hurry....

    Cheers,
    Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  7.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact