0% found this document useful (0 votes)
13 views5 pages

Variable and Operators (1)

The document explains the concept of variables and constants in programming, particularly in C, highlighting their characteristics, naming rules, and the importance of using constants for code readability and maintenance. It also categorizes various types of operators in C, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, comma, and sizeof operators, along with their descriptions and examples. Additionally, it provides information on how to check the size of different data types using the sizeof operator.

Uploaded by

kneshibrian
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
13 views5 pages

Variable and Operators (1)

The document explains the concept of variables and constants in programming, particularly in C, highlighting their characteristics, naming rules, and the importance of using constants for code readability and maintenance. It also categorizes various types of operators in C, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, comma, and sizeof operators, along with their descriptions and examples. Additionally, it provides information on how to check the size of different data types using the sizeof operator.

Uploaded by

kneshibrian
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

A variable is a named storage location in a computer's memory that is used to hold data that can

be changed during program execution. Variables are fundamental concepts in programming,


allowing developers to store, modify, and retrieve values as needed
Key Characteristics of Variables:
1. Name: Each variable has a unique identifier (name) that allows the programmer to
reference it in the code. The name should be meaningful and reflect the data it holds.
2. Data Type: A variable has a specific data type that determines the kind of data it can
store (e.g., integers, floats, characters). The data type also influences how much memory
is allocated for the variable and what operations can be performed on it.
3. Value: The actual data stored in the variable. The value can be changed (assigned) during
the execution of the program.
4. Scope: The context in which the variable is accessible. A variable may be local
(accessible only within a specific function) or global (accessible throughout the entire
program).
5. Lifetime: The duration for which a variable exists in memory. Local variables exist only
while the function is executing, while global variables exist for the duration of the
program.
When declaring a variable in C, there are several rules and conventions that you should follow to
ensure that your code is valid and maintainable
Variable Name Rules
i. Valid Characters: Variable names can include letters (both uppercase and lowercase),
digits, and underscores (_). However, they must start with a letter or an underscore.
ii. Example: var1, _var, myVariable
iii. No Special Characters: Variable names cannot contain special characters such as @, #,
$, etc.
iv. No Spaces: Spaces are not allowed in variable names.
v. Case Sensitivity: Variable names are case-sensitive, meaning Var and var would be
considered different variable
vi. No Keywords: Variable names cannot be the same as C keywords or reserved words
(e.g., int, return, if, while).
Constants are fixed values that do not change during the execution of a program. Unlike
variables, which can be modified, constants remain the same throughout the program's runtime.
Using constants can help improve code readability and maintainability, as they provide
meaningful names for fixed values
Key Characteristics of Constants:
1. Immutable: Once a constant is defined, its value cannot be altered. This helps prevent
accidental changes in critical values.
2. Named Constants: Constants are often given descriptive names (also called identifiers)
to make the code more understandable.
3. Data Types: Constants can be of any data type, including integers, floating-point
numbers, characters, and strings.
4. Scope: Constants can have local or global scope, depending on where they are defined.
Defining Constants in C
In C, you can define constants in several ways:
1. Using #define Preprocessor Directive: This creates a constant using a preprocessor
macro.
#define PI 3.14159 // PI is a constant
2. Using const Keyword: This creates a constant variable that cannot be modified.
const int DAYS_IN_WEEK = 7; // DAYS_IN_WEEK is a constant
Benefits of Using Constants:
 Improved Readability: Constants give meaningful names to fixed values, making the
code easier to understand.
 Ease of Maintenance: If a constant value needs to change, you only need to update it in
one place (where it's defined).
 Prevention of Errors: Constants help avoid accidental modifications to values that
should remain unchanged.
A complete example that uses both #define and const:
#define MAX_SCORE 100 // Using #define to create a constant

const float PI = 3.14159; // Using const to create a constant variable


In C, operators are special symbols that perform operations on variables and values. They can be
categorized into several types based on their functionality. Here’s an overview of the main types
of operators in C:
1. Arithmetic Operators
These operators are used to perform basic mathematical operations.
Operator Description Example

+ Addition a+b

- Subtraction a-b

* Multiplication a * b

/ Division a/b

% Modulus a%b

2. Relational Operators
These operators are used to compare two values.
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


3. Logical Operators
These operators are used to perform logical operations.
Operator Description Example

&& Logical AND (a && b)

` `
Operator Description Example

! Logical NOT (!a)


4. Bitwise Operators
These operators are used to perform bit-level operations.
Operator Description Example

& Bitwise AND a & b

` ` Bitwise OR

^ Bitwise XOR a ^ b

~ Bitwise NOT ~a

<< Left shift a << 1

>> Right shift a >> 1


5. Assignment Operators
These operators are used to assign values to variables.
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


6. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by one.
Operator Description Example

++ Increment ++a or a++

-- Decrement --a or a--


7. Conditional (Ternary) Operator
This operator is a shorthand for the if-else statement.
Operator Description Example

?: Ternary conditional operator result = (a > b) ? a : b;


8. Comma Operator
This operator allows two or more expressions to be evaluated in a single statement, with the last
expression being the result.
Operator Description Example

, Comma operator a = (b = 3, b + 2);


9. Sizeof Operator
This operator returns the size of a variable or data type in bytes.
Operator Description Example

sizeof Size of a variable/data type sizeof(int)


In C, you can check the size of a variable using the sizeof operator. This operator returns the size
of a variable (or data type) in bytes. Here’s how to use it:
You can also use sizeof directly with types
printf("Size of int type: %zu bytes\n", sizeof(int));
printf("Size of float type: %zu bytes\n", sizeof(float));
printf("Size of double type: %zu bytes\n", sizeof(double));
printf("Size of char type: %zu bytes\n", sizeof(char));
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Size of int type: 4 bytes
Size of float type: 4 bytes
Size of double type: 8 bytes
Size of char type: 1 byte

You might also like