← Back to C Course | Chapter 1: Introduction & Basics | Lesson 9 of 21

C Data Types

Data types tell C what kind of thing a value is, like whole numbers, decimals, or single letters. It is like having different-shaped boxes for different items.
Syntax
c
data_type variable_name = value;

// e.g. int, char, float, double

Integer Type

int stores whole numbers with no fractional part, typically using 4 bytes of memory on modern systems, which gives it a range of roughly -2 billion to +2 billion -- more than enough for counters, indexes, and most everyday integer math.

Example: Integer Type

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 100
	int count = 100;
	// Print formatted text to the screen
	printf("%d, size: %zu bytes", count, sizeof(count));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Character Type

char stores a single character in exactly 1 byte, but internally it's really just a small integer representing that character's position in the ASCII table -- which is why you can do arithmetic directly on char values in C.

Example: Character Type

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare grade and set it to 'A'
	char grade = 'A';
	// Print formatted text to the screen
	printf("%c is ASCII %d", grade, grade);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating-Point Type

float stores decimal numbers using 4 bytes and about 6-7 significant digits of precision, which is enough for many everyday calculations but can introduce small rounding errors in anything requiring exact decimal accuracy, like currency.

Example: Floating-Point Type

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare price and set it to 19.99f
	float price = 19.99f;
	// Print formatted text to the screen
	printf("%.2f", price);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Double Type

double uses 8 bytes -- twice the space of float -- to store roughly 15-16 significant digits of precision, making it the default choice in C for decimal math where accuracy matters more than saving memory.

Example: Double Type

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare pi and set it to 3.14159265358979
	double pi = 3.14159265358979;
	// Print formatted text to the screen
	printf("%.10f", pi);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Void Type

void represents the absence of a value and is most often used as a function's return type to signal that the function performs an action but doesn't hand any data back to its caller -- distinct from returning 0 or an empty string.

Example: Void Type

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet() {
	// Print formatted text to the screen
	printf("Hello from a void function");
}
// Program execution starts in main()
int main() {
	greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Storing a decimal value in an int, such as int price = 9.99;, which silently drops the fraction.
  2. Writing char c = "A"; with double quotes, which is a string and not a single character; use A.
  3. Assuming int is always 4 bytes, when its size depends on the platform.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.