Utilizor
Contact Us
HomeC TutorialC Data Types

CC Data Types

Basic data types.

C Data Types

As explained in the Variables chapter, a variable in C must be a specified data type, and you must use a format specifier inside the printf() function to display it:

Data Type Size Description
int 2 or 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
char 1 byte Stores a single character/letter/number, or ASCII values

Examples

Basic Data Types

c example

Demonstrating different types.

#include <stdio.h>

int main() {
  int myNum = 5;             // Integer (whole number)
  float myFloatNum = 5.99;   // Floating point number
  char myLetter = 'D';       // Character

  printf("%d\n", myNum);
  printf("%f\n", myFloatNum);
  printf("%c\n", myLetter);
  return 0;
}

Set Decimal Precision

c example

Formatting float output.

#include <stdio.h>

int main() {
  float myFloatNum = 3.5;
  double myDoubleNum = 19.99;

  printf("%f\n", myFloatNum); // Default shows 6 digits
  printf("%.1f\n", myFloatNum); // Only 1 digit
  printf("%.2lf\n", myDoubleNum);
  return 0;
}

Sizeof Operator

c example

Checking memory size of types.

#include <stdio.h>

int main() {
  int myInt;
  float myFloat;
  double myDouble;
  char myChar;

  printf("%lu\n", sizeof(myInt));
  printf("%lu\n", sizeof(myFloat));
  printf("%lu\n", sizeof(myDouble));
  printf("%lu\n", sizeof(myChar));
  
  return 0;
}

Test Your Knowledge

1. How many bytes is a float usually?

2. Which type holds more precision?