Variables
Variables
Variables
Paul
What is a Variable
Variables are containers for storing data values, like
numbers and characters.
4 Length There is no specific limit on the length of a variable name in C, but it is a good practice to keep variable names
concise and meaningful for better code readability.
5 Underscore It is common in C to use underscores to separate words in a multi-word variable name, following a convention
Convention known as "snake_case." For example, `user_age`, `max_value`, `is_valid`, `_toast`, `_5valid`, etc.
6 CamelCase Another convention used for multi-word variable names is "CamelCase," where each word, except the first one,
Convention starts with an uppercase letter, and there are no underscores. For example, `userAge`, `maxValue`, `isValid`, etc.
7 Naming It is essential to choose descriptive names for variables to improve code readability and maintainability. Variables
Conventions should represent the data they store or the purpose they serve in the program.
Examples of valid
variable names in C:
• int age;
• float temperature;
• char firstName;
• int userAge;
• float maxTemperature;
• char is_valid;
Examples of invalid
Variable names in C
int 123var; // Cannot start with a digit
float my-temp; // Cannot use hyphen/minus
sign
char int; // Cannot use reserved
keyword as a variable name
int User_Age; // You can use uppercase
letter in the beginning of a
variable name, but it’s not
good practice
Following these rules and best practices for
naming variables helps in writing clean and
maintainable C code.
Constants
• If you don't want others (or yourself) to change existing variable values, you can use the
const keyword.
• This will declare the variable as "constant", which means unchangeable and read-only:
const int myNum = 15; // myNum will always be 15
If you try changing the variable
myNum = 10; //result will be an error
Operators are used to perform operations on
variables and values.
s • Comparison operators
• Logical operators
• Bitwise operators
Operator Name Description Example
Adds together two
+ Addition values x+y
Subtracts one value
- Subtraction from another x-y
Assignment *=
/=
x *= 3
x /= 3
x=x*3
x=x/3
Operators %=
&=
x %= 3
x &= 3
x=x%3
x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Operator Name Example
== Equal to x == y
Compariso !=
>
Not equal
Greater than
x != y
x>y
n < Less than x<y
Operators >=
Greater than or
equal to x >= y
Less than or
<= equal to x <= y
Operator Name Description Example
Operators
|| Logical or x < 5 || x < 4
the statements is true
Bitwise AND (&) Performs a bitwise AND operation between the corresponding bits of two integers. The result has a
1 only in the positions where both operands have a 1.
Bitwise OR (|) Performs a bitwise OR operation between the corresponding bits of two integers. The result has a 1
in the positions where either of the operands has a 1.
Bitwise XOR (^) Performs a bitwise exclusive OR operation between the corresponding bits of two integers. The
result has a 1 in the positions where the corresponding bits of the operands are different.
Bitwise NOT (~) Performs a bitwise NOT operation on a single integer, inverting all its bits. It converts 0s to 1s and 1s
to 0s.
Bitwise Left Shift (<<) Shifts the bits of an integer to the left by a specified number of positions. The leftmost bits are lost,
and the rightmost bits are filled with 0s.
Bitwise Right Shift (>>) Shifts the bits of an integer to the right by a specified number of positions. The rightmost bits are
lost, and the leftmost bits are filled with either 0s or the sign bit, depending on whether it is an
arithmetic or logical shift.
END