Variables and Data Types in C Programming

June 04, 2018 | 1 Tags | 0 Comment

Variables and Data Types in C Programming

A variable is a named location in a memory where a program can manipulate the data.

    Rules for naming c variable:
  1. Variable name can contain letters, digits and the underscore. Variable name must begin with letter or underscore
  2. Variable names are case sensitive. Upper and lowercase letters are distinct.
  3. Variable name must not be the same as C reserved words or keywords
    Examples of valid C variable names
  • foo
  • Bar
  • BAZ
  • foo_bar
  • _foo42
  • _
  • QuUx
    Examples of invalid C variable names:
  • 2foo (must not begin with a digit)
  • my foo (spaces not allowed in names)
  • $foo ($ not allowed)
  • while (keywords cannot be used as names)

Each variable in C has a specific type, which determines the size (the range of values that can be stored within that memory) and layout of the variable’s memory (the set of operations that can be applied to the variable).

Basic Data Types in C

Macro Memory (bytes) Range Format Specifier
short int 2 -32.768 (-2^15) to 32.767 (2^15 - 1) %hd
unsigned short int 2 0 to 65.535 (2^16 - 1) %hu
unsigned int 4 0 to 4.294.967.295 (2^32 - 1) %u
int 4 -2.147.483.648 (-2^31) to 2.147.483.647 (2^31 - 1) %d
unsigned long int 4 0 to 4.294.967.295 (2^32 - 1) %lu
long int 4 -2.147.483.648 (-2^31) to 2.147.483.647 (2^31 - 1) %ld
long long int 8 -2^63 to 2^63 - 1 %lld
unsigned long long int 8 0 to 2^64 - 1 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c

Macros defined in limits.h header define constants with the limits of basic data types.

CHAR_BIT number of bits in a byte
MB_LEN_MAX maximum number of bytes in a multibyte character
CHAR_MIN minimum value of char
CHAR_MAX maximum value of char
SCHAR_MIN
SHRT_MIN
INT_MIN
LONG_MIN
LLONG_MIN
minimum value of signed char, short, int, long and long long respectively
SCHAR_MAX
SHRT_MAX
INT_MAX
LONG_MAX
LLONG_MAX
maximum value of signed char, short, int, long and long long respectively
UCHAR_MAX
USHRT_MAX
UINT_MAX
ULONG_MAX
ULLONG_MAX
maximum value of unsigned char, unsigned short, unsigned int, unsigned long and unsigned long long respectively
#include <stdio.h>
#include <limits.h>

int main() {

   printf("The number of bits in a byte %d\n", CHAR_BIT);

   printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
   printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
   printf("Storage size for SIGNED CHAR : %lu \n", sizeof(signed char));

   printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);
   printf("Storage size for UNSIGNED CHAR : %lu \n", sizeof(unsigned char));

   printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
   printf("The maximum value of SHORT INT = %d\n", SHRT_MAX);
   printf("Storage size for SHORT INT : %lu \n", sizeof(short int));

   printf("The maximum value of UNSIGNED SHORT INT = %d\n", USHRT_MAX);
   printf("Storage size for UNSIGNED SHORT INT : %lu \n", sizeof(unsigned short int));

   printf("The minimum value of INT = %d\n", INT_MIN);
   printf("The maximum value of INT = %d\n", INT_MAX);
   printf("Storage size for INT : %lu \n", sizeof(int));

   printf("The maximum value of UNSIGNED INT = %d\n", UINT_MAX);
   printf("Storage size for UNSIGNED INT : %lu \n", sizeof(unsigned int));

   printf("The minimum value of LONG INT = %ld\n", LONG_MIN);
   printf("The maximum value of LONG INT = %ld\n", LONG_MAX);
   printf("Storage size for LONG INT : %lu \n", sizeof(long int));

   printf("The maximum value of UNSIGNED LONG INT = %lu\n", ULONG_MAX);
   printf("Storage size for UNSIGNED LONG INT : %lu \n", sizeof(unsigned long int));

   printf("The minimum value of LONG LONG INT = %lld\n", LLONG_MIN);
   printf("The maximum value of LONG LONG INT = %lld\n", LLONG_MAX);
   printf("Storage size for LONG LONG INT : %lu \n", sizeof(long long int));

   printf("The maximum value of UNSIGNED LONG LONG INT = %llu\n", ULLONG_MAX);
   printf("Storage size for UNSIGNED LONG LONG INT : %lu \n", sizeof(unsigned long long int));

   return 0;
}

Float data types in C

Data Type Memory (bytes) Range Precision (decimal places) Format Specifier
float 4 -1.2E-38 to -3.4E+38 and 1.2E-38 to 3.4E+38 6 %f
double 8 -2.3E-308 to -1.7E+308 and 2.3E-308 to 1.7E+308 15 %lf
long double 16 -3.3E-4932 to -1.1E+4932 and 3.3E-4932 to 1.1E+4932 18 %Lf

Macros defined in float.h header define constants with the limits of float data types.

