Programming means naming things and assigning meaning to data. In C, this begins with variables, their types, and the rules for naming them.
Once you've written your first printf("Hello, World!\n")
, you've already encountered your first function and literal string. The next logical step is understanding how we name data and what kinds of data we can represent.
Identifiers are the names we give to things in code: variables, functions, constants. But not all names are allowed — they must follow specific rules: they must begin with a letter or underscore, may not clash with reserved keywords, and are case-sensitive.
Types in C describe what kind of data is stored and how many bytes are allocated. Some types are exact (int
, char
, float
) while others vary depending on the system (long
, short
). Understanding types is critical not just for memory layout, but for how operations like +
, *
, or ==
behave.
A variable is a named storage location — a binding between an identifier and a value of a certain type. Declaring variables and using them correctly is a foundational concept.
Variables in C are uninitialized by default — using them before assigning a value leads to undefined behavior.
An identifier is a name, which referrs to an object. Each identifier comes with a type,which is kind of a set of rules about the object that identifier references, defining how that data is stored, interpreted, which operations are legal and should be implemented by specific asm
instructions by the compiler.
You may use the same +
operator in you code for int
and float
. But trying to write down the bit-patterns of two separate int
and float
values and the attempt to write down the truth-table will show to you, that even though in high-level-code we mean the same operation - adding them together - the circuits for them need to be implemented completely different. Thus making it neccessary to use different instructions for adding float
and int
.
- Declare Variables: Create variables of type
int
,char
,float
, anddouble
. Assign values to them. - Print Variables: Use
printf
with the appropriate format specifiers (%d
,%c
,%f
,%lf
) to print their values. Just run the code for now and checkout the exercise Format Specifiers.
printf("int: %d", intvar);
printf("char: %c", charvar);
printf("float: %f", floatvar);
printf("double: %lf", doublevar);
- Naming Practice: Try creating a variable with an illegal name (e.g.,
int 2cool;
) and observe the diagnostic message during compilation. - Arithmetic: Declare two
int
variables and compute their sum, product, and remainder. Print the results. - Type Casting: Divide an
int
by anotherint
and cast the result tofloat
.
float res1 = (float)(a / b);
float res2 = (float) a / b ;
printf("Result 1: %f", res1);
printf("Result 2: %f", res2);
- Storage Size: Inspect the sizes of
int
,char
,float
, anddouble
on your system by usingsizeof()
?
printf("Size of int: %d", sizeof(a));
- What are valid and invalid identifier names in C, and in which stage of the compilation, are these names checked?
a-z, A-z "_" 0-9 <- after the first character: not allowed: c names like int, count
- What happens if you use a variable before assigning it a value?
ERROR Message Won't compile
- Why does dividing two integers give an integer result?
Operations between two values of the same type stay in that type, unless you explicitly cast.
Use man printf
to explore format specifiers. Try writing small programs that experiment with assigning values and printing results. Don’t just read — test! Seeing how types behave in practice is much more instructive than memorizing rules.