C Data Types
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Storing a decimal value in an
int, such asint price = 9.99;, which silently drops the fraction. - Writing
char c = "A";with double quotes, which is a string and not a single character; useA. - Assuming
intis always 4 bytes, when its size depends on the platform.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers