0% found this document useful (0 votes)
18 views53 pages

Bil200 Week 3

The document provides information about type casting in C programming. It discusses supported integer and floating point types in C and describes implicit and explicit type casting. Implicit casting occurs automatically when different types are used in an expression, while explicit casting involves using type cast operators like (int) or (double). The document also gives examples of integer and floating point conversions through implicit and explicit casting.

Uploaded by

Zülal Zülalma
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)
18 views53 pages

Bil200 Week 3

The document provides information about type casting in C programming. It discusses supported integer and floating point types in C and describes implicit and explicit type casting. Implicit casting occurs automatically when different types are used in an expression, while explicit casting involves using type cast operators like (int) or (double). The document also gives examples of integer and floating point conversions through implicit and explicit casting.

Uploaded by

Zülal Zülalma
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
You are on page 1/ 53

BiL200 Computer Programming

Prof. Dr. Tansu Filik

WEEK – 3 : Assignments, Type Casting, Logical Operators, Operation precedence


Previously on Bil 200

● Data Types in C

● Variables and Values

● I/O Functions in C

● Formatted writing and reading


● Operations in C

● Unitary Operations

(applied to single variable, +.-,++,--)


Announcement: The orientation lab for BİL200 - Computer Programming course will be held between
October 16-19. Each group must attend the orientation lab at their own time shared below.
Group A : Monday 16.00-18.00 Group B : Tuesday 09.00-11.00 Group C : Tuesday 11.00-13.00
Group D : Thursday 16.00-18.00 Location: Dept. of Industrial Eng. - Computer Lab (END-114)
Bil200 – Computer Programming

printf & scanf example

 Example: Write a program which ask user’s name, surname and age then print as Dear <Name>
<Surname> your age is <Age>.
 Pseudo Code
Print «Please enter your name surname and age:»
Read <Name> and <Surname> and <Age>
Print «Dear <Name> <Surname> your age is <Age>»

 C Code:
#include <stdio.h>
void main(void){
int age;
char name[25], surname[25];
printf ("Please enter your name surname and age : ");
scanf ("%s %s %d", &name, &surname, &age);
printf("Dear %s %s your age is %d.\n", name, surname, age);
}
Bil200 – Computer Programming

Formats ( printf & scanf )

int b = 756;
printf(“%d”, b);

printf (“%9d\n”, b);


Computer Programming

Formats (int)
Computer Programming

Formatted writing, Example - printf

int a, b;
float c, d;

a = 17;
b = a / 2;
printf(“%d\n”, b);
printf(“%3d\n”, b);
printf(“%03d\n”, b);

c = 21.3;
d = c / 3;
printf("%4.2f\n", d);
Computer Programming

Formatted writing, Example - printf

%w.pf

The w sets the maximum width of the entire number, including the
decimal place. The p sets precision.

For example:
printf("%9.2f",12.456);
12.45
----12.45
Computer Programming

Formatted input

scanf ( ) ;
 The function is used for reading values of variables
whose types are specified:

