Data Types in C

Data Types in C. Data Types in C Tutorial. Learn Data Types in C.
Data Types in C

What is Data Type in C ?

In C, a data type is a classification that specifies the type of data that a variable can hold. It also determines the size of the variable, the range of values that it can hold, and the operations that can be performed on it.

Why Data Types are used ?

Data Types are used to define the type of data that a variable can hold. This helps the compiler to allocate memory for the variable and to check for type compatibility during operations.

Data Types in C:

Given below are the different types of Data Types in C:

Data TypeInformationByte SizeValue RangeSyntaxExampleFormat Specifier
intRepresents a standard integer.4 bytes-2,147,483,648 to 2,147,483,647int variableName = 100;int age = 25;%d
charRepresents a single character.1 byte-128 to 127 or 0 to 255char variableName = ‘a’;char firstInitial = ‘J’;%c
shortRepresents an integer with a smaller range than int.2 bytes-32,768 to 32,767short variableName = 10;short temperature = -20;%d
longRepresents an integer with a larger range than int.4 bytes-2,147,483,648 to 2,147,483,647long variableName = 1000000L;long population = 123456789L;%ld
floatRepresents a floating-point number with single precision.4 bytes1.2E-38 to 3.4E+38float variableName = 3.14159f;float pi = 3.14f;%f
doubleRepresents a floating-point number with double precision.8 bytes2.3E-308 to 1.7E+308double variableName = 3.14159;double pi = 3.141592653589793;%lf
boolRepresents a boolean value of true or false.1 bytetrue or falsebool variableName = true;bool isCorrect = true;%d or %i

Code:

Lets see how we can declare all these variables:

#include <stdio.h>

int main() {
  int isCorrect = 1;
  char firstInitial = 'J';
  short temperature = -20;
  int age = 25;
  long population = 123456789L;
  float pi = 3.14f;
  double e = 2.71828;

  printf("bool: %d\n", isCorrect);
  printf("char: %c\n", firstInitial);
  printf("short: %hd\n", temperature);
  printf("int: %d\n", age);
  printf("long: %ld\n", population);
  printf("float: %f\n", pi);
  printf("double: %lf\n", e);

  return 0;
}

Output:

bool: 1
char: J
short: -20
int: 25
long: 123456789
float: 3.140000
double: 2.718280

Signed and Unsigned Modifiers in C

Signed Modifier:

In C, the term “signed” is used as a modifier for integer data types to indicate that the data type can represent both positive and negative values. By default, integer data types are signed in C, which means that if you do not specify signed or unsigned, the compiler will assume that the data type is signed.

For example, a signed int can represent values between -2,147,483,648 and 2,147,483,647. Similarly, a signed short can represent values between -32,768 and 32,767.

Signed data types are often used for variables that represent quantities that can take on negative values, such as temperature, displacement, or velocity.

Unsigned Modifier:

In C, the term “unsigned” is used as a modifier for integer data types to indicate that the data type can only represent non-negative values. Using unsigned data types can be useful when you need to represent only positive values, and you want to increase the maximum value that can be stored.

For example, an unsigned int can represent values between 0 and 4,294,967,295. Similarly, an unsigned short can represent values between 0 and 65,535.

Unsigned data types are often used for variables that represent quantities that cannot take on negative values, such as lengths, counts, or indices.

Given below are the different types of Signed and Unsigned Data Types in C:

