C is strict about what it holds in memory. It doesn’t guess. You have to tell it exactly what kind of data you are storing. This isn’t just pedantry. It affects how your program runs, how much memory it uses, and whether your calculations come out right or garbage.
The Standard Variable Types
There are three standard variable types in C.
Integer: int
This is a 4-byte integer. It handles whole numbers.
Floating point: float
This is a 4-byte floating point value. It handles decimals.
Character: char
This is a 1-byte single character. Think “a” or “3”. A string in C is just an array of these characters.
Then there are derivative types. You use them when the standard three don’t cut it.
double: An 8-byte floating point value. More precision.short: A 2-byte integer. Less memory, less range.unsigned shortorunsigned int: These handle positive integers only. No sign bit. If you need to track population counts or packet sizes, this is often the right choice.
Operators and Operator Precedence
The operators in C look familiar.
+addition-subtraction/division*multiplication%mod
But watch out for division. The / operator performs integer division if both operands are integers. It performs floating point division otherwise.
Look at this code:
It prints a floating point value because a is declared as type float. But a will be 3.0. Why? Because the code performed integer division first. 10 divided by 3 is 3. The result is 3. Then that 3 gets converted to 3.0 when stored in a. The precision was lost before it even hit the variable.
Operator precedence in C follows standard rules. Division and multiplication happen before addition and subtraction.
The result of 5+3*4 is 17. Not 32. The * operator has higher precedence than +. You can override this with parentheses. (5+3)*4 is 32. The 5+3 is evaluated first.
Precedence gets messy later. Once pointers enter the chat, it becomes complicated. But for now, remember: multiply and divide before you add and subtract.
Typecasting
C allows you to convert types on the fly. You do this often with pointers. It also happens automatically during assignment for certain types.
In the example above, the integer result of 10/3 was automatically converted to a float. But what if you wanted the decimal?
You do typecasting by placing the type name in parentheses before the value.
Replace a=10/3; with a=(float)10/3;.
Now a is 3.33333. The 10 is converted to a floating point value before the division. The division happens in floating point land. The result keeps its decimal places.
Typedef
You can declare named, user-defined types in C with the typedef statement. This is common in C code.
This lets you declare Boolean types. C doesn’t have a native bool type in older standards, so this is a workaround.
If you dislike the word “float”, you can say:
Then later:
Place typedef statements anywhere in the program. Just make sure they come before their first use.
Structures
Structures in C group variables into a package.
Whenever you want to declare structures of type rec, you must say struct rec. This is easy to forget. You will get compiler errors if you leave out the struct.
You can compress the code:
Here, the type declaration and the variable r are declared in the same statement.
Or use typedef for the structure name. If you hate typing struct rec r every time:
Then declare records by saying:
Access fields using a period. r.a = 5;.
Arrays
You declare arrays by inserting a size after the normal declaration.
Arrays are contiguous blocks of memory. Accessing an index outside the bounds is undefined behavior. It might crash. It might corrupt data. It might appear to work until it doesn’t.
Incrementing
C has shortcuts for common operations.
i = i + 1;becomesi++;i = i - 1;becomesi--;i = i + 3;becomesi += 3;i = i * j;becomesi *= j;
These aren’t just syntactic sugar. They often compile to more efficient machine code. Use them.
C Errors to Avoid
The / operator with two integers is a trap. It truncates the decimal part. Always check your types before dividing. If you need precision, cast one operand to float or double first.
Think about it whenever you use it. Otherwise, your numbers will be wrong. And debugging integer division issues is painful.
Try This
- Experiment with typecasting. Try
int,char,float. See what happens when you mix them. - Create an array of records. Write code to sort that array on one integer field. It teaches you about pointers, memory layout, and control flow.
The details matter in C. Miss one type, and the whole program breaks. Get it right, and it runs fast.


























