Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Difference Between printf and cout in C++



Both printf() and Cout are used to display the output format in C and C++ respectively. Each function has its syntax and characteristics that are based on user needs and preferences. In this article, we will learn the difference between print () and cout in a detailed manner.

What is printf() in C?

The printf() function is mainly used in C language. It is a formatting function that prints to the standard out. It prints to the console and takes a format specifier to print. It returns an integer value. It is not type-safe in input parameters. It can be used in C++ language too.

Syntax

Following is the syntax of printf() in C and C++ language:

printf("string and format specifier", variable_name);

Here,

  • String: Any text/message to print on console.
  • Format Specifier: According to the variable datatype, use format specifiers like %d, %s etc.
  • variable_name: Any name given to declare the variable.

Example of printf() in C/C++

The printf() function is used to display the output, but to print in C++ use the header "cstdio" that compiles the program.

C C++
#include <stdio.h>

int main() {
    int a = 10;
    printf("The value of a is %d\n", a);
    return 0;
}

Output

The above code produces the following result ?

The value of a is 10
#include <iostream>
#include <cstdio>

int main() {
    int a = 10;
    printf("The value of a is %d\n", a);
    return 0;
}

Output

The above code produces the following result ?

The value of a is 10

What is cout in C++?

In C++, cout is an object of iostream in C++ language. It also prints to the console. It does not take any format specifier to print. It does not return anything. It is type safe in input parameters.

Syntax

Here is the syntax of writing cout in C++:

cout << "string" << variable_name << endl;

Here,

  • string : Any text/message to print on console.
  • variable_name : Any name given to the variable at the time of declaration.

Example of cout in C++

Following is the C++ program where string input is printed as a result.

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main() {
  string str = "tutorialspoint";

  cout << "The value of str : " << str;
  // c_str - representing the current value of the string object.
  printf("\nThe value of str : %s", str.c_str());

  return 0;
}

Output

The above code produces the following output:

The value of str : tutorialspoint
The value of str : tutorialspoint

Difference Between C printf() and C++ cout

In this section, we show the short difference of both the language based on features of printf() and cout:

Feature printf() cout
Language C C++
Header File <stdio.h> <iostream>
Syntax Style Function-based Operator-based (<<)
Type Safety Not type-safe Type-safe
Formatting Uses format specifiers (%d, %s) Uses stream manipulators (setw, setprecision)
Flexibility Less flexible with custom types More flexible with overloading
Speed It is faster It is slower
Extensibility Hard to extend Easy to extend for custom classes
Updated on: 2025-04-21T18:01:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements