0% found this document useful (0 votes)
7 views17 pages

Variables

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 17

Variables

Paul
What is a Variable
Variables are containers for storing data values, like
numbers and characters.

“YOU CAN CHANGE THE VALUE WHENEVER YOU WANT”


Data types Primitive Data types Non-primitive
Predefined Created by the Programmer
• Primitive Data types (You)
• Byte, Short, int, long, float,
double, Boolean, char Not used to call methods Used to call methods to
perform certain functions
• Non-primitive Data types
• Strings, Arrays, Classes
Always has a Value Can be NULL
Primitive Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

Stores whole numbers from -9,223,372,036,854,775,808 to


long 8 bytes 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
To create a variable, specify the type, name and assign it a
value
type variable_Name = value;
type = what data type do you intend on using?
variable_Name = what do you want to call it?
value = what value do you want it to be?
How to USE e.g.
Variables int test = 5;
Or
float gun = 55.2345;
Or
boolean fail = true;
To create a variable, specify the type, name and assign a value
later.
type variable_Name ;
variable_Name = value;

type = what data type do you intend on using?


How to USE variable_Name = what do you want to call it?

Variables value = what value do you want it to be?


e.g.
continued… int test ;
float gun ;
boolean fail ;
test = 234;
gun = 67.8904;
fail = false;
Rules for naming Variables
The rules for naming variables (identifiers) are as follows:
1 Valid Characters Variable names can consist of letters (both uppercase and lowercase), digits, and the underscore character '_'. The
first character of the variable name must be a letter or an underscore.
2 Case Sensitivity C is case-sensitive, which means that uppercase and lowercase letters are considered distinct. For example,
`variable`, `Variable`, and `VARIABLE` are treated as different identifiers.
3 Reserved You cannot use reserved keywords (also known as keywords) as variable names because they have a specific
Keywords meaning in the C language. Examples of reserved keywords include `int`, `float`, `char`, `if`, `else`, `while`, `void` etc.

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.

C divides the operators into the following groups:

Operator • Arithmetic operators


• Assignment operators

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

Arithmetic * Multiplication Multiplies two values


Divides one value by
x*y

Operators / Division another


Returns the division
x/y

% Modulus remainder x%y


Increases the value of
++ Increment a variable by 1 ++x

Decreases the value of


-- Decrement a variable by 1 --x
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3

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

Returns true if both


&& Logical and x < 5 && x < 10
statements are true

Logical Returns true if one of

Operators
|| Logical or x < 5 || x < 4
the statements is true

Reverse the result,


!(x < 5 && x <
! Logical not returns false if the
10)
result is true
Bitwise operators are special operators that perform operations
Bit Wise at the bit level of integers (integral data types). They manipulate
individual bits of the operands. These operators are quite useful
Operators when dealing with low-level programming, device drivers, and
efficient memory management.

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

You might also like