C Output


Displaying information on the screen is one of the most basic tasks in programming. In C, output refers to sending data from the program to the user or an external device, usually through the console or terminal. Understanding output in C is important because it allows you to communicate results, debug programs, and interact with users.

This chapter will guide you through different ways of producing output in C, including standard functions, format specifiers, and best practices for readable results.

What is Output in C?

Output in C is the process of displaying data generated by a program to the user. The most common form of output is text on the screen, but C can also handle other forms, like writing to files. Output is essential because programs are often meant to provide results or information, not just perform calculations silently.

The standard library in C, stdio.h, provides built-in functions to handle output efficiently.

The printf() Function

The most commonly used function for output in C is printf(). It comes from the Standard Input/Output library (stdio.h) and allows you to display text, numbers, and other types of data on the console.

Basic Syntax:

printf("format string", arguments);
  • format string: A string containing text and format specifiers.

  • arguments: Values or variables that will replace the format specifiers.

Example:

#include <stdio.h>

int main() {
    int age = 20;
    printf("I am %d years old.", age);
    return 0;
}

Here, %d is a format specifier for an integer. The age variable replaces %d in the output.

Common Format Specifiers

Format specifiers tell printf() what type of data you want to display. Some commonly used ones include:

  • %d or %i — integer

  • %f — float or double

  • %c — character

  • %s — string

  • %u — unsigned integer

  • %x — hexadecimal integer

  • %% — to display the % symbol

Example:

#include <stdio.h>

int main() {
    int a = 10;
    float b = 5.5;
    char c = 'A';
    char name[] = "Lina";

    printf("Integer: %d\n", a);
    printf("Float: %.2f\n", b); // two decimal places
    printf("Character: %c\n", c);
    printf("Name: %s\n", name);

    return 0;
}

The output will be:

Integer: 10
Float: 5.50
Character: A
Name: Lina

Escape Sequences

C provides escape sequences to control how the output appears. Some commonly used escape sequences:

  • \n — new line

  • \t — horizontal tab

  • \\ — backslash

  • \" — double quote

Example:

printf("Hello\nWorld");

Output:

Hello
World

Printing Multiple Variables

You can print multiple variables in a single printf() statement by adding multiple format specifiers:

int a = 5, b = 10;
printf("a = %d, b = %d", a, b);

Output:

a = 5, b = 10

The order of the variables in the argument list must match the order of the format specifiers.

puts() Function

Another function used for output is puts(). It is simpler than printf() but has some limitations:

  • It automatically adds a newline at the end of the string.

  • It cannot print formatted text or variables; only strings.

Example:

#include <stdio.h>

int main() {
    puts("Hello, world!");
    return 0;
}

Output:

Hello, world!

puts() is useful when you just need to display text quickly without formatting.

putchar() Function

The putchar() function is used to print a single character:

#include <stdio.h>

int main() {
    char ch = 'B';
    putchar(ch);
    return 0;
}

Output:

B

putchar() is often used in loops to display characters one by one, for example when printing arrays or patterns.

Best Practices for Output

  1. Use clear and descriptive messages:
    When displaying results, make it easy for users to understand what the numbers or text represent.

    printf("Total marks obtained: %d\n", totalMarks);
    
  2. Format output properly:
    Align numbers, use tabs or spaces, and control decimal places for floating-point numbers.

    printf("Price: %.2f\n", price);
    
  3. Keep code readable:
    Break long output statements into multiple lines for readability.

  4. Use comments:
    Explain complex output logic with comments so future readers understand your intentions.

Example Program Using Different Output Methods

#include <stdio.h>

int main() {
    char name[] = "Maya";
    int age = 18;
    float marks = 88.5;

    // Using printf()
    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Marks: %.2f\n", marks);

    // Using puts()
    puts("This is a simple message using puts.");

    // Using putchar()
    putchar('X');
    putchar('\n');

    return 0;
}

Output:

Name: Maya
Age: 18
Marks: 88.50
This is a simple message using puts.
X

This example demonstrates different ways to output information in C.

Summary of the Tutorial

Output in C is primarily handled through printf(), puts(), and putchar(). Understanding format specifiers, escape sequences, and proper formatting is essential to produce readable and user-friendly results. Practicing output operations will help you become comfortable with displaying variables, text, and structured information on the screen.


Practice Questions

  1. What is the purpose of the printf() function in C?

  2. Write a printf() statement to display the value of a float variable price with two decimal places.

  3. Explain the difference between puts() and printf().

  4. What does the %c format specifier represent in C?

  5. Write a C program using putchar() to display each character of the string "Lina" on a separate line.

  6. What will be the output of the following code?

    printf("Hello\tWorld\nC Programming");
    
  7. How does C handle escape sequences like \n and \t in output?

  8. Write a printf() statement that displays: Score = 95 where 95 comes from an integer variable named score.

  9. Why is it important to format output properly in a C program?

  10. Explain the difference between printing multiple variables in a single printf() versus multiple printf() statements.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top