What is difference between printf and cout?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vrinda yadav
    New Member
    • Apr 2010
    • 5

    #1

    What is difference between printf and cout?

    What is the difference between the following two statements in a c++ code.?
    1. cout<<*p; //error
    2. printf("%d",*p) ; //no error
  • prasoonsaurav
    New Member
    • Dec 2009
    • 3

    #2
    Please show the complete code.

    If 'p' is a pointer to an integer and if it has been properly initialized then both (1) and (2) print the values at the addresses being pointed to by 'p'.

    Make sure that you have properly initialized 'p', otherwise the behaviour is undefined.

    Comment

    • vrinda yadav
      New Member
      • Apr 2010
      • 5

      #3
      proper code:

      Code:
      #include<iostream.h>
      
      struct temp
      {
        int j;
        int i;
      }a;
      
      void fun(struct temp *p)
      {
        cout<<*p; //error
        p++;
        cout<<*p;
      }
      
      void main()
      {
        struct temp *z=&a;
        z->j=10;
        z->i=1;
        fun(z);
      }
      in this code,
      output: 10 <garbage>
      if used with printf statements
      but does not work with cout statements
      as given above in function fun.

      this is what i want to know the difference between the two.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        printf is a C function and has no type checking therefore it does not know that you have attempted an operation that makes no sense and that ultimately attempts an out of bounds memory access, undefined behaviour that may cause a crash.

        On the other hand cout from C++ is type aware and has detected that you have tried to print a type struct temp that it has no idea how to print and so raises a compile error avoiding the out of bounds memory access that the C code compiles to.


        You need to work out that you wanted void fun(struct temp *p) to do and what it actually does.

        Comment

        • vrinda yadav
          New Member
          • Apr 2010
          • 5

          #5
          if printf("%d",*p) ; statement is replaced with printf("%c",*p) ; in the above code
          then what would be the change in the output and why..??

          Comment

          • poori
            New Member
            • Apr 2010
            • 1

            #6
            good question

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              This is pretty basic stuff that you're asking us to answer. Most of this can be easily found using google.

              For example, the first article that I found on the topic using google was this printf C++ reference which covers what you are asking:

              %c is for printing a character.
              %d is for printing a decimal integer


              So if *p contains a number and you use %c to print it, ASCII value for the number is printed onto the screen. If you use %d print *p, then the number stored in *p will be printed.

              Please take the time to research the problem before you post your question. The experts here are more than willing to help you with a specific problem but you have to do your part to learn the basics. Please take the time to read over the Posting Guidelines specifically the section on Before you Post your Question.

              -Frinny

              Comment

              • vrinda yadav
                New Member
                • Apr 2010
                • 5

                #8
                I did not mean this way..rather I wanted to know what is meant by type checking that is not supported by printf statement..if it can differentiate between whether printing an integer or a character as above..

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Since *p is not a char or an integer using %d or %c on it is still just a recipe for disaster as I pointed out in post #4.

                  Type checking is where the compiler checks the type of the variable being passed to ensure it matches with what the function is expecting. The %c or %d do not invoke type checking, the compiler is doing nothing, the programmer is explicitly telling the function how to treat the data passed to it by putting special characters in a string that the printf function then interprets. The function will then treat the passed data as the type the programmer says it is regardless of what is actually passed.

                  printf can do no type checking because it is a varidac (variable argument list) function, the compiler has specifically been told printf can accept anything, do not bother type checking it.

                  Comment

                  • vrinda yadav
                    New Member
                    • Apr 2010
                    • 5

                    #10
                    *p is a pointer to struct temp.
                    It would be having a memory address wherever it is located.
                    incrementing p gives out of memory bounds.
                    can we print (or access) the two integers contained in it using pointer?
                    I mean like the way we can for an integer array.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      No *p is the struct temp it is not a pointer to anything, p is the pointer to struct temp.

                      Of course you can print the contains in the same way you access the contents at any other point in the code, use the pointer member access operator ->

                      cout << p->j << endl;

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        The difference between printf and cout is that printf is a function and cout is a variable.

                        printf is a function that can output built-in types to stdout. End of speech. It cannot output your Date object variable as mm/dd/yyyy.

                        cout, on the other hand is an object of basic_ostream. That class has an operator<< function that can output built-in types to stdout. This operator<< is the equivalent of printf.

                        However,by writing your own operator<< for your Date class, the cout object can be used to output your Date object as mm/dd/yyyy to stdout. This you cannot do with printf.

                        In short, in C use printf. In C++ use operator<< unless uou want your C++ progam as inflexible as a C program.

                        Comment

                        Working...