Skip to content

Input and Output

Formatting output refers to controlling the appearance and structure of the data displayed when printing to the console or other output mediums. It allows you to present data in a specific way, adjusting the appearance of numbers, text, or other information to enhance readability or meet specific requirements.

c
int num = 42;
float pi = 3.14159;

printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi);
c++
int num = 42;
float pi = 3.14159;
cout << "Integer: " << num << endl;
cout << fixed << setprecision(2) << "Float: " << pi << endl;
java
int num = 42;
double pi = 3.14159;

System.out.printf("Integer: %d\n", num);
System.out.printf("Float: %.2f\n", pi);
python
num = 42
pi = 3.14159

print(f"Integer: {num}")
print("Float: {:.2f}".format(pi))

Format specifier

  • %d: Used for integers (int).
  • For example, printf("%d", 10); will display 10.
  • %f: Used for floating-point numbers (float or double).
  • For example, printf("%.2f", 3.14159); will display 3.14.
  • %c: Used for characters (char).
  • For example, printf("%c", 'A'); will display A.
  • %s: Used for strings (char* or std::string in C++).
  • For example, printf("%s", "Hello"); will display Hello.