A data type tells C what you can store. char holds one
character; int
holds whole numbers. float and double hold decimal
values; double
usually keeps more significant digits. bool holds true or
false; void
means no value. short, long and long long
give integer types with different ranges. signed allows
negatives; unsigned allows only
zero and positive values. Ranges depend on your system. long double
keeps at least as many significant digits as double. Arrays hold
several values; pointers hold addresses. struct groups
values, union
shares storage between alternatives, and enum names
whole-number values. Function types describe inputs and results.
Specialist types cover complex numbers and integers with a chosen number
of bits.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
char characterVariable = 'a';
short shortVariable = 10;
int numberVariable = 205;
unsigned int unsignedVariable = 300;
long longVariable = 100000L;
long long largeVariable = 10000000000LL;
float floatVariable = 101.86f;
double doubleVariable = 123.45;
long double preciseVariable = 123.45L;
bool booleanVariable = true;
printf("%c\n", characterVariable);
printf("%hd %d %u\n", shortVariable, numberVariable, unsignedVariable);
printf("%ld %lld\n", longVariable, largeVariable);
printf("%.2f %.2f\n", floatVariable, doubleVariable);
printf("%.2Lf\n", preciseVariable);
printf("%d\n", booleanVariable);
return 0;
}main(void)
takes no inputs. Each variable gets a type, name and starting value.
'a'
uses single quotes.
<stdbool.h>
supplies bool,
true and false. %d prints these as
1 and 0.
f marks a float. L marks long for a whole
number and long double
for a decimal; LL marks long long.
%c
prints char;
%hd, %d, %u, %ld and %lld print short, int, unsigned int,
long and long long,
respectively.
%.2f
prints float
and double to
two decimal places; %.2Lf
prints long double.
Stored values may be rounded.
Spaces separate values. \n
starts a new line; return 0;
then ends the program successfully.