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
  • Common Format Specifiers
  • 1. %d - Integer
  • 2. %f - Float
  • 3. %lf - Double
  • 4. %c - Char
  • 5. %s - String
  • 6. %p - Pointer
  • 7. %x, %X - Hexadecimal
  • Conclusion

Was this helpful?

  1. Basic Concepts

Format Specifiers

Format specifiers are placeholders used in C programming to specify the type and format of data to be displayed or read. They play a crucial role in functions like printf() for output and scanf() for input. Let's explore some common format specifiers and their usage.

Common Format Specifiers

1. %d - Integer

Used for displaying or reading integer values.

#include <stdio.h>

int main() {
    int number = 42;
    
    // Displaying integer
    printf("Number: %d\n", number);

    // Reading integer
    int input;
    printf("Enter a number: ");
    scanf("%d", &input);

    return 0;
}

2. %f - Float

Used for displaying or reading floating-point numbers.

#include <stdio.h>

int main() {
    float pi = 3.14;

    // Displaying float
    printf("Value of pi: %f\n", pi);

    // Reading float
    float radius;
    printf("Enter the radius: ");
    scanf("%f", &radius);

    return 0;
}

3. %lf - Double

Similar to %f but used for double-precision floating-point numbers.

#include <stdio.h>

int main() {
    double price = 24.99;

    // Displaying double
    printf("Price: %lf\n", price);

    // Reading double
    double total;
    printf("Enter the total: ");
    scanf("%lf", &total);

    return 0;
}

4. %c - Char

Used for displaying or reading individual characters.

#include <stdio.h>

int main() {
    char grade = 'A';

    // Displaying char
    printf("Grade: %c\n", grade);

    // Reading char
    char symbol;
    printf("Enter a symbol: ");
    scanf(" %c", &symbol);  // Note the space before %c to consume the newline character

    return 0;
}

5. %s - String

Used for displaying or reading strings.

#include <stdio.h>

int main() {
    char name[] = "John";

    // Displaying string
    printf("Name: %s\n", name);

    // Reading string
    char input[50];
    printf("Enter your name: ");
    scanf("%s", input);

    return 0;
}

6. %p - Pointer

Used for displaying pointers.

#include <stdio.h>

int main() {
    int number = 42;
    int *ptr = &number;

    // Displaying pointer address
    printf("Address of number: %p\n", (void*)ptr);

    return 0;
}

7. %x, %X - Hexadecimal

Used for displaying or reading values in hexadecimal format.

#include <stdio.h>

int main() {
    int hexValue = 0x1A;  // Hexadecimal representation

    // Displaying hexadecimal
    printf("Hexadecimal Value: %x\n", hexValue);

    // Reading hexadecimal
    int inputHex;
    printf("Enter a hexadecimal value: ");
    scanf("%x", &inputHex);

    return 0;
}

Conclusion

Understanding format specifiers is crucial for precise input and output operations in C programming. By using the appropriate specifier, you ensure that the data is interpreted correctly, enhancing the reliability and functionality of your programs.

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

PreviousData Types and VariablesNextCommand Line Arguments

Was this helpful?