TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C Format Specifier

C Format Specifier with Tutorial or what is c programming, C language with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.

<< Back to C

C Format Specifier

The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character.

The commonly used format specifiers in printf() function are:

Format specifier Description
%d or %i It is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values.
%u It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value.
%o It is used to print the octal unsigned integer where octal integer value always starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc.
%X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc.
%f It is used for printing the decimal floating-point values. By default, it prints the 6 values after '.'.
%e/%E It is used for scientific notation. It is also known as Mantissa or Exponent.
%g It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output.
%p It is used to print the address in a hexadecimal form.
%c It is used to print the unsigned character.
%s It is used to print the strings.
%ld It is used to print the long-signed integer value.

Let's understand the format specifiers in detail through an example.

  • %d
int main()
{
  int b=6;
  int c=8;
  printf("Value of b is:%d", b);
  printf("\nValue of c is:%d",c);

    return 0;
}

In the above code, we are printing the integer value of b and c by using the %d specifier.

Output

C Format Specifier
  • %u
int main()
{
  int b=10;
  int c= -10;
  printf("Value of b is:%u", b);
  printf("\nValue of c is:%u",c);

    return 0;
}

In the above program, we are displaying the value of b and c by using an unsigned format specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it does not print the value of c as c contains the negative value.

Output

C Format Specifier
  • %o
int main()
{
  int a=0100;
  printf("Octal value of a is: %o", a);
  printf("\nInteger value of a is: %d",a);
  return 0;
}

In the above code, we are displaying the octal value and integer value of a.

Output

C Format Specifier
  • %x and %X
int main()
{
  int y=0xA;
  printf("Hexadecimal value of y is: %x", y);
  printf("\nHexadecimal value of y is: %X",y);
  printf("\nInteger value of y is: %d",y);
    return 0;
}

In the above code, y contains the hexadecimal value 'A'. We display the hexadecimal value of y in two formats. We use %x and %X to print the hexadecimal value where %x displays the value in small letters, i.e., 'a' and %X displays the value in a capital letter, i.e., 'A'.

Output

C Format Specifier
  • %f
int main()
{
  float y=3.4;
  printf("Floating point value of y is: %f", y);
  return 0;
}

The above code prints the floating value of y.

Output

C Format Specifier
  • %e
int main()
{
  float y=3;
  printf("Exponential value of y is: %e", y);
  return 0;
}

Output

C Format Specifier
  • %E
int main()
{
  float y=3;
  printf("Exponential value of y is: %E", y);
  return 0;
}

Output

C Format Specifier
  • %g
int main()
{
  float y=3.8;
  printf("Float value of y is: %g", y);
  return 0;
}

In the above code, we are displaying the floating value of y by using %g specifier. The %g specifier displays the output same as the input with a same precision.

Output

C Format Specifier
  • %p
int main()
{
  int y=5;
  printf("Address value of y in hexadecimal form is: %p", &y);
  return 0;
}

Output

C Format Specifier
  • %c
int main()
{
  char a='c';
  printf("Value of a is: %c", a);
  return 0;
}

Output

C Format Specifier
  • %s
int main()
{
  printf("%s", "javaTpoint");
  return 0;
}

Output

C Format Specifier

Minimum Field Width Specifier

Suppose we want to display an output that occupies a minimum number of spaces on the screen. You can achieve this by displaying an integer number after the percent sign of the format specifier.

int main()
{
 int x=900;
  printf("%8d", x);
  printf("\n%-8d",x);
  return 0;
}

In the above program, %8d specifier displays the value after 8 spaces while %-8d specifier will make a value left-aligned.

Output

C Format Specifier

Now we will see how to fill the empty spaces. It is shown in the below code:

int main()
{
 int x=12;
  printf("%08d", x);
  return 0;
}

In the above program, %08d means that the empty space is filled with zeroes.

Output

C Format Specifier

Specifying Precision

We can specify the precision by using '.' (Dot) operator which is followed by integer and format specifier.

int main()
{
 float x=12.2;
  printf("%.2f", x);
  return 0;
} 

Output

C Format Specifier
Next TopicC Escape Sequence




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf