
Basic Declarations and Expressions in C Programming
C is one of the most powerful programming languages ever created, and mastering its fundamentals is essential for anyone stepping into the world of coding. Before you can write complex programs, you must first understand how to declare variables and use expressions to perform calculations and make decisions.
Think of declarations as introducing characters in a story. Before using them, you need to define who they are and what role they play. Expressions, on the other hand, are like conversations—where these characters interact and perform meaningful tasks. In this discussion, we will explore the building blocks of C programming and understand how to use them effectively.
1. Understanding Declarations in C
1.1 What is a Declaration?
When you declare something, you tell the compiler, “Hey, I’m going to use this variable, and here’s what type of data it will hold.” In C, every variable must be declared before it is used.
The basic syntax looks like this:
type variable_name;
For example:
int age; // Declaring an integer variable
float salary; // Declaring a floating-point variable
char grade; // Declaring a character variable
Here’s what’s happening:
age
will store a whole number.salary
will store decimal values.grade
will hold a single character like ‘A’, ‘B’, or ‘C’.
1.2 Variable Initialization – Giving Life to Variables
Declaring a variable is like buying an empty notebook—you need to write something in it before it’s useful. Similarly, in C, you must initialize a variable before using it.
int age = 25;
float salary = 45000.50;
char grade = 'A';
This means:
age
is set to25
.salary
is set to45000.50
.grade
is set to'A'
.
You can also declare multiple variables of the same type together:
int x = 5, y = 10, z = 15;
It’s like setting up multiple characters in your story at once!
1.3 Constants – Values That Never Change
Some values should never be changed once assigned—like the mathematical constant π (Pi). In C, we use constants to store such values.
const float PI = 3.14159; // Constant variable
#define MAX_STUDENTS 100 // Preprocessor macro
const
prevents modification of the value.#define
replacesMAX_STUDENTS
with100
before the program is compiled.
2. Expressions in C – Bringing Code to Life
2.1 What is an Expression?
An expression is a combination of variables, constants, and operators that produces a value. Expressions are how programs perform calculations and make decisions.
For example:
int sum = 10 + 20; // sum = 30
float area = PI * 4 * 4; // area = 50.265
Expressions make programs dynamic—they allow us to compute, compare, and control program flow.
2.2 Types of Expressions
A. Arithmetic Expressions – Doing Math in C
C supports basic mathematical operations:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus – remainder after division)
Example:
int a = 10, b = 3;
int sum = a + b; // sum = 13
int remainder = a % b; // remainder = 1
The modulus operator (%) is particularly useful in checking odd/even numbers:
if (number % 2 == 0)
printf("Even Number");
else
printf("Odd Number");
B. Relational Expressions – Making Comparisons
These expressions compare values and return 1
(true) or 0
(false).
int x = 10, y = 20;
int result = (x < y); // result = 1 (true)
Operators include:
<, >
(less than, greater than)<=, >=
(less than or equal to, greater than or equal to)==, !=
(equal to, not equal to)
C. Logical Expressions – Thinking Like a Computer
Logical expressions help programs make decisions.
Operators:
&&
(AND) – Both conditions must be true||
(OR) – At least one condition must be true!
(NOT) – Reverses the truth value
Example:
int isRainy = 1; // 1 means true
int haveUmbrella = 0; // 0 means false
if (isRainy && !haveUmbrella) {
printf("Stay inside!");
}
Here, since it’s raining and you don’t have an umbrella, the program suggests staying inside.
D. Assignment Expressions – Giving Variables a Job
Assignment expressions store values in variables using =
.
int a;
a = 10; // Assigning 10 to 'a'
To make things faster, we use compound assignment operators:
a += 5; // a = a + 5;
a -= 2; // a = a - 2;
a *= 3; // a = a * 3;
a /= 2; // a = a / 2;
E. Increment and Decrement – Counting Made Easy
Ever counted something quickly by saying “1, 2, 3…”? C has a shortcut for this too!
int a = 5;
a++; // a becomes 6 (post-increment)
++a; // a becomes 7 (pre-increment)
a--; // a becomes 6 (post-decrement)
--a; // a becomes 5 (pre-decrement)
a++
uses the current value first, then increases it.++a
increases first, then uses the new value.
F. The Ternary Operator – A Quick If-Else Alternative
Instead of writing long if-else
statements, use ?:
for quick decisions:
int age = 20;
char* category = (age >= 18) ? "Adult" : "Minor";
printf("%s", category);
Here, if age
is 18
or more, it prints Adult; otherwise, it prints Minor.
3. Operator Precedence – Who Goes First?
When multiple operators appear in an expression, which one is executed first? C follows strict precedence rules.
Example:
int result = 10 + 5 * 2; // result = 20 (Multiplication first)
Use parentheses to control execution order:
int result = (10 + 5) * 2; // result = 30
Conclusion
Declarations and expressions are the foundation of C programming.
- Declarations introduce variables and constants.
- Expressions perform calculations and comparisons.
By mastering these concepts, you will gain the ability to write logical, efficient, and error-free programs. The next step? Start coding! Experiment with these expressions, modify them, and see how they behave. Programming is best learned by doing!
Bikki Singh
- Full Stack Developer
- Exp. 6 Years
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!
Learn and Practice 25 Programming Questions On Basic Declaration and Expressions
Q 1. Declare two integer variables `a` and `b`, assign them values, and print their sum.
Expected Output:
Sum: 30
Q 2. Declare a float variable `f`, assign a value, and print it with two decimal places.
Expected Output:
Float: 12.35
Q 3. Declare two character variables `c1` and `c2`, assign them values, and print their ASCII values.
Expected Output:
ASCII value of A: 65
ASCII value of z: 122
Q 4. Declare an integer variable `num`, assign it a value, and check if it's positive or negative.
Expected Output:
Negative
Q 5. Declare an integer variable `x`, assign it a value, and print whether it is even or odd.
Expected Output:
15 is odd
Q 6. Declare two integer variables `a` and `b`, and print the result of `a` divided by `b` as a floating-point number.
Expected Output:
Result: 3.33
Q 7. Declare a float variable `f1`, assign it a value, and cast it to an integer. Print both the float and integer values.
Expected Output:
Float: 9.99, Integer: 9
Q 8. Declare two integer variables `a` and `b`, swap their values using a temporary variable, and print the result.
Expected Output:
After swap: a = 10, b = 5
Q 9. Declare an integer variable `n`, calculate its square, and print the result.
Expected Output:
Square of 4 is 16
Q 10. Declare three integer variables `a`, `b`, and `c`. Calculate and print the average of these three numbers.
Expected Output:
Average: 20.00
Q 11. Declare a variable `x`, assign it a value, and print its cube.
Expected Output:
Cube of 3 is 27
Q 12. Declare an integer variable `n`, and use it to calculate the remainder when divided by 5.
Expected Output:
Remainder when 14 is divided by 5: 4
Q 13. Declare an integer variable `x`, and use a compound assignment operator to add 10 to it and print the result.
Expected Output:
x after adding 10: 15
Q 14. Declare a float variable `f`, divide it by 2, and print the result.
Expected Output:
Result: 4.00
Q 15. Declare a variable `n` and use the modulus operator to check if it is divisible by 3.
Expected Output:
9 is divisible by 3
Q 16. Declare an integer variable `n`, and print the value of `n` after incrementing it by 1 using the increment operator.
Expected Output:
Value after increment: 8
Q 17. Declare two integer variables `a` and `b`, and print the result of `a` multiplied by `b`.
Expected Output:
Product: 42
Q 18. Declare an integer variable `x`, and use the decrement operator to subtract 1 from it. Print the result.
Expected Output:
Value after decrement: 4
Q 19. Declare a float variable `a`, assign it a value, and print the square root of `a`.
Expected Output:
Square root of 16.00 is 4.00
Q 20. Declare an integer variable `n`, assign it a value, and print its absolute value.
Expected Output:
Absolute value of -8 is 8
Q 21. Declare a float variable `x`, and print the result of `x` raised to the power of 3.
Expected Output:
Cube of 2.00 is 8.00
Q 22. Declare two integer variables `a` and `b`, and print the result of `a` divided by `b` as an integer.
Expected Output:
Result: 4
Q 23. Declare an integer variable `x`, and print the result of `x` multiplied by itself (x^2).
Expected Output:
Square of 4 is 16
Q 24. Declare two integer variables `a` and `b`, and print whether `a` is greater than `b`.
Expected Output:
10 is not greater than 20
Q 25. Declare a variable `x` and use the ternary operator to check if `x` is positive, negative, or zero.
Expected Output:
0 is Zero