Variables: - Symbol Representing A Place To Store Information
Variables: - Symbol Representing A Place To Store Information
age
20
v1
50
v2
sum
30
80
Variable Declaration
A variable must be declared before it can be used.
Tell the compiler that you want to reserve some memory in which to store data of some specified type.
Declaration of Variables
type name = initial_value; type name1 = initial_value1, name2 = initial_value2, ;
{ int v1, v2, sum; v1 = 50; v2 = 30; sum = v1 + v2; }
{
int v1= 50, v2 = 30, sum; sum = v1 + v2;
float
single-precision floating point
double
double-precision floating point
char
one byte character
Variable Names
Contains one or more characters,
A-Z, a-z 0-9 _
As always, case-sensitive
Size Limitation
Maximum of 63 characters for a variable name
Sometimes 31 or 8 in very old C
Case sensitive: upper and lower case characters are regarded as different
floating int Int main3 4yi
no commas
12,000
Ranges
Machine dependent
Minimum: 16 digits in a literal integer 32 bits of storage
Signed: 2,147,483,648 to +2,147,483,647 Unsigned: 0 to +4,294,967,295
Maybe 64 on others
Assignment
y = 10; z = 5;
Operations Plus: +
x = y + z;
Minus: x = y z;
Muliply: *
x = y * z;
Divide: /
x = y / z;
Modulus
x = y % z;
Display
%i print out the decimal value %o print out the octal value
Display
%i print out the decimal value %x print out the hexadecimal value
Example - I
#include <stdio.h> int main() { int a, b, c, d; a = 10; b = 20; c = 0177; d = 0xAF; printf(The four numbers are %i, %i, %o, %x\n, a, b, c, d); printf(The four decimal numbers are %i, %i, %i, %i\n, a, b, c, d); }
Example II
#include <stdio.h> int main() { int a, b, c, f; a = 10; b = 20; c = 0177; f = a/b; printf(%i / %i = %i\n, a, b, f); f = b/a; printf(%i / %i = %i\n, b, a, f); f = c/a; printf(%i / %i = %i\n, c, a, f); }
float Variables - II
Ranges
IEEE floating-point standard
e = 8, f = 23
3.41038
Assignment
y = 10.00; z = 5.8;
Calculation Plus: +
x = y + z;
Minus: x = y z;
Muliply: *
x = y * z;
Divide: /
x = y / z;
Example I
#include <stdio.h> int main() { int a, b, c; float f; a = 10; b = 20; c = a/b; printf(%i / %i = %i\n, a, b, c); f = a/b; printf(%i / %i = %f\n, a, b, f); }
Ranges 1.7976910308
Display
%c
Example II
#include <stdio.h> int main() { int a, b, f; char c;
a = 36; b = 52; c = a;
printf(The three numbers are %i , %i, %i\n, a, b, c); printf(The three characters are %c , %c, %c\n, a, b, c); }
printf(integerVar = %? \n, integerVar); printf(floatingVar = %? \n, floatingVar ); printf(doubleVar = %? \n, doubleVar ); printf(doubleVar = %? \n, doubleVar ); // scientific notation printf(charVar = %? \n, charVar ); return 0; }
signed, unsigned
specify whether is a signed quantity use u to indicate unsigned %u for unsigned
Assignment Operators
Join the arithmetic operators
Format: op=
Examples:
Unary Operators
Unary plus / minus
+/ Example: -a
Unary increment/decrement
++ / --
M = M + 1;
M += 1;
++M;
M++;
Arithmetic Operators
add: +, minus: -, multiply: *, divide: /, modulus: % Parentheses (grouping): ( ) Unary plus / minus
+ -
Unary increment/decrement
++ --
Operator Precedence
Precedence
Operators with higher precedence are evaluated first Operators with same precedence are evaluated from left to right In decreasing precedence
() unary increment (++), unary decrement (--) unary plus (+), unary minus (-) multiply (*), divide(/), modulus(%) add(+), minus(-)
Order for
c = -a * b a+b*c/d