Bil200 Week 3
Bil200 Week 3
● Data Types in C
● I/O Functions in C
● Unitary Operations
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
int b = 756;
printf(“%d”, b);
Formats (int)
Computer Programming
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
%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:
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
int num;
scanf(“%d”,&num);
int a, b;
scanf(“%d %d”, &a, &b);
Computer Programming
0
2 byte
Reading A
from
keyboard
(Assume
10 is
entered)
Computer Programming
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
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
Examples:
tamSayi degeri 33
karakter degeri !
kayarNokta degeri 33.000000
kayarNokta degeri -107374176.000000
Computer Programming
radyan = PI/180*derece;
printf("%f\n", radyan);
return 0;
}
Introduction to Programming Languages
Type casting
Type casting
Type casting
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.
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 */
Type Casting
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
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
(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
2 + 3 – 1 = (2 + 3) – 1 = 4
2 – 3 + 1 = (2 – 3) + 1 = 0
Introduction to Programming Languages
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
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
#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;
}
return 0;
}
Introduction to Programming Languages
C - Operators
Operators
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
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
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
1 && 2 TRUE
Operators
Boolean operators - Or
At least one value must be 1.
1 || 2
1 || 0 || -1
Computer Programming
Operators
!1
!0
Computer Programming
Operators
Operators
1 != 2
1 != 0
42 != 42
Computer Programming
Operators
Operators
Highest
Operator precedence
Parentheses
Not (!)
Relational (<, >, <=, >=)
Equals (==)
Not equals (!=)
And (&&)
Or (||)
Lowest
Computer Programming
Common Mistakes
(mistake) (correct)
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int not; int not;
(mistake) (correct)
{ {
{ {
printf("Yanlis\n"); printf("Yanlis\n");
} }
return 0; return 0;
} }