FLT_RADIX the radix (integer base) used by the representation of all three floating-point types (float, double, long double)
DECIMAL_DIG conversion from long double to decimal with at least DECIMAL_DIG digits and back to long double is the identity conversion: this is the decimal precision required to serialize/deserialize a long double
FLT_DECIMAL_DIG
DBL_DECIMAL_DIG
LDBL_DECIMAL_DIG
conversion from float/double/long double to decimal with at least FLT_DECIMAL_DIG/DBL_DECIMAL_DIG/LDBL_DECIMAL_DIG digits and back is the identity conversion: this is the decimal precision required to serialize/deserialize a floating point value
FLT_MIN
DBL_MIN
LDBL_MIN
minimum, normalized, positive value of float, double and long double respectively
FLT_TRUE_MIN
DBL_TRUE_MIN
LDBL_TRUE_MIN
minimum positive value of float, double and long double respectively
FLT_MAX
DBL_MAX
LDBL_MAX
maximum value of float, double and long double respectively
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON
difference between 1.0 and the next representable value for float, double and long double respectively
FLT_DIG
DBL_DIG
LDBL_DIG
number of decimal digits that are guaranteed to be preserved in text to float/double/long double and back to text roundtrip without change due to rounding or overflow
FLT_MANT_DIG
DBL_MANT_DIG
LDBL_MANT_DIG
number of base-FLT_RADIX digits that are in the floating-point mantissa and that can be represented without losing precision for float, double and long double respectively
FLT_MIN_EXP
DBL_MIN_EXP
LDBL_MIN_EXP
minimum negative integer such that FLT_RADIX raised by power one less than that integer is a normalized float, double and long double respectively
FLT_MIN_10_EXP
DBL_MIN_10_EXP
LDBL_MIN_10_EXP
minimum negative integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
FLT_MAX_EXP
DBL_MAX_EXP
LDBL_MAX_EXP
maximum positive integer such that FLT_RADIX raised by power one less than that integer is a normalized float, double and long double respectively
FLT_MAX_10_EXP
DBL_MAX_10_EXP
LDBL_MAX_10_EXP
maximum positive integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
FLT_HAS_SUBNORM
DBL_HAS_SUBNORM
LDBL_HAS_SUBNORM
whether the type supports subnormal (denormal) numbers: -1 indeterminable, 0 absent, 1 present
FLT_ROUNDS rounding mode of floating-point arithmetics
FLT_EVAL_METHOD use of extended precision for intermediate results: 0 not used, 1 double is used instead of float, 2: long double is used
#include <stdio.h>
#include <float.h>

int main() {
   printf("FLT_RADIX    = %d\n", FLT_RADIX);
   printf("DECIMAL_DIG  = %d\n", DECIMAL_DIG);

   printf("FLT_MIN      = %e\n", FLT_MIN);
   printf("FLT_MAX      = %e\n", FLT_MAX);
   printf("FLT_EPSILON  = %Le\n", FLT_EPSILON);
   printf("FLT_DIG      = %d\n", FLT_DIG);
   printf("FLT_MANT_DIG = %d\n", FLT_MANT_DIG);
   printf("FLT_MIN_EXP  = %d\n",  FLT_MIN_EXP);
   printf("FLT_MIN_10_EXP  = %d\n",  FLT_MIN_10_EXP);
   printf("FLT_MAX_EXP     = %d\n",  FLT_MAX_EXP);
   printf("FLT_MAX_10_EXP  = %d\n",  FLT_MAX_10_EXP);
   printf("FLT_ROUNDS      = %d\n",  FLT_ROUNDS);
   printf("FLT_EVAL_METHOD = %d\n",  FLT_EVAL_METHOD);
   printf("FLT_HAS_SUBNORM = %d\n",  FLT_HAS_SUBNORM);

   printf("DBL_MIN      = %e\n", DBL_MIN);
   printf("DBL_MAX      = %e\n", DBL_MAX);
   printf("DBL_EPSILON  = %Le\n", DBL_EPSILON);
   printf("DBL_DIG      = %d\n", DBL_DIG);
   printf("DBL_MANT_DIG = %d\n", DBL_MANT_DIG);
   printf("DBL_MIN_EXP  = %d\n",  DBL_MIN_EXP);
   printf("DBL_MIN_10_EXP  = %d\n",  DBL_MIN_10_EXP);
   printf("DBL_MAX_EXP     = %d\n",  DBL_MAX_EXP);
   printf("DBL_MAX_10_EXP  = %d\n",  DBL_MAX_10_EXP);
   printf("DBL_HAS_SUBNORM = %d\n",  DBL_HAS_SUBNORM);

   printf("LDBL_MIN      = %Le\n", LDBL_MIN);
   printf("LDBL_MAX      = %Le\n", LDBL_MAX);
   printf("LDBL_EPSILON  = %Le\n", LDBL_EPSILON);
   printf("LDBL_DIG      = %d\n", LDBL_DIG);
   printf("LDBL_MANT_DIG = %d\n", LDBL_MANT_DIG);
   printf("LDBL_MIN_EXP  = %d\n",  LDBL_MIN_EXP);
   printf("LDBL_MIN_10_EXP  = %d\n",  LDBL_MIN_10_EXP);
   printf("LDBL_MAX_EXP     = %d\n",  LDBL_MAX_EXP);
   printf("LDBL_MAX_10_EXP  = %d\n",  LDBL_MAX_10_EXP);
   printf("LDBL_HAS_SUBNORM = %d\n",  LDBL_HAS_SUBNORM);

   return 0;
}

Before using a variable, you must declare it. To declare a variable, you must specify its data type and its name. The variable declaration statement always ends with a semicolon (;).

Example

int count;
char ch;

C also allows you to initialize a variable when you declare it.

int count = 10;
char ch = 'a';
C
Samuel Yang image
Samuel Yang

If you like this tutorial, you can support me

Donate Now