Summary of "Learn to program with c - Part 4 - Types"
Purpose and repository
- This episode covers C data types: an overview, ranges, signed vs unsigned, floating-point limits, and printf format specifiers. Variables are promised for the next episode.
- Example code and exercise answers are provided in the presenter’s public Git repository (c_tutorial). You can clone it and run the example programs.
Getting the example code
-
Clone the repository:
git clone <repository URL> # e.g. github.com/<username>/c_tutorial -
Change into the episode directory:
cd c_tutorial/episode_04/types -
Update later with:
git pull -
Build the example programs (a simple Makefile is included):
make -
Run an example and page the output:
./hello_types | less
printf and format strings — how they work
printftakes a format string (which can include format specifiers/placeholders) followed by arguments that fill those placeholders.- Common format specifiers introduced:
%d— decimal integer%f— floating-point (double)%s— string%c— character
Examples:
printf("%d\n", 5); // prints integer 5
printf("%f\n", 3.14); // prints floating-point number
printf("%s %s\n", "a", "b"); // prints two strings separated by space
printf("%c\n", 'a'); // prints character 'a'
printf("%c%c%c\n", 97,98,99); // prints "abc" because 97 == 'a' in ASCII
-
Multiple placeholders are matched in order:
printf("%f %f %f\n", x, y, z); -
Controlling floating precision: use a precision modifier after
%, e.g."%.2f"prints two decimal places:printf("%.2f\n", 3.14159); // output: 3.14 -
Order matters: placeholders are matched in order with the subsequent arguments.
- Exercise suggestion: try printing a floating-point value using the integer specifier (e.g.,
printf("%d\n", 3.14)) and observe the undefined/implementation-dependent result.
C primitive types and important notes
char
- Usually 8 bits.
signed char: typically -128 .. 127.unsigned char: 0 .. 255.- Commonly used to store ASCII characters; ASCII codes map numeric values to characters (e.g., 97 ->
'a'). - Print with
%c(or as integer with%d).
short
- Minimum required size: 16 bits.
- Typical 16-bit range: -32,768 .. 32,767.
unsigned short: 0 .. 65,535.- Today rarely used except in embedded or space-constrained code.
int
- The usual choice for integer arithmetic.
- Size depends on platform/compiler. On many modern 64-bit systems,
intis 32 bits:- signed
int(typical 32-bit): -2,147,483,648 .. 2,147,483,647 unsigned int: 0 .. 4,294,967,295
- signed
long and long long
long(orlong int) is larger thaninton some platforms;unsigned longgives a larger positive range.long longis larger still (use when you need more thanlong).- Exact sizes are platform-dependent; the tutorial includes a program that prints the type limits on your system.
Floating-point types
float(typically 32-bit IEEE-754):- Approximate maximum: ±3.402823e+38
- Smallest positive normalized: ~1.175494e-38
double(typically 64-bit IEEE-754):- Approximate maximum: ±1.797693e+308
- Smallest positive normalized: ~2.22507e-308
long double— can be larger (platform dependent).- Important: floating-point types have limited precision and cannot exactly represent most rational numbers; precision loss is normal.
_Bool / bool
- C’s boolean type is
_Bool; includingstdbool.hgives the aliasbooland the valuestrue/false. - Historically, C used
intfor booleans where0 == falseand any non-zero ==true;boolimproves readability.
Other types mentioned (not covered in detail)
complex.h— complex number type (recent addition)void— special/no-value typeenum— enumerationsstruct— user-defined compound types (important for combining primitives; e.g., a coordinate type with twoints). Structs will be covered separately.
Example programs and tools in the repo
hello_types.c:- Demonstrates printing different types with
printfand shows limits for each type using macros fromlimits.h(e.g.,CHAR_MIN,CHAR_MAX). - Demonstrates
printfmodifiers for precision and padding.
- Demonstrates printing different types with
exercise.c:- Contains exercise solutions (including the earlier “print 5 without writing 5” example).
Makefile:- Simple build script showing how to compile the provided programs (and how
makeonly rebuilds changed files).
- Simple build script showing how to compile the provided programs (and how
- Recommendation: compile and read the output of
hello_typescarefully to see how types behave on your system.
Key takeaways / lessons
- Know the data type you choose: size, signed vs unsigned, and range matter.
- Always match
printfformat specifiers to the actual argument types; mismatches are incorrect and can produce unexpected results. - Floating-point numbers have limited precision — choose
float/double/long doubleappropriately for the required range and precision. - Use
stdbool.hfor booleans to make code clearer. - Use provided examples and the included programs to inspect how types behave on your machine (type sizes and macro limits may differ across systems).
- Variables (how to declare and use them) are the logical next topic — types by themselves are not useful until you store values in variables (covered in the next episode).
Practical exercises suggested
- Print a floating-point number using the integer
printfspecifier (%d) in a C program and observe the result. Try variations and examine behavior. - Compile and run
hello_typesand read through its output carefully to understand type limits andprintfformatting.
Speakers / sources featured
- Presenter/author of the tutorial (the tutorial host).
- Git repository for examples (presenter’s
c_tutorialrepository on GitHub). - Standard C headers referenced:
limits.h(for type limits),stdbool.h(forbool),complex.h(complex type). - The ASCII man page (
man ascii) used to show ASCII codes to character mapping.
Category
Educational
Share this summary
Is the summary off?
If you think the summary is inaccurate, you can reprocess it with the latest model.