3 th Week Variables, constant, and expressions Hazırlayan Asst. Prof. Dr. Tansu Filik
Previously on Bil 200 Unitary operations (applied to single variable, +.-,++,--) Type casting Operation/Operator precedence Comments/Documentation Formatted writing/output
Formats ( printf & scanf ) int b = 756; printf( %d, b); printf ( %9d\n, b);
Formats (int)
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);
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); This statement outputs four spaces and then 12.45. Those four spaces plus 12.45 (five characters total) equal the 9 in the width. Only two values are shown to the right of the decimal because.2 is used in the %f conversion character. ----12.45
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.
Formatted input Examples float a; int b; scanf("%d %f", &b, &a); float a; long int b; scanf("%4ld %4.2f", &b, &a);
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);
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 2 byte Reading from keyboard (Assume 10 is entered) 0 A
Components of a C program Types, Variables, Keywords, Assignments, Constants
Components of a C program: Types C language has 4 basic types: char, int, float, double These types may be extended by type variations: long, short, const Users may define custom types as arrays and structures. What is the type of string? Actually strings are arrays of characters.
Components of a C program: Types Char Characters are stored as small integer numbers. Each character has a corresponding numerical value. These are known as ASCII codes. For example, the numerical value of character A is 65. ASCII: American Standard Code for Information Interchange
Components of a C program: Types Char ASCII values are between 0 and 127. Value 0 is special, and indicated as'\0' or NULL. Value 127 corresponds to DEL There are several other non printable or printable special characters: '\n' '\t' '\'' '\\' etc. Characters with values over 127 are known as extended character set. They are not standard, and may vary according to the platform.
ASCII codes
Components of a C program: Types Char - Example Numerical value can be assigned to characters: char ch; ch = 65; Letters can also be assigned to characters. Use single quotes for letters. The following lines do exactly the same as the above line codes. char ch; ch = 'A';
Components of a C program: Types char 1 bytes -128 to 127 unsigned char 1 bytes 0 to 255 short int 2 bytes -32768 to 32767 unsigned short int 2 bytes 0 to 65535 int 4 bytes -2147483648 to 2147483647 unsigned int 4 bytes 0 to 4294967295 long int 4 bytes -2147483648 to 2147483647 unsigned long int 4 bytes 0 to 4294967295 float 4 bytes 1.175494e-38 to 3.402823e+38 double 8 bytes 2.225074e-308 to 1.797693e+308 precision of float 6 digits precision of double 15 digits
Numerical Values 0, 1, 2, -100 Integer 0.0, 1e25, -0.01e-12 Floating point 0x10, 0xca, 0xdeadbeef 16-hex 010, 036, -0100 8-octal 123456789ul unsigned long int 'A', ' ', '&', '\'' Character "A", "Buffy", "@#*!" String '\101', '\x41' oct/hex 'A' "\101\x42" AB '\013', '\xb' BELL - beep '\007', '\x7' VTAB vert. tab
Components of a C program: Types Examples int i; float sonuc; int i = 0; char ilk = 'K'; float a = 0.0; double db32 = 1e1; long int li; long float son; short int si = 0; unsigned char ilk = 'K'; long double ldb = 1e-3;
Components of a C program: Keywords int i; float sonuc; int i = 0; char ilk = 'K'; float a = 0.0; double db32 = 1e1; ------------------------------------------------------------------------------------------------------ long int li; long float son; short int si = 0; unsigned char ilk = 'K'; long double ldb = 1e-3;
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_
Components of a C program: Assignments Gives a value to a variable. Assignment operator is =
Examples float x = 2.5 ; Components of a C program: Assignments char ch ; int sayi ; ch = '\n' ; sayi = 4 + 5 ; /* now the value is 9 */ sayi = sayi * 2; /* now the value is 18. */
Components of a C program: Assignments Examples: #include <stdio.h> /* assignments.*/ int main() { int tamsayi; char karakter; float kayarnokta; tamsayi = 33; karakter = 33; kayarnokta = 33; tamsayi = 'A'; karakter = 'A'; kayarnokta = 'A'; tamsayi = 33.33; karakter = 33.33; kayarnokta = 33.33; tamsayi = kayarnokta; kayarnokta = tamsayi; return 0; }
Components of a C program: Assignments Examples: int tamsayi; char karakter; float kayarnokta; float ayarnokta; printf("tamsayi degeri %d \n", tamsayi); printf("karakter degeri %c \n", karakter); printf("kayarnokta degeri %f \n",kayarnokta); printf("ayarnokta degeri %f \n", ayarnokta); tamsayi = 33; karakter = 33; kayarnokta = 33;
Components of a C program: Assignments Examples: tamsayi degeri 33 karakter degeri! kayarnokta degeri 33.000000 ayarnokta degeri -107374176.000000
C Programming Language: Example Print "Dereceyi Gir:" Read derece #include <stdio.h> /*Convert degrees to radians */ const float PI = 3.1415926; int main() { float derece; float radyan; Write explanatory comments in your program Global variable Local variables Variable declaration: must defined before using radyan = PI / 180 * derece Print radyan printf("dereceyi gir:"); scanf("%f", &derece); radyan = PI/180*derece; printf("%f\n", radyan); return 0; } Formatted printing is done by printf(). It is defined inside stdio.h
Operators Logical and arithmetic 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.
Operators Boolean in C The boolean type in C is int. The value 0 corresponds to False. All other integer values correspond to True.
Operators Example #include <stdio.h> int main() { int neyse = 0; } if (neyse) { printf( Doğru mu?\n ); } else { printf( Hayır değil.\n ); } return 0;
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.
Operators Boolean operators These are used to construct boolean expressions. They are also called logical operators. And && Or Not! Equals == Not equals!= Relational <, >, <=, >=
Operators Boolean operators - And All values must be different than 0. 1 && 2 1 && 0 && -1
Operators Boolean operators - Or At least one value must be 1. 1 2 1 0-1
Operators Boolean operators - Not Turns true into false, and false into true.!1!0
Operators Boolean operators - Equals Returns true, if both sides are the same. 1 == 2 1 == 0 42 == 42 0 == 0
Operators Boolean operators - Not equals Returns true if both sides are different. 1!= 2 1!= 0 42!= 42
Operators Boolean operators - Relational If the inequality is logically true, returns 1. 1 < 2 0 > 1 42 <= 42 yas >= 18
Highest Lowest Operator precedence Parentheses Not (!) Relational (<, >, <=, >=) Equals (==) Not equals (!=) And (&&) Or ( ) Operators
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).
Common Mistakes, Example (mistake) #include <stdio.h> int main() { int not; (correct) #include <stdio.h> int main() { int not; scanf("%d", ¬); scanf("%d", ¬); } if (not = 48 not = 49) { printf("çok yakın!\n"); } return 0; } if (not == 48 not == 49) { printf("çok yakın!\n"); } return 0;
Common Mistakes, Example (mistake) #include <stdio.h> int main() { int not; (correct) #include <stdio.h> int main() { int not; scanf("%d", ¬); scanf("%d", ¬); } if ( 0 < not < 48 ) { } printf("yanlis\n"); return 0; } if ( 0 < not && not < 48 ) { } printf("yanlis\n"); return 0;
Control structures if-else mechanism
Control structures: if-else mechanism Single selection Structure is: if ( expression ) { commands; } or if ( expression ) command;
Example #include <stdio.h> Control structures: if-else mechanism void main ( ) { int a = 1, b = 2, c ; } if (a > b) { } else { } c = a; c = b;
if else if else Control structures: if-else mechanism The condition structure selects a group of commands to be executed according to the logical value of a boolean statement. The logical statements return true or false values. If the statement is TRUE, the if part of the commands are executed. Otherwise, the else part gets executed.
if else if else Control structures: if-else mechanism A simple if statement is called a singular selection. It either works a command or not, according to whether the logical expression is true or not. A statement with both if and else is a double selection. It executes a separate list of commands when the logical statement is false. Nested lists of if/else statements may control more than two situations.
if else if else Control structures: if-else mechanism if has a general form: if (condition1){ } statement ; else if (condition2) { statement ; } else if (condition3) { } else { } statement ; statemens ;