Variables, Data Types, and Operators in C
Course Title: Mastering C: From Fundamentals to Advanced Programming
Section Title: Introduction to C and Development Environment
Topic: Basic C syntax: Variables, data types, and operators
In the previous topics, we explored the history and evolution of the C programming language and set up a development environment to start coding. In this topic, we will delve into the fundamental syntax of C programming and explore variables, data types, and operators.
Variables in C
Variables are names given to a location in memory that holds a value. In C, variables are declared before they can be used, and each variable has a specific data type that determines its size and behavior.
Declaring Variables
A variable is declared by specifying its data type followed by its name. Here is a simple example:
int myVariable;
In this example, myVariable
is the name of the variable, and int
is its data type.
Data Types in C
C has several built-in data types that are used to store different types of values. Here are some of the most common data types:
Data Type | Description | Size (bytes) | Example |
---|---|---|---|
int |
Integer value | 2 or 4 | 1, 2, 3 |
float |
Floating-point | 4 | 3.14, -0.5 |
char |
Character | 1 | 'a', 'z' |
double |
Double-precision | 8 | 0.000001 |
bool |
Boolean value | 1 | true, false |
Note: The size of a data type may vary depending on the system architecture.
Initializing Variables
Variables can be initialized at the time of declaration or later in the code.
// Initializing a variable at declaration
int myVariable = 10;
// Initializing a variable later
int myVariable;
myVariable = 10;
Operators in C
Operators are used to perform operations on variables and values. C provides various types of operators:
Arithmetic Operators
| Operator | Description | Example | |----------|-------------|---------| |
+
| Addition |a + b
| |-
| Subtraction |a - b
| |*
| Multiplication |a * b
| |/
| Division |a / b
| |%
| Modulus |a % b
|Assignment Operators
| Operator | Description | Example | |----------|-------------|---------| |
=
| Assign |a = b
| |+=
| Add and Assign |a += b
| |-=
| Subtract and Assign |a -= b
| |*=
| Multiply and Assign |a *= b
| |/=
| Divide and Assign |a /= b
| |%=
| Modulus and Assign |a %= b
|Comparison Operators
| Operator | Description | Example | |----------|-------------|---------| |
==
| Equal to |a == b
| |!=
| Not Equal to |a != b
| |>
| Greater than |a > b
| |<
| Less than |a < b
| |>=
| Greater than or Equal to |a >= b
| |<=
| Less than or Equal to |a <= b
|
Practical Example
#include <stdio.h>
int main() {
// Declare and initialize variables
int a = 10;
int b = 20;
// Perform arithmetic operations
int sum = a + b;
int difference = a - b;
// Use comparison operator
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is less than or equal to b\n");
}
// Print results
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
return 0;
}
Key Takeaways
- Variables are declared before they can be used and must have a specific data type.
- C has several built-in data types, including
int
,float
,char
,double
, andbool
. - Operators are used to perform operations on variables and values.
- Arithmetic operators perform mathematical operations.
- Assignment operators assign values to variables.
- Comparison operators compare values.
What to Expect Next
In the next topic, 'Writing your first C program: Hello, World!', we will put our knowledge of basic C syntax into practice by writing and executing our first C program.
Images

Comments