0% found this document useful (0 votes)
2 views12 pages

Mod02 Notes

The document outlines various operators in C programming, including assignment, arithmetic, increment/decrement, and shorthand arithmetic assignments. It also discusses character data types, ASCII coding, and type conversion between different data types in C. Additionally, it provides examples and tasks for programming exercises related to these concepts.

Uploaded by

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

Mod02 Notes

The document outlines various operators in C programming, including assignment, arithmetic, increment/decrement, and shorthand arithmetic assignments. It also discusses character data types, ASCII coding, and type conversion between different data types in C. Additionally, it provides examples and tasks for programming exercises related to these concepts.

Uploaded by

hamzaismail054
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

• Assignment operator =

₋ works right to left


Ex: num1 = 5.3;
• Arithmetic operators +, -, *, /, %
sum = num1 + num2; // addition
diff = num1 – num2; // subtraction
prod = num1 * num2; // multiplication
Operators ratio = num1/num2 // division
(check compute_d.c and remainder = num1%num2; // remainder
operators.c in Tasks) Note:
- remainder operator % only works for integers
- operator % computes the remainder when num1 is divided
into num2
Ex: if num1 is 12 and num2 is 5 then the remainder is 2
12 = 2 x 5 + 2
• Arithmetic operators ∗, /,% have higher
precedence (are carried out first) than +
and −
Ex: a = b + c * d
Operators first c * d is computed and then added to b

(continued) • Arithmetic operator’s direction of


associativity is left to right
Ex: z = u + v + w
first u + v is computed and then added to w
Increment and Decrement operators ++, --
• incrementing an integer means increasing its
value by one unit
• two ways to increment an integer,
Other x=x+1
x++ (or ++x)
operators • difference between x++ and ++x?
- when ++ appears before the variable then x is
incremented before it is used in the expression
- when ++ appears after the variable then the
increment takes place after the variable has
been used
• Arithmetic Assignments +=, -=, *=, /=
₋ short hand for two operations
Ex: x += 2 is equivalent to x = x+2
Other Ex: x -= 3 is equivalent to x = x-3
operators Ex: x *= 4 is equivalent to x = x*4
Ex: x /= 5 is equivalent to x = x/5
(continued)
Math Expression Definition Math Library Function
𝑥 Square root function sqrt(x)
ln 𝑥 natural logarithm log(x)
log 𝑥 base 10 logarithm log10(x)
𝑒𝑥 exp(x)
Math library 𝑥𝑎 power function pow(x,a)
𝑥 absolute value function abs(x) for int
fabs(x) for float/double
#include<math.h> sin 𝑥 sine function sin(x)
cos 𝑥 cosine function cos(x)
tan 𝑥 tangent function tan(x)
sin−1 𝑥 arc-sine or sine inverse asin(x)
cos −1 𝑥 arc-cosine or cosine inverse acos(x)
tan−1 𝑥 arc-tangent or tangent inverse atan(x)
Write a program that calculates
1
𝑧= + sin 𝑥 + (𝑥 2 + 𝑦 3.5 )𝑒 𝑥𝑥
2
for the x and y values entered by the user.

Task
Here is a sample input/output:
(check mathlib.c)

Enter x and y: 1.2 3.4


You entered: 1.2000 3.4000
z = 509.9217
• A character data type, or char, occupies 1 Byte
of computer memory and has room to store a
single letter, a single digit, or a symbol.

• Format code %c is used for char variables in


printf() and scanf().
Character
• Single characters are surrounded by single
Data Type quotes.
- Ex: char myvalue = ‘a‘;

• Char data type is stored internally as 1 byte


short integers between 0 and 127. The method
of coding a character as an integer is called the
ASCII code (see next page for ASCII table).
Dec Char Dec Char Dec Char Dec Char
0‘\0‘ 16‘\020‘ 32‘ ‘ 48‘0‘
1‘\001‘ 17‘\021‘ 33‘!‘ 49‘1‘
2‘\002‘ 18‘\022‘ 34‘\"‘ 50‘2‘
3‘\003‘ 19‘\023‘ 35‘\#‘ 51‘3‘
4‘\004‘ 20‘\024‘ 36‘\$‘ 52‘4‘
5‘\005‘ 21‘\025‘ 37‘\%‘ 53‘5‘
Table of 6‘\006‘ 22‘\026‘ 38‘&‘ 54‘6‘
7‘\007‘ 23‘\027‘ 39‘\‘‘ 55‘7‘
ASCII Codes 8‘\b‘ 24‘\030‘ 40‘(‘ 56‘8‘
9‘\t‘ 25‘\031‘ 41‘)‘ 57‘9‘
10‘\n‘ 26‘\032‘ 42‘*‘ 58‘:‘
11‘\v‘ 27‘\033‘ 43‘+‘ 59‘;‘
12‘\f‘ 28‘\034‘ 44‘,‘ 60‘<‘
13‘\r‘ 29‘\035‘ 45‘-‘ 61‘=‘
14‘\016‘ 30‘\036‘ 46‘.‘ 62‘>‘
15‘\017‘ 31‘\037‘ 47‘/‘ 63‘?‘
Dec Char Dec Char Dec Char Dec Char
64‘\@‘ 80‘P‘ 96‘‘‘ 112‘p‘
65‘A‘ 81‘Q‘ 97‘a‘ 113‘q‘
66‘B‘ 82‘R‘ 98‘b‘ 114‘r‘
67‘C‘ 83‘S‘ 99‘c‘ 115‘s‘
68‘D‘ 84‘T‘ 100‘d‘ 116‘t‘
Table of 69‘E‘
70‘F‘
85‘U‘
86‘V‘
101‘e‘
102‘f‘
117‘u‘
118‘v‘
ASCII Codes 71‘G‘ 87‘W‘ 103‘g‘ 119‘w‘
72‘H‘ 88‘X‘ 104‘h‘ 120‘x‘
(Continued) 73‘I‘ 89‘Y‘ 105‘i‘ 121‘y‘
74‘J‘ 90‘Z‘ 106‘j‘ 122‘z‘
75‘K‘ 91‘[‘ 107‘k‘ 123‘{‘
76‘L‘ 92‘\\‘ 108‘l‘ 124‘|‘
77‘M‘ 93‘]‘ 109‘m‘ 125‘}‘
78‘N‘ 94‘^‘ 110‘n‘ 126‘~‘
79‘O‘ 95‘_‘ 111‘o‘ 127‘\177‘
char myvalue = ‘a‘; // declare and initialize a char

Example printf(“myvalue is : %c\n“, myvalue); // prints letter a


(Check char2ascii.c)
printf(“myvalue is : %d\n”, myvalue); // prints 97
(97 is ASCII for letter a)
Write a program that reads a student B00
number from the keyboard and prints it back
to the screen.

Task Here is a sample input/output:


Check (readwriteB00)

Enter your B00 number: B00999999


You entered: B00999999
In C, the type conversion automatically takes place
between types as follows:
- a char can be converted to an int using the ASCII code
- an int can be converted to a float (without decimal part)
- a float can be converted to a double (with less precision)

Conversion Note that conversion in the reverse direction might lead to


Between Data data loss (problem!)
Types in C
Example:
int b = 3.5; //assigning a float to an int (b becomes 3) - problem!
float a = 3; //assigning an int to a float (a becomes 3.000000) - OK
double c = a; //assigning a float to a double -- OK
float d = c; //assigning a double to a float - problem!
int e = ‘A’; //assigning a char to an int (e becomes 65 that is ASCII for A)

You might also like