Data TypeInformationByte SizeValue RangeSyntaxExampleFormat Specifier
signed charSigned character type1-128 to 127signed charsigned chat x = -5;%c
or
%hhd
unsigned charUnsigned character type10 to 255unsigned charunsigned char y = 200;%c
0r
%hhu
signed short intSigned short integer type2-32,768 to 32,767signed short int
or
signed short
or
short int
or
short
signed short int x = -100;%hd
unsigned short intUnsigned short integer type20 to 65,535unsigned short int
or
unsigned short
unsigned short int y = 5000;%hu
signed intSigned integer type4-2,147,483,648 to 2,147,483,647signed int
or
signed
or
int
signed int x = -1000;%d
unsigned intUnsigned integer type40 to 4,294,967,295unsigned int
or
unsigned
unsigned int y = 40000;%u
signed long intSigned long integer type4 or 8-2,147,483,648 to 2,147,483,647
or
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int
or
signed long
or
long int
or
long
signed long int x = -100000;%ld
unsigned long intUnsigned long integer type4 or 80 to 4,294,967,295
or
0 to 18,446,744,073,709,551,615
unsigned long int
or
unsigned long
unsigned long int y = 4000000000;%lu
signed long long intSigned long long integer type8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807signed long long int
or
signed long long
or
long long int
or
long long
signed long long int x = -1000000000;%lld
unsigned long long intUnsigned long long integer type80 to 18,446,744,073,709,551,615unsigned long long int
or
unsigned long long
unsigned long long int y = 10000000000;%llu

Again, lets see how we can declare all these variables:

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

int main() {
  // Signed Types
  bool isCorrect = true;
  char firstInitial = 'J';
  signed char temperature = -20;
  short int age = -25;
  signed int score = -100;
  int32_t population = -123456789L; // int32_t is 32 bit signed int
  long long int bankBalance = -123456789012345LL;
  float pi = -3.14f;
  double e = -2.71828;

  // Unsigned Types
  unsigned char asciiCode = 65;
  unsigned short int numberOfStudents = 100;
  unsigned int distance = 2500U;
  uint32_t websiteHits = 1234567890UL; // uint32_t is 32 bit unsigned int
  unsigned long long int largeNumber = 12345678901234567890ULL;

  printf("bool: %d\n", isCorrect);
  printf("char: %c\n", firstInitial);
  printf("signed char: %hhd\n", temperature);
  printf("short int: %hd\n", age);
  printf("signed int: %d\n", score);
  printf("int32_t: %ld\n", population);
  printf("long long int: %lld\n", bankBalance);
  printf("float: %f\n", pi);
  printf("double: %lf\n", e);
  printf("unsigned char: %hhu\n", asciiCode);
  printf("unsigned short int: %hu\n", numberOfStudents);
  printf("unsigned int: %u\n", distance);
  printf("uint32_t: %lu\n", websiteHits);
  printf("unsigned long long int: %llu\n", largeNumber);

  return 0;
}

Output:

bool: 1
char: J
signed char: -20
short int: -25
signed int: -100
int32_t: -123456789
long long int: -123456789012345
float: -3.140000
double: -2.718280
unsigned char: 65
unsigned short int: 100
unsigned int: 2500
uint32_t: 1234567890
unsigned long long int: 12345678901234567890

Rules while declaring/using Data Types:

  1. Choose the appropriate data type for the specific data you are working with. For example, use a bool data type for true/false values, char for single characters, int for integers, float for decimal numbers with single precision, double for decimal numbers with double precision, and so on.
  2. Make sure the data type you choose can accommodate the expected range of values for the variable. For example, if you need to store large numbers, you may need to use a long or long long data type instead of an int.
  3. Use the sizeof operator to determine the size of a data type in bytes. This can help you ensure that you have allocated enough memory for a variable.
  4. Be aware of the limitations of signed and unsigned data types, and use them appropriately. For example, an unsigned data type cannot store negative values, so be careful when using them for calculations that may result in negative values.
  5. Use the appropriate format specifier when printing variables to the console or writing to a file. This will ensure that the output is formatted correctly and can be read by the intended audience.
  6. Avoid using variables with the same name as built-in functions or keywords in C. This can cause conflicts and unexpected behavior in your code.
  7. Declare variables before using them, and initialize them to a default value if possible. This will prevent errors and help ensure that your program behaves predictably.
  8. Use const to declare variables that should not be modified during program execution. This can help prevent accidental changes to important values.
  9. Avoid using global variables whenever possible. They can make it difficult to reason about program behavior and can lead to unexpected interactions between different parts of your code.

More: