Sizes of the types
On one line, separated by spaces, print the size in bytes of the types char, short, int, float and double, using the sizeof operator.
1 2 4 4 8A constant is a fixed value written directly in the code; a variable is a name associated with a memory location whose content can change. The type chosen for each determines how much space it occupies, what values it can hold, and what operations can be applied to it.
U and L suffixessizeof operatorLiteral constants are fixed values that cannot be changed during execution. Their type is determined automatically by the compiler, based on the value and the syntax used.
| Base | Writing convention | Examples |
|---|---|---|
| Decimal (10) | the first digit is not 0 | 23, -555L, 3276L |
| Octal (8) | the first digit is 0; the digits 8 and 9 are illegal | 067, -067, 067u |
| Hexadecimal (16) | prefix 0x or 0X | 0x1F, 0xFFu, 0xAB3L |
int x = 010; does not mean ten, it means eight - the 0 prefix
triggers interpretation in base 8. This is a frequent mistake when writing calendar dates or
codes with insignificant leading zeros.| Situation | Assigned type | Example |
|---|---|---|
the value fits in the int range | int | 15, 23 |
exceeds int, fits in long | long | 99999 |
exceeds long | double | 98765432100 |
The default type can be forced with suffixes: U/u for
unsigned, L/l for long, combined as
UL.
| Sequence | Meaning | ASCII code |
|---|---|---|
'\n' | newline | 10 |
'\t' | horizontal tab | 9 |
'\r' | carriage return | 13 |
'\0' | null character - marks the end of a string | 0 |
'\\' | backslash | 92 |
'\'' | apostrophe | 39 |
'A' is a character constant and
occupies 1 byte. "A" is a string and occupies 2 bytes: the letter
plus the '\0' terminator. Confusing the two produces errors that are hard to
track down.| Type | Typical size | Range of values | printf specifier |
|---|---|---|---|
char | 1 byte | −128 ... 127 | %c / %d |
unsigned char | 1 byte | 0 ... 255 | %u |
short | 2 bytes | −32,768 ... 32,767 | %hd |
int | 4 bytes | −2,147,483,648 ... 2,147,483,647 | %d |
unsigned int | 4 bytes | 0 ... 4,294,967,295 | %u |
long long | 8 bytes | ±9.2 · 10¹⁸ | %lld |
float | 4 bytes | ±3.4 · 10³⁸, ~7 significant digits | %f |
double | 8 bytes | ±1.7 · 10³⁰⁸, ~15 significant digits | %lf |
bool (C++) | 1 byte | true / false | - |
char ≤ short ≤ int ≤ long). On an 8-bit
microcontroller, int is 2 bytes; on an ordinary PC, 4. That is why you always
check with sizeof.Declaring a variable reserves space in memory and associates it with a name and a type:
type name; // simple declaration, undefined value type name = value; // declaration with initialization type n1, n2, n3; // several variables of the same type const type name = value; // a value that can no longer be changed
| Class | Where it is visible | How long it lives | Default initial value |
|---|---|---|---|
auto (local) | in the block where it is declared | until the block exits | undefined |
local static | in the block where it is declared | the entire program execution | 0 |
| global | throughout the file, after the declaration | the entire program execution | 0 |
extern | in other source files | the entire program execution | 0 |
register | in the block where it is declared | until the block exits | undefined |
static variable is only
visible in its own function, but is not destroyed on exit: on the next call it finds its
value again. This is the standard mechanism for counters that must remember their state
between calls.Watch step by step how an ordinary local variable behaves compared to one
declared static, over three successive calls of the same function.
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("Type\tBytes\tMin\t\tMax\n");
printf("----------------------------------------\n");
printf("char\t%d\t%d\t\t%d\n", (int)sizeof(char), CHAR_MIN, CHAR_MAX);
printf("short\t%d\t%d\t\t%d\n", (int)sizeof(short), SHRT_MIN, SHRT_MAX);
printf("int\t%d\t%d\t%d\n", (int)sizeof(int), INT_MIN, INT_MAX);
printf("long\t%d\t%ld\t%ld\n", (int)sizeof(long), LONG_MIN, LONG_MAX);
printf("float\t%d\n", (int)sizeof(float));
printf("double\t%d\n", (int)sizeof(double));
return 0;
}
#include <stdio.h>
int main(void)
{
int decimal = 100; // base 10
int octal = 0144; // base 8 -> also 100
int hexadecimal = 0x64; // base 16 -> also 100
printf("%d %d %d\n", decimal, octal, hexadecimal); // 100 100 100
// the same value shown in different bases
printf("decimal: %d octal: %o hex: %X\n", 255, 255, 255);
// the leading-zero trap
int wrong = 010;
printf("010 means %d, not 10!\n", wrong); // 8
// character vs string
char c = 'A';
char s[] = "A";
printf("sizeof('A') in C = %d, sizeof(\"A\") = %d\n", (int)sizeof(c), (int)sizeof(s));
return 0;
}
#include <stdio.h>
int main(void)
{
short small = 32767; // the maximum value for short
printf("before: %d\n", small);
small = small + 1; // overflow!
printf("after +1: %d\n", small); // -32768, drops to the minimum value
unsigned char u = 255;
u = u + 1;
printf("unsigned char 255+1 = %u\n", u); // 0, wraps back to the start
return 0;
}
The sizes of types and their overflow behavior are not learned from tables, but by seeing them. Run the programs below and change the values to see exactly where each type breaks.
This is where you write the code. The ✓ Check button runs your program on several data sets and shows you right away where the result differs. At the end you submit the track you chose, and the instructor grades it automatically.
Every exercise has the program already started, and the comments tell you what is left to add. Write the code, press ✓ Check and read what each failed test says. If you get stuck, open the hints one at a time. You may also print messages for the user (such as Enter n:); the test looks at what you print at the end.
On one line, separated by spaces, print the size in bytes of the types char, short, int, float and double, using the sizeof operator.
1 2 4 4 8Declare the constant const double PI = 3.14159265;. Read the radius r of a circle and print its circumference (2·PI·r) with two decimals.
16.28Read a non-negative integer n. Print it on one line in base 10, base 8 and base 16 (lower-case letters).
200200 310 c8Read a character c. On one line, print its ASCII code and the character that follows it in the ASCII table.
A65 BThe function count() must return how many times it has been called so far. main calls it three times and prints the results. Complete the function using a static variable. (Functions are covered in detail in Lab 10; here it is enough to fill in the body of the one in the skeleton.)
1 2 3Problems start from an empty main. The output format must be followed exactly (spaces and newlines do not matter unless the statement says otherwise), so do not print prompts. The visible tests cover only the simple cases; grading also runs hidden tests chosen to catch the edge cases.
Read two integers a and b, each between -32768 and 32767. On one line, print their sum computed in an int, then the same sum stored in a short variable. Example: for 30000 30000 print 60000 -5536.
30000 3000060000 -5536A colour is described by three components r g b, each between 0 and 255. Its code is the number r · 65536 + g · 256 + b. Read the components and print, on one line, the code in decimal, then in the form used on the web: #RRGGBB, with upper-case hexadecimal digits. Example: for 255 128 0 print 16744448 #FF8000.
255 128 016744448 #FF8000Print exactly the three lines below (between Name: and "Ana" there is a TAB character):
Name: "Ana" Path: C:\temp\new Percent: 100%
Spacing matters here: the output is compared character by character (only trailing spaces are ignored).
Name: "Ana"
Path: C:\temp\new
Percent: 100%Read a real number x. On one line print: the integer part obtained by converting to int, the value rounded to the nearest integer (halves are rounded away from zero) and x with two decimals.
2.72 3 2.70short variable with the maximum value and add 1; explain the result.sizeof('A') in C with sizeof('A') in C++ - the results differ, explain why.static counter and call it five times; print the value on each call.printf and observe the effect.<limits.h>, the maximum value of the int type on your
computer: start from 1 and double the value until it becomes negative, then adjust. Compare the
result with INT_MAX. Repeat for unsigned int.