03 Variable Type Expression GUI
03 Variable Type Expression GUI
• Constants
– Constants like 14, ‘a’, 1.4e-5, etc.
• Operators
– Arithmetic operators like +, -, *, / ,%, etc.
– Logical operators like ||, &&, !, etc. and so on.
• White Spaces
– Spaces, new lines, horizontal/vertical tabs, comments (a
sequence of characters enclosed in /* and */ or after //) etc.
These are used to separate the adjacent identifiers, keywords and
constants.
5 September 2022 Programming for Engineers (C language) NV Binh 7
1 Introduction to Computer and C language
Basic Data Types
• Integral Types: Integers are stored in various sizes. They can be
signed or unsigned.
Suppose an integer is represented by a byte (8 bits).
Leftmost bit is the sign bit. If the sign bit is 0, the number is treated
as positive. Otherwise (the sign bit is 1), the number is negative.
Example:
01001011 = 75 (decimal).
The largest positive number is 01111111 = 27 – 1 = 127.
Negative numbers are stored in two ways as two’s complement or
as one’s complement.
-75 = 10110100 (one’s complement)
-75 = 10110101 (two’s complement)
5 September 2022 Programming for Engineers (C language) NV Binh 8
1 Introduction to Computer and C language
Basic Data Types
Example: 2’s complement
• To add 6+3 using 5 bit 2’s complement
representation,
00110
+
00011
——–
01001
• To subtract 6-3, rewrite as 6+ (-3):
00110
+
11101 (2s-complement of 3)
———
5 September 2022 00011 Programming for Engineers (C language) NV Binh 9
1 Introduction to Computer and C language
• Data Types
Basic Integral Types
Type Storage size Value range
• Global Variables
/* Compute area and perimeter of a circle */
#include <stdio.h>
float pi = 3.14159; /* Global */
int main() {
– These variables are declared float rad;
outside all functions.
printf(“Enter the radius: ”);
scanf(“%f”, &rad);
– Lifetime of a global variable is the
if (rad > 0.0) {
entire execution period of the float area = pi * rad * rad;
program. float peri = 2 * pi * rad;
Example
if (num % 2 == 0)
printf(“%d is an even number
\n”, num);
else
printf(“%d is an odd number
\n”, num);
• Logical Operators
– &&, || and ! are the three logical operators
expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero
expr1 || expr2 has a value 1 if either expr1 or expr2 is nonzero
!expr1 has a value 1 if expr1 is zero else 0
– Example
if (marks >= 40 && attendance >= 75) grade = ‘P’
if (marks < 40 || attendance < 75) grade = ‘N’
b + (++b) + b = 33
b + b + (++b) = 31
b + b * (++b) = 132
c = 5;
printf(“%d\n”, c);
printf(“%d\n”, ++c);
printf(“%d\n”, c);
return 0;
}
5 September 2022 Programming for Engineers (C language) NV Binh 26
1 Introduction to Computer and C language
Precedence and Order of evaluation
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= Right to left
|=
Comma , Left to right
5 September 2022 Programming for Engineers (C language) NV Binh 27
1 Introduction to Computer and C language
Type Conversions
– The operands of a binary operator must have the same type and the result is
also of the same type.
– Integer division:
c = (9 / 5) * (5 - 32)
– The operands of the division are both int, and hence, the result also would be
int. For correct results, one may write:
c = (9.0 / 5.0) * (5 - 32)
– In case the two operands of a binary operator are different, but compatible,
then they are converted to the same type by the compiler. The mechanism (set of
rules) is called Automatic Type Casting.
c = (9.0 / 5) * (5- 32)
– It is possible to force a conversion of an operand. This is called Explicit Type
casting.
c = ((float) 9 / 5) * (5 – 32)
B is the base
e is the exponent
– All these put together have finite number of bits (usually 32 or 64 bits) of storage.
– Example
Assume B = 10 and p = 3
23.7 = +0.237E2
23.74 = +0.237E2
37000 = +0.370E5
37028 = +0.370E5
-0.000124 = -0.124E-4