The * and # Modifiers in printf() - C Language Basics

C examples for Language Basics:printf

Introduction

Preceding g, G, f, E, or e specifiers with a # ensures that there will be a decimal point even if there are no decimal digits.

Preceding the x or X specifier with # causes the hexadecimal number to be printed with a 0x prefix.

Preceding the o specifier with # causes the number to be printed with a leading zero.

Preceding the a specifier with # ensures that a decimal point will be displayed.

The minimum field width and precision specifiers can be provided by arguments to printf() by using an * as a placeholder.

printf( ) will match the * to an argument in the order in which they occur.

The following program illustrates both # and *:

Demo Code

#include <stdio.h>

int main(void)
{
   printf("%x %#x\n", 10, 10);
   printf("%*.*f", 10, 4, 1234.34);

   return 0;/*from w  w  w. j  a v  a  2  s.c  o  m*/
}

Result


Related Tutorials