Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
Understanding variables, data types, and operators is
fundamental to mastering C programming. These elements form the core logic
of any program—whether it’s a calculator, an operating system kernel, or an
embedded controller.
🧠 What Are Variables?
A variable in C is a named space in memory used to
store data that can change during program execution. Think of variables as
containers to hold values that your program uses and manipulates.
✅ Syntax:
data_type
variable_name;
✅ Example:
int
age = 25;
float
salary = 50450.75;
char
grade = 'A';
🧾 Naming Rules for
Variables
Rule |
Description |
Must begin with a
letter (A-Z or a-z) or underscore _ |
|
Subsequent characters can be letters, digits, or underscores |
|
Case-sensitive
(value, Value, and VALUE are different) |
|
No reserved keywords (e.g., int, return) |
❌ Invalid Examples:
int
1num; // starts with a digit
float
float; // reserved keyword
char
my-name; // hyphen not allowed
🧩 Variable Types Based on
Scope and Storage
C supports different storage classes that determine a
variable’s lifetime, visibility, and default value.
Storage Class |
Scope |
Lifetime |
Default Value |
auto |
Local |
Function |
Garbage value |
static |
Local/Global |
Entire
Program |
0 |
extern |
Global |
Declared Elsewhere |
— |
register |
Local |
Function |
Garbage value |
🔢 C Data Types
Data types specify the type of data a variable can store.
They are mainly divided into:
✅ Basic Data Types
Data Type |
Description |
Memory |
Format Specifier |
int |
Integer values |
2/4 B |
%d |
float |
Decimal
numbers |
4 B |
%f |
double |
Double-precision |
8 B |
%lf |
char |
Character |
1 B |
%c |
✅ Example:
int
age = 30;
float
pi = 3.14;
char
letter = 'B';
✅ Derived Data Types
Type |
Description |
array |
Collection of elements |
pointer |
Stores memory
address |
function |
Block of reusable code |
✅ User-Defined Data Types
Type |
Description |
struct |
Collection of
different data types |
union |
Shared memory
for multiple fields |
enum |
Set of named integer
constants |
typedef |
Alias for
existing data types |
📋 Table: Size of Data
Types (Typical)
Data Type |
Size (bytes) |
Range |
int |
4 |
-2,147,483,648 to
2,147,483,647 |
float |
4 |
±3.4e-38 to ±3.4e+38 |
double |
8 |
±1.7e-308 to ±1.7e+308 |
char |
1 |
-128 to 127
or 0 to 255 |
🎯 Constants
Constants are values that cannot be changed during the
execution of a program.
✅ Using const keyword:
const
float PI = 3.14159;
✅ Using #define macro:
#define
GRAVITY 9.8
🎛️ Operators in C
C provides a rich set of operators that allow for
arithmetic, comparisons, logic checks, and more.
🔢 Arithmetic Operators
Used for mathematical operations.
Operator |
Meaning |
Example |
+ |
Addition |
a + b |
- |
Subtraction |
a - b |
* |
Multiplication |
a * b |
/ |
Division |
a / b |
% |
Modulus |
a % b |
int
a = 10, b = 3;
printf("%d",
a % b); // Output: 1
👁️🗨️
Relational Operators
Used to compare values.
Operator |
Meaning |
Example |
== |
Equal |
a == b |
!= |
Not equal |
a != b |
> |
Greater than |
a > b |
< |
Less than |
a < b |
>= |
Greater or equal |
a >= b |
<= |
Less or equal |
a <= b |
🔍 Logical Operators
Combine or invert conditions.
Operator |
Meaning |
Example |
&& |
AND |
a > 0 && b
> 0 |
` |
` |
|
! |
NOT |
!a |
🧠 Assignment Operators
Used to assign values.
Operator |
Meaning |
Example |
= |
Assign |
a = 5 |
+= |
Add and
assign |
a += 2 |
-= |
Subtract and assign |
a -= 3 |
*= |
Multiply and
assign |
a *= 4 |
/= |
Divide and assign |
a /= 2 |
🎛️ Bitwise Operators
Used for bit-level operations.
Operator |
Description |
& |
Bitwise AND |
` |
` |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left shift |
>> |
Right shift |
🧪 Type Conversion
C allows implicit and explicit type
conversion.
✅ Implicit Conversion:
int
x = 10;
float
y = x + 5.5; // int automatically
converted to float
✅ Explicit Conversion (Casting):
float
a = 7.5;
int
b = (int) a; // b = 7
🧰 Sample Program: Using
Variables and Operators
#include
<stdio.h>
int
main() {
int a = 10, b = 3;
float result;
result = (float) a / b;
printf("Division result: %.2f\n",
result);
return 0;
}
Output:
Division result: 3.33
🧱 Recap Table: Variable
& Operator Summary
Concept |
Summary |
Variable |
Named storage in
memory |
Data Type |
Defines type
and size of data |
Constant |
Fixed value throughout
program |
Operator |
Performs
operations on variables |
Type Conversion |
Changing from one type
to another |
🧑💻
Real-World Use Cases
A: Absolutely. C is widely used in systems programming, embedded systems, and performance-critical applications.
A: Just basic computer literacy. No prior programming knowledge is required.
A: Code::Blocks or VS Code with a C plugin is great for beginners.
A: C is a compiled language. It uses compilers like GCC or Clang.
A: With consistent practice, 4–8 weeks is sufficient to grasp core concepts.
A: Pointers and manual memory management can be tricky for beginners.
A: C is not typically used for web apps, but it can handle back-end processes or be integrated via CGI.
A: Each has its use. C is great for low-level control and speed, but C++ and Python offer more abstraction and ease of use.
A: Use a terminal/IDE to compile with gcc filename.c -o output and run with ./output.
A: GitHub, HackerRank, and open-source forums are great places to find beginner to advanced C projects.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)