Type Modifiers and Format Specifiers

An unsigned type name is the signed type prefixed with the keyword unsigned. We use unsigned types when using integer values that cannot be negative, like a person’s age or a database record ID number. Because an unsigned type does not have to represent negative numbers, it’s range of positive numbers is twice as large as its corresponding signed type. This is because signed numbers use one bit as a sign bit.

#include <stdio.h>


int main(void){
  
  int x = 90210;
  unsigned int y = 8675309U;
 
  
  /*
   * use %u to display
   * unsigned int
   */
  printf("x = %d\n", x);
  printf("y = %u\n", y);
  
  return 0;
}

To display the value of an unsigned int, use the conversion characters %u. For long integers, add an l modifier prefix to the conversion character, for type long long use two letter ls.

When specifying integer constants as long, we must append an uppercase or lowercase letter L to the numeric value. It’s evident that using an uppercase L is more readable that its lowercase counterpart, because it can’t be confused with the number 1. When specifying integer constants as unsigned, we must append the letter U to the numeric value.

#include <stdio.h>

int main(void){
  
  long int a = 57600000L;
  long long int b = 149600000LL;
  
  printf("long int a = %ld\n", a);
  
  printf("long long int b = %lld\n", b);
  
  
 return 0; 
}

The types unsigned and unsigned int, as well as unsigned long and unsigned long int, are the same; however, it is generally considered best practice to use the more descriptive data type and modifier combination.

#include <stdio.h>

int main(void){
 
  unsigned long long y = 2600ULL; 
  

  printf("y = %llu\n", y);
  
  y = y * y;
  
  printf("y^2 = %llu\n", y);
  
  y = 2600ULL;
  
  y = y * y * y;
  
  printf ("y^3 = %llu\n", y);
  
  y = 2600ULL;
  
  y = y * y * y * y;
  
  printf ("y^3 = %llu\n", y);
  
  return 0;
  
}

Values of type float are known as single precision floating-point numbers; values of type double are known as double precision floating-point numbers. The long double type provides an even greater range and precision than double.

#include <stdio.h>


int main(void){
 
  float piOne = 3.1459f;
  double piTwo = 3.14159265359;
  long double piThree = 3.1415926535897932L;
  
  
  printf("Pi = %f\n", piOne);
  printf("Pi = %f\n", piTwo);
  printf("pi =% Lf\n", piThree);
  
  printf("Wait.... that's:\n");
  
    printf("Pi = %.11f\n", piTwo);
  printf("pi =%.16Lf\n", piThree);
  
  
  return 0;
  
}

We can specify the number of fractional digits to display by including a number after the decimal in the format specifier. By default, the format specifier %f displays six fractional digits.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s