A variable names an object with a type. int count = 5; defines and initializes an integer object; count = 7; changes its value, not its type. const int limit = 10; makes that object nonmodifiable through its name. char stores a character-sized integer; float and double represent floating-point values approximately. "cat" is a string literal; 'c' is a character constant.
#include <stdio.h>
int main(void) {
int count = 5;
double average = (double)count / 2;
printf("count=%d average=%.1f\n", count, average);
return 0;
}
Output: count=5 average=2.5. The cast converts count before division. With two integer operands, 5 / 2 is 2; division by zero is undefined. +, -, *, /, and % perform arithmetic; += updates a value. x++ produces the old value and increments; ++x produces the new value. Avoid combining multiple changes to the same variable in one expression.
if (count > 0) { ... } else { ... } chooses a branch. Comparisons include ==, !=, <, and >=; assignment is =. Zero is false, nonzero true. && requires both conditions and || either; both short-circuit, so p != NULL && *p == 1 tests p first. ! negates. condition ? a : b selects one expression.
for (int i = 0; i < 3; i++) runs the body for 0, 1, 2. while checks before the body; do { ... } while (condition); checks afterward, so it runs at least once. Use braces to make the controlled statements clear.
Reading expressions and control flow
A declaration reserves an object and specifies its type; initialization gives its first value. An uninitialized automatic int must not be read. const prevents modification through that qualified lvalue; it does not turn an object into a macro or make other aliases magically safe. A character constant like 'A' has type int in C; a string literal contains an additional terminating null character. Floating-point values are approximations, so equality after arithmetic can be surprising.
Operator precedence affects grouping: 2 + 3 * 4 means 2 + (3 * 4). Parentheses clarify intent. % is integer remainder, not a general floating-point operator; division or remainder by zero is undefined. Signed integer overflow is also undefined. i++ is convenient as a statement; when its value matters, it yields the value before incrementing. Avoid expressions that both modify and access the same variable without the required sequencing.
In a for loop the initializer runs once, the condition is checked before each iteration, and the update runs after the body. For example, for (int i = 0; i < 3; ++i) prints three values if the body prints i. A while loop may run zero times; a do loop executes its body before its first condition check. break exits the nearest loop or switch; continue advances the nearest loop to its next iteration. Always ensure a loop’s progress and bounds are clear. Short-circuit evaluation allows a guard to prevent an invalid operation, but the pointer must still refer to a live object if dereferenced.