C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • The printf() Function
  • Formatting Output
  • Escape Sequences
  • Field Width and Precision
  • Concatenating Strings
  • Conclusion

Was this helpful?

  1. Basic Concepts

Displaying Output

Displaying output is a fundamental aspect of C programming, allowing you to communicate information to users through the console. In this section, we'll explore various techniques and functions used to display output in C, focusing on the commonly used printf() function.

The printf() Function

The printf() function is the primary tool for output in C. It allows you to format and display text, variables, and other data on the console. Here's a basic example:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

In this example, the string "Hello, World!\n" is printed to the console, where represents a newline character.

Formatting Output

printf() supports various format specifiers that control the display of different types of data. Here are some common ones:

  • %d: Integer

  • %f: Float

  • %c: Character

  • %s: String

#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.8;
    char grade = 'A';
    char name[] = "John";

    printf("Age: %d\n", age);
    printf("Height: %.1f feet\n", height);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}

Escape Sequences

Escape sequences are special characters that represent non-printable characters or provide specific formatting. Common escape sequences include for a newline, for a tab, and \" for a double quote.

#include <stdio.h>

int main() {
    printf("This is a new line.\n");
    printf("This text is\ttabbed.\n");
    printf("He said, \"Hello!\"\n");

    return 0;
}

Field Width and Precision

You can control the field width and precision of the displayed values using numeric parameters in the format specifier. For example, %5d specifies a field width of 5 for an integer.

#include <stdio.h>

int main() {
    int num1 = 123;
    float num2 = 12.3456;

    printf("Number 1: %5d\n", num1);
    printf("Number 2: %.2f\n", num2);

    return 0;
}

Concatenating Strings

You can concatenate strings using the %s specifier. Additionally, you can use multiple specifiers in a single printf() statement.

#include <stdio.h>

int main() {
    char first_name[] = "John";
    char last_name[] = "Doe";

    printf("Full Name: %s %s\n", first_name, last_name);

    return 0;
}

Conclusion

Displaying output in C is achieved through the versatile printf() function. By understanding format specifiers, escape sequences, and additional options, you can present information effectively to users. This is a fundamental skill that lays the groundwork for creating interactive and informative C programs.

In the upcoming sections, we'll explore more advanced concepts in C programming. If you have specific questions or areas you'd like to explore further, feel free to ask. Happy coding!

PreviousThe #include StatementNextReading Input from the Terminal

Was this helpful?