3.variables & Operators
3.variables & Operators
Sarwar Morshed
American International University-Bangladesh (AIUB)
Topics Covered
Naming Convention of Variables.
Types of Variables
Constants
Variable Declaration
Arithmetic, Logical and BitwiseOperators
Increment, Decrement and Assignment Operators
How to Declare a Variable
To declare a variable of type char with name ch.
char ch;
A variable is a location in memory where data is
stored.
A variable has two parts;
<type> <name>;
Type specifies what kind of data is allowed to store in
that variable.
Name or Identifier is used to identify the variable.
Naming Convention of Variables
A variable name cannot be a keyword.
Example: float, int, char, main, if, else, for, while.
These words are already reserved by C.
A variable name must start with either an
underscore ‘_’ or an alphabet (Lower or Upper).
A variable name can consist of digits, alphabets and
underscore.
Correct Incorrect
count 1count
test123 hi!!
high_balance high…balance
Types of Variables
Data Types
char - single character (8 bits)
int - 32 bits
float - 32 bits
double - 64 bits
The amount of space occupied by these data types
depends on the machine architecture
Modifiers
Variables could be signed or unsigned.
Variables could be short or long.
Types Of Variables
Unsigned data is always positive or zero.
If we have a signed char, then the range is from
-128 to 127.
If it is unsigned, then the range is from 0-255.
void main() {
float f = 1.245;
int i;
i=f;
printf("%d\n", i);
f=i+f;
printf("%0.3f\n", f);
}
Casting
You can explicitly cast a variable of one type to be a
variable of another type.
This is useful if you aren’t sure how conversion will
work, or you want to force conversion to happen in a
specific way.
Example:
int a = (int) 12.54;
Casting Example
#include <stdio.h>
void main() {
int a=15, b=10;
double x;
x = a / b;
printf("%0.3f\n", x);
x = (double) a / (double) b;
printf("%0.3f\n", x);
}
Using Scanf
Use scanf to get input from the user.
Same syntax as printf.
Use %d, %f, %c etc.
Can get many inputs with single scanf
scanf("%d%d%d", &a, &b, &c);
Use & (ampersand) before the variable.
Printing a float
Simple form:
printf( "%f", 3.141592653 );
Fancy form:
printf( "%6.2f", 3.141592653 ); // prints two spaces followed
by 3.14
6 specifies minimum field width: at least 6 characters
will be printed, with spaces added if necessary
2 specifies max. number of digits to be printed after the
decimal point
Printing an int as an octal number
printf( "%o\n", 17 ); // prints 21
Printing an int as a hexadecimal number
printf( "%x\n", 31 ); // prints 1F
scanf / printf example
#include <stdio.h>
void main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("a=%d, b=%d, c=%d", a, b, c);
}
Arithmetic Operators
• C has a number of arithmetic operators.
Assignment operator: =
Binary arithmetic operators: +, -, *, /, %
Can be applied to int, float, or double, except for
% which can only be applied to integers.
% is the “modulus” or “mod” operator: a % b is equal
to the remainder when a is divided by b.
Example: 8 % 3 == 2.
Unary arithmetic operator: -. Example:
x = -y;
Arithmetic Operators
Shortcut operators: +=, -=, *=, /=
x += 2; /* equivalent to x = x + 2; */
x *= 2; /* equivalent to x = x * 2; */
Increment/decrement operators: ++, --
x++; /* acts like x = x + 1; */
x--; /* acts like x = x - 1; */
++ and – –: tricky expressions
Both x++ and ++x are expressions. The expression x++ acts
like x, and ++x acts like the expression x+1.
What’s special is the side effect: evaluating these
expressions causes x to be incremented by 1.
int a = 10, b, c;
b = a++; /* a is now 11, b is 10 */
c = ++b; /* a, b, c are all 11 */
For clarity, try not to mix ++ or - - into complicated
expressions.
Note that the expression that ++ or -- is applied to must
be a variable.
(x + 2)++; /* no good! */
x + 2 = 8; /* no good! */
(x++)++; /* no good! */
Order of Evaluation
How are expressions with many operators evaluated?
Two considerations:
Precedence
Associativity
How is 1 + 2 * 3 evaluated?
Parentheses must be used if we want the addition to
be performed first.
Order of Evaluation
What about expressions containing operators at the
same precedence level? E.g., (12 / 6 * 2) or (5 - 3 - 1)?
These parse as:
((12 / 6) * 2) and ((5 - 3) -1)
They are left associative.