C Program to Find Size of int, float, double and char
C Program to Find Size of int, float, double and char
A student copies a table claiming every int is four bytes. Measure the actual implementation rather than treating that table as the C language rule.
sizeof returns an object or type size in C bytes as size_t. char is one byte; the sizes of other arithmetic types depend on the implementation.
Before this exercise, review storing values and reading and checking input. The worked program below applies those ideas to this specific problem.
Explain
Explain why measure type sizes uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Measure long and keep the output labeled as an implementation result.
Measure Type Sizes: follow the data
Given data
A student copies a table claiming every int is four bytes. Measure the actual implementation rather than treating that table as the C language rule.
Rule
sizeof returns an object or type size in C bytes as size_t. char is one byte; the sizes of other arithmetic types depend on the implementation.
Observed result
char: 1 int: 4 float: 4 double: 8
Understand the operation
Use %zu to format each sizeof result. Printing it with %d supplies the wrong argument type even if the visible value is small.
The displayed table is an observed result from the verification host. CHAR_BIT measures bits per C byte, while sizeof reports byte counts; neither question should be silently replaced by the other.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
printf("char: %zu\nint: %zu\nfloat: %zu\ndouble: %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));
return 0;
}
Measured example output; non-char sizes can differ
char: 1
int: 4
float: 4
double: 8
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
printf("char: %zu\nint: %zu\nfloat: %zu\ndouble: %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));
return 0;
}
Each sizeof expression produces a size_t value.
The complete format prints one result per line, without allocating an object for the sizeof(type) operands.
Follow the worked calculation
Start with the sample
Each sizeof expression produces a size_t value.
Apply the rule
The complete format prints one result per line, without allocating an object for the sizeof(type) operands.
Check the result
char: 1 int: 4 float: 4 double: 8
char: 1 int: 4 float: 4 double: 8
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
printf("char: %zu\nint: %zu\nfloat: %zu\ndouble: %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));
return 0;
}
Measure long and keep the output labeled as an implementation result.
Worked answer
The worked answer prints sizeof(long). Its sample host reports eight, but other valid C implementations can report a different value. Only the char byte count is fixed here.
C17 · complete program
#include <stdio.h>
int main(void) {
printf("char: %zu\nlong: %zu\n", sizeof(char), sizeof(long));
return 0;
}
Measured example output; long size can differ
char: 1
long: 8
Run in C compiler →Recognize the important boundaries
sizeof a pointer does not report the size of an array it points into. sizeof a structure may include padding.
sizeof normally does not evaluate an expression operand; variably modified types have additional rules. This lesson uses ordinary fixed types.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
sizeof returns an object or type size in C bytes as size_t. char is one byte; the sizes of other arithmetic types depend on the implementation.
Continue with ranges and byte units.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.