### week_3 - METU Computer Engineering

```Valid variable names
Arithmetic expressions
Logic operators
Unary and binary operators
Assignment operators
Formating the output of numbers
Error types
Elements of a C program
int1 and Int1 are not the same identifiers/variables
C has the following basic built-in datatypes.
int
float
double
char
int first, second;
scanf("%d%d", &first, &second);
double miles; /* distance in miles
scanf("%lf", &miles);
*/
Type conversions (casting)
float a = 5.25;
int b = a;
/*Casting from float to int. The value of b here is 5*/
char c = ‘A’;
int x = c;
/*Casting from char to int.
The value of x here is 65: the ASCII code of ‘A’*/
int x=7, y=5 ;
float z;
z=x/y;
/* the value of z is 1.00 */
int x=7, y=5;
float z;
z = (float)x/(float)y;
/ the value of z is 1.4*/
Type conversions (casting)
printf( "Welcome : %d", (3/2) );
Output is : 1 and fraction part of the number is lost
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
Value of mean : 3.400000
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
printf("Value of sum : %d\n", sum );
Value of sum : 116
What is the result of printf("%d", ‘d’– ‘a’ );
! exclamation mark
= is assignment and == is an equality operator
Assignment operators
=
+=
-=
*=
/=
%=
a+=10; is the same with
a=a + 10;
Logic operators
&& AND
||
OR
if( (a > = 25) || (b==4) )
if( (a > = 25) && (b!=4) )
= -5
=1
Formating the output of integer values
Formating the output of double values
Errors
Compile time  Syntax errors
•When the code violated the grammer rules of C.
•Compiler detecs these errors
Run-time errors
•happen when the program directs the computer to an
illegal operation.
•Such as Division by zero
Logic errors
•A faulty algorithm
•It gives no error message.
Run-time error (division by zero)
If the value of variable “second” is given as zero
Logic error
Data Type of an Expression
The data type of an expression depends on the type(s) of its operands.
if both operands are int  int
otherwise
 double
An expression that has operands of both type int and double is a mixed-type expression .
The data type of such a mixed-type expression will be double .
When an assignment statement is executed, the expression is first evaluated; then the
result is assigned to the variable listed to the left of the assignment operator ( = ).
Either a type double or a type int expression may be assigned to a type double
int m, n;
double p , x , y;
m = 3;
n = 2;
p = 2.0;
x = m / p;
y = m / n;
3/2.0
3/2
Sample questions
( be careful that
the values are integers)
Hint: b--/2*3%2*4 : execute this part from left to right
This one is first executed
Homework
Write a program that:
1.
Reads two integers and two float numbers from the user
2.
Writes examples with preincrement (++a),
postincrement operators (a--).
3.
Writes examples with += and -= operators
4.
Casts the integers to float values and writes results
5.
Writes the result of the expression
3+(17*2-9)/2*3-29/2
Send the C file to my address ([email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */)
```