Introduction to C

1,176 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,176
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
66
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to C

  1. 1. Introduction
  2. 2. Basic info about the course <ul><li>Aim: To teach basic programming skills </li></ul><ul><li>Course web site: </li></ul><ul><ul><li>http://www.cmpe.boun.edu.tr/courses/cmpe150/fall2008 </li></ul></ul><ul><li>There is no difference between the sections. </li></ul><ul><li>There are some changes in the organization of the course. </li></ul><ul><li>5 hours of class every week </li></ul><ul><ul><li>1 hour lecture and 2 hours PS (lectured by the instructor) </li></ul></ul><ul><ul><li>2 hours lab (taught by an assistant; student assistants will also help the assistant) </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  3. 3. VERY IMPORTANT NOTICE <ul><li>All announcements are made through the CMPE150 e-mail list. The list has been constructed using your e-mail addresses in the Registration system. Make sure that your address in the Registration system is: </li></ul><ul><ul><li>your BOUN address, </li></ul></ul><ul><ul><li>you check it every day, </li></ul></ul><ul><ul><li>its quota is not exceeded and it is functional. </li></ul></ul><ul><ul><li>(We will re-construct the list after the add-drop period is over, so change it now if your Registration e-mail address is not from BOUN.) </li></ul></ul><ul><li>Note that Yahoo, GMail, Hotmail, and similar addresses frequently have problems or the messages may go to the spam folder. </li></ul><ul><li>It is completely your responsibility to make sure that your account receives the messages properly. We will send the announcements via the mailing list and assume you read it the same day. There are more than 350 students in this course and we do not have any additional means to contact you if your e-mail address is not working. </li></ul><ul><li>NO EXCUSES WILL BE ACCEPTED FOR AN ANNOUNCEMENT ALREADY MADE THROUGH THE MAILING LIST. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  4. 4. Basic info about the course <ul><li>You will have </li></ul><ul><ul><li>2 midterms </li></ul></ul><ul><ul><li>1 final </li></ul></ul><ul><ul><li>3 projects </li></ul></ul><ul><ul><li>throughout the semester. </li></ul></ul><ul><li>The projects are NOT mandatory (except for CMPE students) . However, there will be a question closely related with the project in every exam. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  5. 5. Grading <ul><ul><li>Midterm 1 30% </li></ul></ul><ul><ul><li>Midterm 2 30% </li></ul></ul><ul><ul><li>Final 40% </li></ul></ul><ul><li>CMPE students start with -15 points  </li></ul><ul><ul><li>To compensate for this, they have to submit all of their projects. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  6. 6. Exams and labs <ul><li>The exams are in front of the computer. You will use our online compiler in the exams. </li></ul><ul><li>You will use the same online compiler also in the labs. THEREFORE, YOU MAY CONCLUDE THAT YOU WILL NOT DO WELL IN THE EXAMS IF YOU DO NOT ATTEND THE LABS. </li></ul><ul><li>No attendance taken in class or lab. </li></ul><ul><li>However, we STRONGLY recommend that you attend the labs. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  7. 7. Exams and labs <ul><li>You will be assigned a username and password for the online compiler. </li></ul><ul><li>In the exams, you will receive an additional password. It is your responsibility to ensure that no one else sees this password. NO MERCY ABOUT CHEATING. </li></ul><ul><li>We do not increase (or even decrease) your letter grades at the end of the semester. Don’t ask for it. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  8. 8. Course outline <ul><li>1- Introduction, printf, scanf, variables, operators, constants </li></ul><ul><li>2- Data types, assignment type conversions, type casting, post/pre-increment, type casting </li></ul><ul><li>3- If, nested if, logical operators, switch </li></ul><ul><li>4- While, for, do-while </li></ul><ul><li>5- Nested loops, break, continue </li></ul><ul><li>6- Functions, scope, macro-substitution </li></ul><ul><li>7- Pointers, variable parameters (aka. call by reference or pointers as function arguments) </li></ul><ul><li>8- Arrays, passing arrays to functions, sorting and binary search </li></ul><ul><li>9- Strings </li></ul><ul><li>10- Multi-dimensional arrays </li></ul><ul><li>11- Structs </li></ul><ul><li>12- Review </li></ul><ul><li>Note that pointers (except for variable parameters) are not included) in CMPE150 anymore. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  9. 9. Let’s get started ! <ul><li>We will first learn how a computer operates. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  10. 10. A computer system <ul><li>A computer system is composed of: </li></ul><ul><ul><li>a monitor, </li></ul></ul><ul><ul><li>a keyboard, </li></ul></ul><ul><ul><li>a mouse, </li></ul></ul><ul><ul><li>and a case (that contains several controlling components such as processor and alike), </li></ul></ul><ul><ul><li>and also other peripherals like </li></ul></ul><ul><ul><ul><li>CD player (might have been included in the case), </li></ul></ul></ul><ul><ul><ul><li>printer, </li></ul></ul></ul><ul><ul><ul><li>scanner, </li></ul></ul></ul><ul><ul><ul><li>modem, </li></ul></ul></ul><ul><ul><ul><li>etc. </li></ul></ul></ul><ul><li>all connected together </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  11. 11. Input and output devices Fall 2008 CMPE 150 – Introduction to Computing Input Input Input Input Input Output Output /Output /Output
  12. 12. A computer system <ul><li>Note that everything could be packed in a single box, but the concepts are the same. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  13. 13. A computer system <ul><li>Everything we had in the previous slide is hardware . </li></ul><ul><ul><li>i.e., physical components that implement what is requested by the software . </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing HARDWARE OPERATING SYSTEM (Windows, Linux, MacOS, etc.) APPLICATIONS (Eg: Word, Excel, Explorer, MSN, C Compiler, your own programs, etc.) SOFTWARE
  14. 14. A computer system <ul><li>In this course, we will learn how to develop our own software (using C language), but we need to understand how our programs will be executed by the hardware. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  15. 15. CPU: Central Processing Unit <ul><li>In terms of hardware, what is important for us is the CPU. </li></ul><ul><li>It does all processing and control. </li></ul><ul><ul><li>Everything is controlled and executed by the CPU. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  16. 16. CPU: Central Processing Unit Fall 2008 CMPE 150 – Introduction to Computing Control Unit Registers Arithmetic & Logic Unit R1 R2 . . . Rm IR ...
  17. 17. How are the instructions executed? Fall 2008 CMPE 150 – Introduction to Computing Central Processing Unit (CPU) Control Unit Registers Arithmetic & Logic Unit instr 2 instr 3 ... instr n instr 1 Program Main Memory R1 R2 . . . Rm IR ...
  18. 18. How do we write programs? Fall 2008 CMPE 150 – Introduction to Computing We write our programs in &quot;C language&quot; (which is an English-like language) #include <stdio.h> int main() { printf(&quot;Hello world!&quot;); return 0; } We use a compiler (such as GCC, Visual C, Borland C, etc.) to translate our program from &quot;C language&quot; to &quot;machine language&quot; Compile & Link This is the executable code in &quot;machine language.&quot; This is the only thing the computer can understand and run (execute). 1110101011001001010001010100101000010010100101010101000101001000100101001 (source code) (object code) (machine code or executable code)
  19. 19. Statement vs. Instruction <ul><li>Our source code (in C) is composed of statements . </li></ul><ul><ul><li>Eg: a=b+c/2; </li></ul></ul><ul><li>The corresponding machine code is composed of instructions . </li></ul><ul><ul><li>Eg: 1101001010110010 (divide c by 2) </li></ul></ul><ul><ul><li>0110100100100101 (add it to b) </li></ul></ul><ul><ul><li>1010110110111011 (put the result in a) </li></ul></ul><ul><ul><li>CPU is capable of executing instructions, not statements. Statements may be too complex. </li></ul></ul><ul><ul><li>Compiler implements each statement using several instructions. </li></ul></ul><ul><ul><ul><li>Eg: The statement &quot; a=b+c/2; &quot; can be implemented as </li></ul></ul></ul><ul><ul><ul><ul><ul><li>temp1 = c/2 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>a = b + temp1 </li></ul></ul></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  20. 20. Why have input/output? <ul><li>A program should not always produce the same output. </li></ul><ul><ul><li>O/w, you may keep the result and delete the program after you run it for the first time. </li></ul></ul><ul><li>A program should be consistent; i.e., it should not produce random results. </li></ul><ul><li>Therefore, a program should take some input , process it, and produce some output as the result of that input. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  21. 21. Execution of an instruction <ul><li>Let’s see how an instruction like &quot; a=b+2 &quot; is executed. </li></ul><ul><ul><li>Assume initially a is 4 and b is 6 . </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing Central Processing Unit (CPU) Control Unit Registers Arithmetic & Logic Unit ... a=b+2 Main Memory R1 R2 . . . Rm IR ... b 6 6 ... a=b+2 2 8 a 4 Program
  22. 22. Welcome to C Programming Language <ul><li>Now that we have an overall understanding of the computer, we can start writing C programs. </li></ul><ul><li>Remember: </li></ul>Fall 2008 CMPE 150 – Introduction to Computing We write our programs in &quot;C language&quot; (which is an English-like language) #include <stdio.h> int main() { printf(&quot;Hello world!&quot;); return 0; } We use a compiler (such as GCC, Visual C, Borland C, etc.) to translate our program from &quot;C language&quot; to &quot;machine language&quot; Compile & Link This is the executable code in &quot;machine language.&quot; This is the only thing the computer can understand and run (execute). 1110101011001001010001010100101000010010100101010101000101001000100101001 (source code) (object code) (machine code or executable code)
  23. 23. Our first C program: Hello World <ul><li>Every C program has a main() function. </li></ul><ul><ul><li>It wraps all the statements to be executed. </li></ul></ul><ul><li>We make use of previously written functions. They are provided by header files. </li></ul><ul><ul><li>Typically, we include the standard input/output header file, named stdio.h . </li></ul></ul><ul><li>We write all statements inside the main() function. </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Hello world&quot;); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  24. 24. Need for input <ul><li>Note that the Hello World program has no input. </li></ul><ul><ul><li>Therefore, it always produces the same output: </li></ul></ul><ul><ul><li>Hello World </li></ul></ul><ul><ul><li>So, after we run this program once, we know what it will always produce. Therefore, we don’t need the program anymore; we can safely delete it. </li></ul></ul><ul><li>Definitely this is not what we want. (O/w, nobody will pay us  ) </li></ul><ul><ul><li>We want to write programs that can take input and produce different results according to the input. </li></ul></ul><ul><li>(Details of I/O functions will be covered in the labs.) </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  25. 25. A program that also performs input <ul><li>#include <stdio.h> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>int a, b, c; </li></ul><ul><li>printf(&quot;Enter two numbers: &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;, &a, &b); </li></ul><ul><li>c=a+b; </li></ul><ul><li>printf(&quot;Result is %d &quot;, c); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>Enter two numbers: </li></ul><ul><li>Result is 13 </li></ul>Fall 2008 CMPE 150 – Introduction to Computing C Program User screen _ _ _ 5 8 _ Read two integers (decimals) into variables a and b Display the value of variable c after the text &quot;Result is&quot;
  26. 26. Variables <ul><li>Operations (such as addition, subtraction, etc.) operate on operands. </li></ul><ul><li>You need some space to store the value of each operand. </li></ul><ul><li>A variable provides storage space for a value. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  27. 27. Variables <ul><li>IMPORTANT: The value of a variable can never be empty. The value is represented via multiple bits, each of which is either 0 or 1. So, the variable always has a value. </li></ul><ul><li>When a local variable is defined, its initial value is undefined. In other words, it has an arbitrary value. (For the moment, we will not use global variables.) </li></ul><ul><li>So, make sure that the variable has a valid value before you perform any operation based on that value. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  28. 28. Variables <ul><li>Each variable consists of multiple bits. E.g.: </li></ul><ul><li>Thus, every value is actually stored as a sequence of bits (1s and 0s) in the computer. </li></ul><ul><li>The number of bits is called the size of the variable. </li></ul><ul><li>The size of a variable depends on the type of the variable, the hardware, the operating system, and the compiler used. </li></ul><ul><ul><li>So, in your programs NEVER make assumptions about the size of a variable. </li></ul></ul><ul><ul><li>The size may change due to the factors listed above, and your program will not work. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing  2 13 +2 10 +2 9 +2 7 +2 5 +2 4 +2 2 +2 1 +2 0 =9911 0 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  29. 29. Variables <ul><li>#include <stdio.h> </li></ul><ul><li>int main </li></ul><ul><li>{ </li></ul><ul><li>int a, b, c; </li></ul><ul><li>a=10; </li></ul><ul><li>b=3; </li></ul><ul><li>c=a-b; </li></ul><ul><li>a=b+2; </li></ul><ul><li>} </li></ul>Fall 2008 CMPE 150 – Introduction to Computing Program 10 7 3 5 a ... b ... c ...
  30. 30. Rules for identifier names <ul><li>While defining names for variables (and also functions, user-defined types, and constants in the future) you should obey the following rules: </li></ul><ul><ul><li>The first character of a name must be a letter or underscore (‘_’). </li></ul></ul><ul><ul><li>The remaining characters must be letters, digits, or underscore. </li></ul></ul><ul><ul><li>Only the first 31 characters are significant. </li></ul></ul><ul><ul><li>Avoid reserved words such as int, float, char, etc. as identifier names. </li></ul></ul><ul><li>However, it is better to avoid starting identifier names with underscore. </li></ul><ul><li>Also remember that C language is case-sensitive. </li></ul><ul><li>It is a very good practice to use meaningful names. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  31. 31. Rules for identifier names <ul><li>Valid: </li></ul><ul><ul><ul><li>a, a1, count, no_of_students, B56, b_56 </li></ul></ul></ul><ul><li>Invalid: </li></ul><ul><ul><ul><li>1a, sayı, int, $100 </li></ul></ul></ul><ul><li>Valid but not recommended: </li></ul><ul><ul><ul><li>_b56, Tuna, FB, GS, BJK, I_dont_remember_what_this_variable_means, a_very_very_long_identifier_name_1, a_very_very_long_identifier_name_2 </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  32. 32. Standard data types <ul><li>You have to specify the type of a variable when you define it. </li></ul><ul><li>There are three standard data types: </li></ul><ul><ul><li>Integer (i.e., whole numbers) </li></ul></ul><ul><ul><li>Float (i.e., real or floating-point numbers) </li></ul></ul><ul><ul><li>Characters </li></ul></ul><ul><li>We will discuss user-defined types later in the course. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  33. 33. Integers <ul><li>Syntax: </li></ul><ul><ul><ul><li>int variable_list; </li></ul></ul></ul><ul><ul><li>where variable_list is a comma-separated list of variable names. Each variable name may be followed by an optional assignment operator and a value for initialization. </li></ul></ul><ul><ul><li>Eg: int a, b=10, c; </li></ul></ul><ul><li>Integer is a class of variable types. The most basic one is int . </li></ul><ul><li>The size may change, but the leftmost bit is used for the sign. The remaining bits represent the value in binary. </li></ul><ul><li>Though the size of an int variable may vary, it is always limited, i.e., it contains a limited number of bits. Therefore, the maximum and minimum values that can be represented by an int variable is limited. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  34. 34. Integers <ul><li>For example, assume in your system an integer has 16 bits. </li></ul><ul><li>Leftmost bit is used for the sign, so 15 bits are left for the value. So, you have 2 15 =32,768 positive values, ranging from 0 to 32,767. Similarly, you have 32,768 negative values, this time ranging from -1 to -32,768. </li></ul><ul><li>If you have 32 bits (4 bytes) for an integer, than the maximum value is 2 31 =2,147,483,647. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing sign bit value 0 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1
  35. 35. Integers <ul><li>There are variations of int such as long int , short int , unsigned int . </li></ul><ul><ul><li>For each one of these types, you may ignore the word &quot;int&quot; and use long , short , and unsigned , respectively. </li></ul></ul><ul><li>The sizes of these types are ordered as follows: </li></ul><ul><ul><ul><li>short int ≤ int ≤ long int </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  36. 36. Floating-point numbers <ul><li>Syntax: </li></ul><ul><ul><ul><li>float variable_list; </li></ul></ul></ul><ul><li>Float type is used for real numbers. </li></ul><ul><li>Note that all integers may be represented as floating-point numbers, but not vice versa. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  37. 37. Floating-point numbers <ul><li>Similar to integers, floats also have their limits: maximum and minimum values are limited as well as the precision. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing Lower limit Upper limit The value you want to store Due to loss of precision, what you actually store might be this , or this
  38. 38. Floating-point numbers <ul><li>There are two variations of float : double and long double . </li></ul><ul><ul><li>They have wider range and higher precision. </li></ul></ul><ul><li>The sizes of these types are ordered as follows: </li></ul><ul><ul><ul><li>float ≤ double ≤ long double </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  39. 39. Characters <ul><li>Syntax: </li></ul><ul><ul><ul><li>char variable_list; </li></ul></ul></ul><ul><li>Character is the only type that has a fixed size in all implementations: 1 byte. </li></ul><ul><li>All letters (uppercase and lowercase, separately), digits, and signs (such as +,-,!,?,$,£,^,#, comma itself, and many others) are of type character. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  40. 40. Characters <ul><li>Since every value is represented with bits (0s and 1s), we need a mapping for all these letters, digits, and signs. </li></ul><ul><li>This mapping is provided by a table of characters and their corresponding integer values. </li></ul><ul><ul><li>The most widely used table for this purpose is the ASCII table. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  41. 41. Characters <ul><li>The ASCII table contains the values for 256 values (of which only the first 128 are relevant for you). Each row of the table contains one character. The row number is called the ASCII code of the corresponding character. </li></ul><ul><ul><li>(The topic of character encoding is beyond the scope of this course. So, we will work with the simplified definition here.) </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  42. 42. Characters <ul><li>Never memorize the ASCII codes. They are available in all programming books and the Internet. (Eg: http://www.ascii-code.com ) </li></ul><ul><li>What is important for us is the following three rules: </li></ul><ul><ul><li>All lowercase letters (a,b,c,...) are consecutive. </li></ul></ul><ul><ul><li>All uppercase letters (A,B,C,...) are consecutive. </li></ul></ul><ul><ul><li>All digits are consecutive. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  43. 43. ASCII table (partial) Fall 2008 CMPE 150 – Introduction to Computing ASCII code Symbol ASCII code Symbol ASCII code Symbol ASCII code Symbol ... ... 66 B 84 T 107 k 32 blank 67 C 85 U 108 l 37 % 68 D 86 V 109 m 42 * 69 E 87 W 110 n 43 + 70 F 88 X 111 o ... ... 71 G 89 Y 112 p 48 0 72 H 90 Z 113 q 49 1 73 I ... ... 114 r 50 2 74 J 97 a 115 s 51 3 75 K 98 b 116 t 52 4 76 L 99 c 117 u 53 5 77 M 100 d 118 v 54 6 78 N 101 e 119 w 55 7 79 O 102 f 120 x 56 8 80 P 103 g 121 y 57 9 81 Q 104 h 122 z ... ... 82 R 105 i ... ... 65 A 83 S 106 j    
  44. 44. Characters <ul><li>A character variable actually stores the ASCII value of the corresponding letter, digit, or sign. </li></ul><ul><li>I/O functions (printf(), scanf(), etc.) do the translation between the image of a character displayed on the screen and the ASCII code that is actually stored in the memory of the computer. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  45. 45. Characters <ul><li>Note that a and A have different ASCII codes ( 97 and 65 ). </li></ul><ul><li>You could also have a variable with name a . To differentiate between the variable and the character, we specify all characters in single quotes, such as ‘a’ . Variable names are never given in quotes. </li></ul><ul><ul><li>Example: char ch; </li></ul></ul><ul><ul><ul><li>ch=‘a’; </li></ul></ul></ul><ul><li>Note that using double quotes makes it a string (to be discussed later in the course) rather than a character. Thus, ‘a’ and &quot;a&quot; are different. </li></ul><ul><li>Similarly, 1 and ‘1’ are different. Former has the value 1 whereas the latter has the ASCII value of 49 . </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  46. 46. Characters <ul><li>Example: Consider the code segment below. </li></ul><ul><ul><ul><li>char ch; </li></ul></ul></ul><ul><ul><ul><li>ch=‘A’; </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Output is %c&quot;, ch); </li></ul></ul></ul><ul><li>The string in printf() is stored as </li></ul><ul><ul><ul><li>79,117,116,112,117,116,32,105,115,32,37,99 </li></ul></ul></ul><ul><ul><li>which are the ASCII codes of the characters in the string. </li></ul></ul><ul><li>When printf() is executed, it first replaces 37,99 ( %c ) with 65 ( A ), and then displays the corresponding characters on the screen. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  47. 47. Constants <ul><li>Syntax: </li></ul><ul><ul><ul><li>#define constant_name constant_value </li></ul></ul></ul><ul><li>As the name implies, variables store values that vary while constants represent fixed values. </li></ul><ul><li>Note that there is no storage when you use constants. Actually, when you compiler your program, the compiler replaces the constant name with the value you defined. </li></ul><ul><li>The pre-processor replaces every occurrence of constant_name with everything that is to the right of constant_name in the definition. </li></ul><ul><ul><li>Note that there is no semicolon at the end of the definition. </li></ul></ul><ul><li>Conventionally, we use names in uppercase for constants. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  48. 48. Enumerated type <ul><li>Used to define your own types. </li></ul><ul><li>Syntax: </li></ul><ul><ul><ul><li>enum type_name { </li></ul></ul></ul><ul><ul><ul><li>item_name =constant_int_value , ... </li></ul></ul></ul><ul><ul><ul><li>} variable_list ; </li></ul></ul></ul><ul><li>By default, the value of the first item is 0, and it increases by one for consecutive items. However, you may change the default value by specifying the constant value explicitly. </li></ul><ul><li>Eg: enum boolean {FALSE,TRUE} v1, v2; </li></ul><ul><ul><ul><li>enum days {SUN,MON,TUE,WED,THU,FRI,SAT}; </li></ul></ul></ul><ul><ul><ul><li>enum {one=1,five=5,six,seven,ten=10,eleven} num; </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing Text in green is optional
  49. 49. Operators <ul><li>We will cover the most basic operators in class. More operators will be covered in the labs. </li></ul><ul><li>Assignment operator (=) </li></ul><ul><ul><li>Note that this is not the &quot;equals&quot; operator. It should be pronounced as &quot;becomes.&quot; (Equals is another operator.) </li></ul></ul><ul><ul><li>The value of the expression on the RHS is assigned (copied) to the LHS. </li></ul></ul><ul><ul><li>It has right-to-left associativity. </li></ul></ul><ul><ul><ul><ul><li>a=b=c=10; </li></ul></ul></ul></ul><ul><ul><ul><li>makes all three variables 10 . </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  50. 50. Assignment and type conversion <ul><li>When a variable of a narrower type is assigned to a variable of wider type, no problem. </li></ul><ul><ul><li>Eg: int a=10; float f; </li></ul></ul><ul><ul><ul><ul><ul><li>f=a; </li></ul></ul></ul></ul></ul><ul><li>However, there is loss of information in reverse direction. </li></ul><ul><ul><li>Eg: float f=10.9; int a; </li></ul></ul><ul><ul><ul><ul><ul><li>a=f; </li></ul></ul></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  51. 51. Operators <ul><li>Arithmetic operators (+,-,*,/,%) </li></ul><ul><ul><li>General meanings are obvious. </li></ul></ul><ul><ul><li>What is important is the following: If one of the operands is of a wider type, the result is also of that type. (Its importance will be more obvious soon.) </li></ul></ul><ul><ul><ul><li>Eg: Result of int+float is float. Result of float+double is double. </li></ul></ul></ul><ul><ul><li>In C language, there are two types of division: integer division and float division. </li></ul></ul><ul><ul><ul><li>If both operands are of integer class, we perform integer division and the result is obtained by truncating the decimal part. </li></ul></ul></ul><ul><ul><ul><ul><li>Eg: 8/3 is 2 , not 2.666667 . </li></ul></ul></ul></ul><ul><ul><ul><li>If one of the operands is of float class, the result is float. </li></ul></ul></ul><ul><ul><ul><ul><li>Eg: 8.0/3 or 8/3.0 or 8.0/3.0 is 2.666667 , not 2 . </li></ul></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  52. 52. Operators <ul><ul><li>Remainder operator is % . Both operands must be of integer class. </li></ul></ul><ul><ul><ul><li>Eg: 10%6 is 4 (equivalent to 10 mod 6 ) </li></ul></ul></ul><ul><li>+,-,*,/,% have left-to-right associativity. That means a/b/c is equivalent to (a/b)/c , but not a/(b/c) . </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  53. 53. Operators <ul><li>Logic operators (&&, ||, !) </li></ul><ul><ul><li>Logic operators take integer class operands. </li></ul></ul><ul><ul><ul><li>Zero means false. </li></ul></ul></ul><ul><ul><ul><li>Anything non-zero means true. </li></ul></ul></ul><ul><ul><li>&quot; && &quot; does a logical-AND operation. (True only if both operands are true.) </li></ul></ul><ul><ul><li>&quot; || &quot; does a logical-OR operation. (False only if both operands are false.) </li></ul></ul><ul><ul><li>&quot; ! &quot; does a negation operation. (Converts true to false, and false to true.) </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  54. 54. Operators Fall 2008 CMPE 150 – Introduction to Computing <ul><li>Logic operators follow the logic rules </li></ul><ul><li>The order of evaluation is from left to right </li></ul><ul><li>As usual parenthesis overrides default order </li></ul>a b a && b a || b true true true true true false false true false true false true false false false false
  55. 55. Operators <ul><ul><li>If the first operand of the &quot; && &quot; operator is false, the second operand is not evaluated at all (since it is obvious that the whole expression is false). </li></ul></ul><ul><ul><ul><li>Eg: In the expression below, if the values of b and c are initially 0 and 1 , respectively, </li></ul></ul></ul><ul><ul><ul><li> a = b && (c=2) </li></ul></ul></ul><ul><ul><ul><li>then the second operand is not evaluated at all, so c keeps its value as 1 . </li></ul></ul></ul><ul><ul><li>Similarly, if the first operand of the &quot; || &quot; operator is true, the second operand is not evaluated at all. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  56. 56. Operators <ul><li>Bitwise operators (&, |, ^, <<, >>, ~) </li></ul><ul><ul><li>Bitwise operators take integer class operands. </li></ul></ul><ul><ul><ul><li>For the logic operators, the variable represents a single logical value, true or false. </li></ul></ul></ul><ul><ul><ul><li>For the bitwise operators, each bit of the variable represents true or false. </li></ul></ul></ul><ul><ul><li>& , | , and ^ perform bitwise-AND, -OR, -XOR, respectively. </li></ul></ul><ul><ul><li><< and >> perform left- and right-shifts. </li></ul></ul><ul><ul><li>&quot; ~ &quot; takes bitwise one’s complement. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  57. 57. Operators Fall 2008 CMPE 150 – Introduction to Computing Operation Result 5 & 10 (0000 0101 & 0000 1010) 0 (0000 0000) 5 && 10 (0000 0101 && 0000 1010) 1 (0000 0001) 5 | 10 (0000 0101 | 0000 1010) 15 (0000 1111) 8 ^ 10 (0000 0111 ^ 0000 1010) 13 (0000 1101) 7 << 2 (0000 0111 << 0000 0010) 28 (0001 1100) 7 >> 2 (0000 0111 >> 0000 0010) 1 (0000 0001) ~5 (~0000 0101) -6 (in two’s complement) (1111 1010)
  58. 58. Operators <ul><li>Other assignment operators (+=, -=, *=, /=, %=) </li></ul><ul><ul><li>Instead of writing a=a+b , you can write a+=b in short. Similar with -= , *= , /= , and others. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  59. 59. Operators <ul><li>Pre/Post increment/decrement operators (++, --) </li></ul><ul><ul><li>The operator ++ increments the value of the operand by 1. </li></ul></ul><ul><ul><ul><li>If the operator comes BEFORE the variable name, the value of the variable is incremented before being used, i.e., the value of the expression is the incremented value. This is pre-increment. </li></ul></ul></ul><ul><ul><ul><li>In post-increment, the operator is used after the variable name, and incrementation is performed after the value is used, i.e., the value of the expression is the value of the variable before incrementation. </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  60. 60. Operators <ul><ul><li>Eg: a=10; c=10, </li></ul></ul><ul><ul><li>b=++a; d=c++; </li></ul></ul><ul><ul><ul><li>Both a and c will be come 11 , but b will be 11 while d is 10 . </li></ul></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  61. 61. Operators <ul><li>Comparison operators (==,!=,<,<=,...) </li></ul><ul><ul><li>&quot; == &quot; is the &quot;is equal to&quot; operator. Like all other comparison operators, it evaluates to a Boolean value of true or false, no matter what the operand types are. </li></ul></ul><ul><ul><li>IMPORTANT: When you compare two float values that are supposed to be equal mathematically, the comparison may fail due to the loss of precision discussed before. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  62. 62. Operators Fall 2008 CMPE 150 – Introduction to Computing Symbol Usage Meaning == x == y is x equal to y? != x != y is x not equal to y? > x > y is x greater than y? < x < y is x less than y? >= x >= y is x greater than or equal to y? <= x <=y is x less than or equal to y?
  63. 63. Operators <ul><li>We can create complex expressions by joining several expressions with logic operators. </li></ul>Fall 2008 CMPE 150 – Introduction to Computing Symbol Usage Meaning && exp1 && exp2 AND || exp1 || exp2 OR ! ! exp NOT
  64. 64. Operators <ul><li>While using multiple operators in the same expression, you should be careful with the precedence and associativity of the operands. </li></ul><ul><ul><li>Eg: The following does NOT check if a is between 5 and 10 . </li></ul></ul><ul><ul><ul><ul><li>bool = 5<a<10; </li></ul></ul></ul></ul><ul><ul><ul><li>bool will be true if a is 20 . (Why?) </li></ul></ul></ul><ul><ul><li>Don’t hesitate to use parentheses when you are not sure about the precedence (or to make things explicit). </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  65. 65. Operator precedence table Fall 2008 CMPE 150 – Introduction to Computing Operator Associativity () [] . -> left-to-right ++  -- +  - !  ~ (type) * & sizeof   right-to-left *  /  % left-to-right +  - left-to-right <<  >> left-to-right <  <= >  >= left-to-right ==  != left-to-right & left-to-right ^ left-to-right | left-to-right && left-to-right || left-to-right ?: right-to-left = +=  -= *=  /= %=  &= ^=  |= <<=  >>= right-to-left , left-to-right
  66. 66. Operators <ul><li>Precedence, associativity, and order of evaluation: </li></ul><ul><ul><li>In the table is given in the previous slide, precedence decreases as you go down. </li></ul></ul><ul><ul><li>If two operands in an expression have the same precedence, you decide according to the associativity column. </li></ul></ul><ul><ul><li>There is a common misunderstanding about associativity. </li></ul></ul><ul><ul><ul><li>Note that associativity has nothing to do with the order of evaluation of the operands. </li></ul></ul></ul><ul><ul><ul><li>Order of evaluation of operands is not specified in C language. </li></ul></ul></ul><ul><ul><li>It is strongly recommended that you read and understand Section 2.12 (pp. 52-54) in the book by Kernighan & Ritchie. </li></ul></ul>Fall 2008 CMPE 150 – Introduction to Computing
  67. 67. Type casting <ul><li>Also called coersion or type conversion . </li></ul><ul><li>It does NOT change the type of a variable. It is not possible to change the type of a variable. </li></ul><ul><li>What casting does is to convert the type of a value . </li></ul>Fall 2008 CMPE 150 – Introduction to Computing
  68. 68. Type casting <ul><li>Eg: int a=10, b=3; </li></ul><ul><li>float f, g; </li></ul><ul><li>f=a/b; </li></ul><ul><li>g=(float)a/b; </li></ul><ul><li>The type of a does not change; it is still and integer. However, in the expression (float)a/b , the value of a , which is 10 , is converted to float value of 10.0 , and then it is divided by b , which is 3 . Thus, we perform float division and g becomes 3.3333... </li></ul><ul><li>On the other hand, we perform an integer division for f , so it becomes 3 . </li></ul>Fall 2008 CMPE 150 – Introduction to Computing

×