C data types determine what kind of value an object can store and how that value is interpreted. Common beginner types include char, signed and unsigned integer types, float, double and long double.
| Declaration | scanf() | printf() |
|---|---|---|
char c; | scanf(" %c", &c); | printf("%c", c); |
int i; | scanf("%d", &i); | printf("%d", i); |
unsigned int u; | scanf("%u", &u); | printf("%u", u); |
short s; | scanf("%hd", &s); | printf("%hd", s); |
unsigned short us; | scanf("%hu", &us); | printf("%hu", us); |
long l; | scanf("%ld", &l); | printf("%ld", l); |
unsigned long ul; | scanf("%lu", &ul); | printf("%lu", ul); |
float f; | scanf("%f", &f); | printf("%f", f); |
double d; | scanf("%lf", &d); | printf("%f", d); |
long double ld; | scanf("%Lf", &ld); | printf("%Lf", ld); |
Important: scanf() distinguishes %f for float * from %lf for double *. With printf(), a float argument is promoted to double, so %f is used for both float and double values.
#include <stdio.h>
int main(void) {
int count = 10;
float price = 5.25f;
double distance = 10.123456;
char grade = 'A';
printf("Integer: %d\n", count);
printf("Float: %.2f\n", price);
printf("Double: %.6f\n", distance);
printf("Character: %c\n", grade);
return 0;
}
Output
Integer: 10
Float: 5.25
Double: 10.123456
Character: A
printf("char: %zu bytes\n", sizeof(char));
printf("int: %zu bytes\n", sizeof(int));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
sizeof returns a value of type size_t, so %zu is the matching printf() conversion. Except for char, exact byte sizes are implementation-dependent, so check them on the compiler/platform you are using.
const int max_items = 10;
const double tax_rate = 0.18;
See constants and variables in C for more examples.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.