Our very own printf!

The printf function in C is a powerful tool for formatting and printing text. It allows you to output text with various formatting options. Here's a sample code demonstrating the use of printf with different format specifiers, including floating-point lengths:

#include <stdio.h>

int main() {
    // Integer formatting
    int integerVariable = 42;
    printf("Integer: %d\n", integerVariable);

    // Character formatting
    char charVariable = 'A';
    printf("Character: %c\n", charVariable);

    // String formatting
    char stringVariable[] = "Hello, printf!";
    printf("String: %s\n", stringVariable);

    // Floating-point formatting
    double doubleVariable = 3.14159;
    printf("Double: %f\n", doubleVariable);

    // Specifying precision for floating-point
    printf("Double with 2 decimal places: %.2f\n", doubleVariable);

    // Scientific notation for floating-point
    printf("Double in scientific notation: %e\n", doubleVariable);

    // Width and precision for floating-point
    printf("Double with width and precision: %10.3f\n", doubleVariable);

    // Printing in hexadecimal
    printf("Integer in hexadecimal: %x\n", integerVariable);

    // Printing in octal
    printf("Integer in octal: %o\n", integerVariable);

    // Printing the address of a variable
    printf("Address of integerVariable: %p\n", (void*)&integerVariable);

    // Printing a percentage sign
    printf("This is a percentage sign: %%\n");

    return 0;
}

To print pointers and unsigned values using printf in C, you can use the following format specifiers:

  1. Printing Pointers:

    • Use %p to print the pointer address.
  2. Printing Unsigned Values:

    • Use %u for unsigned int.

    • Use %lu for unsigned long.

    • Use %llu for unsigned long long.

Here's an example demonstrating how to print pointers and unsigned values:

#include <stdio.h>

int main() {
    int integerValue = -42;
    unsigned int unsignedIntValue = 42;
    unsigned long unsignedLongValue = 1234567890L;
    unsigned long long unsignedLongLongValue = 9876543210ULL;

    int *pointer = &integerValue;

    // Printing pointers
    printf("Pointer address: %p\n", (void*)pointer);

    // Printing unsigned values
    printf("Unsigned integer: %u\n", unsignedIntValue);
    printf("Unsigned long: %lu\n", unsignedLongValue);
    printf("Unsigned long long: %llu\n", unsignedLongLongValue);

    return 0;
}

In this example, %p is used to print the address stored in the pointer, and %u, %lu, and %llu are used for unsigned integer, unsigned long, and unsigned long long values, respectively. Make sure to cast the pointer to (void*) when using %p to avoid compiler warnings.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.