scanf ( “format" , &var1, &var2, … ) ;

 “format" indicates the types of variables whose values


are entered. We use & symbol before the variable names.
This & sign indicates the memory address of the variable.
Computer Programming

Formatted input

 Examples
float a;
int b;
scanf("%d %f", &b, &a);

float a;
long int b;
scanf("%4ld %4.2f", &b, &a);
Computer Programming

Inputting from console

int num;
scanf(“%d”,&num);

 The entered number from the keyboard is stored in a physical


address (of two bytes) allocated for num

int a, b;
scanf(“%d %d”, &a, &b);
Computer Programming

Inputting from console

• In scanf(), the parameters are always addresses. For


numeric variables, you put “&” before the variable name to
make it an address
&num &num

0
2 byte
Reading A
from
keyboard
(Assume
10 is
entered)
Computer Programming

Components of a C program: Expressions

 Character lists that consist of letters numbers, and _


(underscore).
 Never start with numbers.
 Must be different than keywords.
 Case sensitive.
 No Turkish specific letters!
 Examples
mErHaBa, x1, y2, _benim_sayi_
Computer Programming

Components of a C program: Assignments

 Gives a value to a variable.


 Assignment operator is =
Computer Programming

Components of a C program: Assignments

 Examples
float x = 2.5 ;

char ch ;
int sayi ;

ch = '\n' ;

sayi = 4 + 5 ;
/* now the value is 9 */

sayi = sayi * 2;
/* now the value is 18. */
Computer Programming

Components of a C program: Assignments

 Examples: tamSayi = 'A';


#include <stdio.h> karakter = 'A';
/* assignments.*/ kayarNokta = 'A';
int main() tamSayi = 33.33;
{ karakter = 33.33;
int tamSayi; kayarNokta = 33.33;
char karakter;
float kayarNokta; tamSayi = kayarNokta;
kayarNokta = tamSayi;
tamSayi = 33;
karakter = 33; return 0;
kayarNokta = 33; }
Computer Programming

Components of a C program: Assignments

 Examples:
int tamSayi;
char karakter; printf("tamSayi degeri %d \n",
tamSayi);
float kayarNokta;
printf("karakter degeri %c \n",
float ayarNokta; karakter);
printf("kayarNokta degeri %f
tamSayi = 33; \n",kayarNokta);
karakter = 33; printf("ayarNokta degeri %f \n",
ayarNokta);
kayarNokta = 33;
Computer Programming

Components of a C program: Assignments

 Examples:
tamSayi degeri 33
karakter degeri !
kayarNokta degeri 33.000000
kayarNokta degeri -107374176.000000
Computer Programming

C Programming Language: Example

#include <stdio.h> Write explanatory


comments in your
/*Convert degrees to radians program
*/
const float PI = 3.1415926; Global variable
int main()
{ Local variables
Print "Dereceyi Gir:" float derece;
Variable declaration: must defined before
Read derece float radyan; using

Formatted printing is done


radyan = PI / 180 * derece printf("Dereceyi gir:"); by printf(). It is defined
Print radyan scanf("%f", &derece); inside stdio.h

radyan = PI/180*derece;
printf("%f\n", radyan);
return 0;
}
Introduction to Programming Languages

Type casting

Conversions between double and int


Introduction to Programming Languages

Type casting

Supported integer types


 C language has several integer types.
 char 8 bit
 short 16 bit
 unsigned short 16 bit
 int 32 bit
 unsigned int 32 bit
 long int 32 bit
 unsigned long 32 bit
Introduction to Programming Languages

Type casting

Supported floating types


 C language has several floating point types.

 float 32 bit
 double 64 bit
 long double 64 bit
Type Casting
Introduction to Programming Languages

Type casting

Implicit Casting
If both int and double (or float) is used in an expression, C
compiler automatically does type casting.
 int k = 5, m = 4, n;
 double x = 2.5, y = 4.1, z;
 z = k + x; k variable is converted to double
before the + operation.

 z = k / m; k/m is calculated, result is


converted to double
x*y is calculated as double, result is converted to
 n = x * y; int.
Introduction to Programming Languages

Type casting

Explicit Casting
The programmer can deliberately tell the compiler which type to
use.
 int k = 5, m = 4, n;
 double x = 2.5, y = 4.1, z;

 z = (double) k + x; /* 7.5 */
 z = (double) k / (double) m; /* 1.25 */
 n = (int) x * (int) y; /* 8 */

y variable is casted to int


Introduction to Programming Languages

Type Casting

Forced casting types do not change type of the variable, but


the value is temporarily changed to the new type.

int k=5;
printf(".2f\n", (double) k); /* 5.00 */
printf("%d\n",k); /* 5 */

After these lines, k‘s type will still be int and its value is 5.
Introduction to Programming Languages

Operation precedence

Use the PEDMAS rule to determine the


precedences.
Introduction to Programming Languages

Operation precedence

float
 float is a data type which gives more accurate numerical results, but
requires more computation power and memory. Normally, computers
perform integer operations faster.
 Example: float operation

= 1 + 2 * 3 - 4.0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2
Introduction to Programming Languages

Operation precedence

Integers
Expression may look deceiving. Be careful about performing integer
operations.

 Example:
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3 / 5
=0
Introduction to Programming Languages

Operation precedence

Floating points

Example: The result is more accurate.

(1 + 2.0) * (3 - 4) / 5
= ((1 + 2.0) * (3 - 4)) / 5
= (3.0 * -1) / 5
= -3.0 / 5
= -0.6
Introduction to Programming Languages

Operation precedence

Precedences : Extra info

*, /, % have the same precedence.


 +, - have the same precedence.
 For the same precedence operators, the command is
executed from left to right.

2 + 3 – 1 = (2 + 3) – 1 = 4
2 – 3 + 1 = (2 – 3) + 1 = 0
Introduction to Programming Languages

Assignment and operation precedences:

a=0, b=1;
x=(++a==b);
x=1 (true)

x=2>=b;
x=1 (true)
Introduction to Programming Languages

Operator Precedence

Operator Precedence
!+-& highest
* / %
+ -
< <= >= >
== !=
&&
||
= lowest
Introduction to Programming Languages

Comments / Documentation

 Documentation is an integral part of program development process.

 Documentation is aimed to explain how a program flows to those who try


to read your program (which can be yourself).

 starts with /* and continues up to the next */

 Comments within comments are not allowed.


Introduction to Programming Languages

Comments

#include <stdio.h>
/* Ekrandan girilen bir
sayının karekökünü
yazar. */

int main()
{
/*
bir seyler yazalim
/* hatali açıklama */
*/

}
Introduction to Programming Languages

Coding style - indentation

How to write intelligible codes


Introduction to Programming Languages

Coding style - indentation

#include <stdio.h>

#include <stdio.h>
int main() int main(){
int sayi;
{ scanf("%d",&sayi);
int sayi; if(sayi<10) {
printf("az\n"); }
scanf("%d", &sayi); return 0;
}

if( sayi < 10 ) {


printf("az\n");
}

return 0;
}
Introduction to Programming Languages

C - Operators

 Arithmetic Operators ( + , * , - , / ….)


 Relational Operators (>, <, … )
 Logical Operators ( &&, ||, …)
 Bitwise Operators (<< , >>, …)
 Assignment Operators ( =, *=, +=, …)
 Miscellaneous Operators (&, *, …)
Computer Programming

Operators

Logical and arithmetic operators


Computer Programming

Operators

boolean
 Boolean is a type that may have True or
False values.
 They are used for determining a logical
situation.
 Specifically useful for conditional
executions.
 Logical expressions: are formulas that may
evaluate to True or False.
Computer Programming

Operators

Boolean in C

 The boolean type in C is int.


 The value 0  False.
 All other integer values (3, 100, etc.)  True.
Computer Programming

Operators

Example
#include <stdio.h> if ( statement is TRUE )
int main() Execute this line of code
{
int neyse = 0;

if (neyse) { if ( statement ) {
printf(“Doğru mu?\n”); /* Execute these statements if TRUE */
} }
else { else {
printf(“Hayır değil.\n”); /* Execute these statements if FALSE */
} }
return 0;
}
Computer Programming

Operators

Boolean expressions

 These expressions result in 0 or 1.


 In this way, we control their truth.
 If the expression is logically true, its value is 1,
else its value is 0.
Computer Programming

Operators

Boolean operators
 These are used to construct boolean
expressions.
 They are also called logical operators.
And  && > greater than 5 > 4 is TRUE
< less than 4 < 5 is TRUE
Or  || >= greater than or equal 4 >= 4 is TRUE
<= less than or equal 3 <= 4 is TRUE
Not  ! == equal to 5 == 5 is TRUE
Equals  == != not equal to 5 != 4 is TRUE

Not equals  !=
Relational  <, >, <=, >=
Computer Programming

Operators

Boolean operators - And


All values must be different than 0.

1 && 2  TRUE

1 && 0 && -1  FALSE


Computer Programming

Operators

Boolean operators - Or
At least one value must be 1.

1 || 2

1 || 0 || -1
Computer Programming

Operators

Boolean operators - Not


Turns true into false, and false into true.

!1
!0
Computer Programming

Operators

Boolean operators - Equals


Returns true, if both sides are the same.
1 == 2
1 == 0
42 == 42
0 == 0
Computer Programming

Operators

Boolean operators - Not equals


Returns true if both sides are different.

1 != 2
1 != 0
42 != 42
Computer Programming

Operators

Boolean operators - Relational


If the inequality is logically true, returns 1.
1<2
0>1
42 <= 42
yas >= 18
Computer Programming

Operators

Highest
Operator precedence
 Parentheses
 Not (!)
 Relational (<, >, <=, >=)
 Equals (==)
 Not equals (!=)
 And (&&)
 Or (||)
Lowest
Computer Programming

Common Mistakes

 The most common mistake is the careless


construction of expressions without considering
operation precedences.

 Another common mistake is writing a single (=)


instead of a (==).
In this case, the compiler does not give errors, but the
assignment will almost always be TRUE (if the assigned
value is different than 0).
Computer Programming

Common Mistakes, Example

(mistake) (correct)
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int not; int not;

scanf("%d", &not); scanf("%d", &not);

if (not = 48 || not = 49) { if (not == 48 || not == 49) {


printf("Çok yakın!\n"); printf("Çok yakın!\n");
} }
return 0; return 0;
} }
Computer Programming

Common Mistakes, Example

(mistake) (correct)

#include <stdio.h> #include <stdio.h>


int main() int main()

{ {

int not; int not;

scanf("%d", &not); scanf("%d", &not);

if ( 0 < not < 48 ) if ( 0 < not && not < 48 )

{ {

printf("Yanlis\n"); printf("Yanlis\n");

} }

return 0; return 0;

} }

You might also like