0% found this document useful (0 votes)
61 views

Variable and Data Types

1) Variables are used to store and retrieve data in a program. They must be declared with a specific data type before being assigned a value. Common data types in C include char, int, float, and double. 2) Operators are used to perform operations on variables and values. Common arithmetic operators include +, -, *, /, and %. Relational operators like >, <, ==, and != are used to compare values. Logical operators && and || are used to combine conditional expressions. 3) Variables are assigned values using the = operator. Values can be assigned when variables are declared or at a later time. Operators follow a specific order of precedence that determines the order in which

Uploaded by

Masudul Huq Robi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Variable and Data Types

1) Variables are used to store and retrieve data in a program. They must be declared with a specific data type before being assigned a value. Common data types in C include char, int, float, and double. 2) Operators are used to perform operations on variables and values. Common arithmetic operators include +, -, *, /, and %. Relational operators like >, <, ==, and != are used to compare values. Logical operators && and || are used to combine conditional expressions. 3) Variables are assigned values using the = operator. Values can be assigned when variables are declared or at a later time. Operators follow a specific order of precedence that determines the order in which

Uploaded by

Masudul Huq Robi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Fundamental C Programming

Class-3

1
Variables & Data Types

2
Variables in Programming
Represent storage units in a program
Used to store/retrieve data over life of
program
Type of variable determines what can be
placed in the storage unit
Assignment – process of placing a
particular value in a variable
Variables must be declared before they
are assigned
The value of a variable can change; A
constant always has the same value 3
Variable Declaration
• All variables must be declared in a C
program before
the first executable statement! Examples:
main(){
int a, b, c;
float d;
/* Do something here */
}

4
Naming Variables
• When a variable is declared it is given a name
• Variable names in C may only consist of letters, digits,
and underscores and may not begin with a digit
• Variable names in C are case sensitive
• ANSI standard requires only 31 or fewer characters.
Enhances portability to follow this rule
• A good name for your variables is important
– Choose a name that reflects the role of the variable in a
program, e.g.
• Good: customer_name, ss_number;
• Bad : cn, ss;

5
Variable Assignment

• After variables are declared, they must


(should) be given values. This is called
assignment and it is done with the ‘=‘ operator.
Examples:
float a, b;
int c;
b = 2.12;
c = 200;

6
Basic C Data Types
There are four basic data types in C:
– char
– int
– float
– double

7
Declaring Variables
• All variables must always be declared before the first
executable instruction in a C program
• Variable declarations are always:
– var_type var_name;
• int age;
• float annual_salary;
• double weight, height; /* multiple vars */
• In most cases, variables have no meaningful value at
this stage. Memory is set aside for them, but they are
not meaningful until assigned.

8
Assigning Values to Variables
• Either when they are declared, or at any
subsequent time, variables are assigned values
using the “=“
operator.
• Examples
int age = 52; //joint declaration/assignment
double salary;
salary = 150000.23;
age = 53; //value may change at any time

9
Operators

10
Operators
• Arithmetic
+ , - , * , / , % (modulus)
++ , -- (prefix and postfix increment)
+= , -= , *= , /= , %=
• Logical
&& , || , == , !=
• Bit Shifting
<< , >> , | , & , ^

11
Arithmetic Operations
Five simple binary arithmetic operators
1. + “plus” c = a + b
2. - “minus” c = a - b
3. * “times” c = a * b
4. / “divided by” c = a/b
5. % “modulus” c = a % b
1. What are the values of c in each case above if
– int a = 10, b = 2;
– float a = 10, b = 2;
– int a = 10; float b = 2; ??
12
Relational Operators
• Four basic operators for comparison of values in C.
These are typically called relational operators:
1. > “greater than”
2. < “less than”
3. >= “greater than or equal to”
4. <= “less than or equal to”
• For the declaration
int a=1,b=2,c;
what is the value of the following expressions?
a > b; a<b; a>=b; a<=b

13
Relational Operators, cont.
• Typically used with conditional expressions,
e.g.
– if (a < 1) then …
• However, also completely valid expressions
which evaluate to a result – either 1 (true) or 0
(false).
int c, a=2, b=1;
c = (a > b);
What is the value of c?
14
Equality Operators
• C distinguished between relational and equality
operators.
• This is mainly to clarify rules of order of
precedence.
• Two equality operators
1. == “is equal to”
2. != “is not equal to”
• These follow all of the same rules for relational
operators described on the previous slide.

15
Logical Operators
Logical Operators are used to create compound
expressions
• There are two logical operators in C
1. || “logical or”
¨ A compound expression formed with || evaluates to
1 (true) if any one of its components is true
2. && “logical and”
¨ A compound expression formed with && evaluates
to true if all of its components are true

16
Logical Operators, cont.
Logical operators, like relational operators, are
typically used in conditional expressions
if ( (a == 1) && (b < 3) || (c == 1) )
• However, these can also be used in regular
expressions
int a = 1, b = 2, c = 3, d;
d = ( a > b ) || ( c == (b – 1) );
What is the value of d here?
17

You might also like