Presentation is loading. Please wait.

Presentation is loading. Please wait.

"Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Similar presentations


Presentation on theme: ""Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password."— Presentation transcript:

1 "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months." — Clifford Stoll — Clifford Stoll Monday 03, 2008

2 Introduction to C Programming Lecture 3

3 Today’s Lecture Include Data Types Data Types Arithmetic Operations Arithmetic Operations Tokens Tokens Numeric Constants Numeric Constants Punctuations Punctuations Operators Operators Keywords of C Keywords of C

4 Data Types 1. int 2. short 3. long 4. float 5. double 6. char

5 Arithmetic operators Plus+ Minus- Multiply* Divide/ Modulus%

6 Arithmetic operators i + j x * y a / b a % b

7 % = Remainder 5 % 2 = 1 2 % 2 = 0

8 4 / 2 = 2 5 / 2 = ?

9 Precedence Highest:( ) Highest:( ) Next:*, /, % Next:*, /, % Lowest:+, - Lowest:+, -

10 Another C++ Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: 123-45-6789 ** Section: 0304 ** E-mail: jstudent22@umbc.edu.pk ** ** This program prompts the user for two integer values then displays ** their product. ** ***********************************************/

11 Another C Program (con’t) #include main( ) { int value1, value2, product ; cout<<“Enter two integer values: ; cin>>value1 cin>>value2; //cin>>value1>>value2 product = value1 * value2 ; cout<<“Product = ”<<product ; return 0 ; }

12 Tokens The smallest element in the C language is the token. The smallest element in the C language is the token. It may be a single character or a sequence of characters to form a single item. It may be a single character or a sequence of characters to form a single item.

13 Tokens are: Tokens can be: Tokens can be: –Numeric constants –Character constants –String constants –Keywords –Names (identifiers) –Punctuation –Operators

14 Numeric Constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: Examples: –123 –98.6 –1000000

15 Character Constants Singular! Singular! One character defined character set. One character defined character set. Surrounded on the single quotation mark. Surrounded on the single quotation mark. Examples: Examples: –‘A’ –‘a’ –‘$’ –‘4’

16 String Constants A sequence characters surrounded by double quotation marks. A sequence characters surrounded by double quotation marks. Considered a single item. Considered a single item. Examples: Examples: –“UMBC” –“I like ice cream.” –“123” –“CAR” –“car”

17 Keywords Sometimes called reserved words. Sometimes called reserved words. Are defined as a part of the C language. Are defined as a part of the C language. Can not be used for anything else! Can not be used for anything else! Examples: Examples: –int –while –for

18 Names Sometimes called identifiers. Sometimes called identifiers. Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: Are case sensitive: –abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores. Must begin with a letter and the rest can be letters, digits, and underscores. Must follow the standards for this course! Must follow the standards for this course!

19 Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; :, ‘ “ [ ] { } ( ) ; :, ‘ “ [ ] { } ( )

20 Operators There are operators for: There are operators for: –assignments –mathematical operations –relational operations –Boolean operations –bitwise operations –shifting values –calling functions –subscripting –obtaining the size of an object –obtaining the address of an object –referencing an object through its address –choosing between alternate subexpressions

21 Key Words of C main main if if else else while while do do for for

22 x = 2 + 4 ; = 6 ; = 6 ; Memory x 6

23 Memory x = a + b ; ab x

24 #include <iostream.h> main ( ) { int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; int TotalAge ; int AverageAge ; cout << “ Please enter the age of student 1: “ ; cin >> age1 ; cout << “ Please enter the age of student 2: “ ; cin >> age2 ; : : TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ; AverageAge = TotalAge / 10 ; cout<< “The average age of the class is :” << AverageAge ; }

25 Quadratic Equation In algebra y = ax 2 ax 2 + bx + c In C y = a * x * x + b * x + c

26 a*b%c +d

27 a*(b%c) = a*b%c ?

28 Discriminant b2 - 2a = b*b - 4*a*c /2 *a Incorrect answer Solution = (b*b - 4*a*c) /(2 *a) Correct answer 4c

29 No expression on the left hand side of the assignment Integer division truncates fractional part Liberal use of brackets/parenthesis

30 Interesting Problem Given a four-digit integer, separate and print the digits on the screen Given a four-digit integer, separate and print the digits on the screen

31 Analysis Number = 1234 Number = 1234 Take the remainder of the above number after dividing by 10 Take the remainder of the above number after dividing by 10 Eg 1234 / 10 gives remainder 4 1234 % 10 = 4 Remove last digit Remove last digit –1234/10 = 123.4 –123(Truncation due to Integer Division) 123 %10 gives 3 123 %10 gives 3 Remove last digit Remove last digit –123/10 = 12.3 –12 (Truncation due to Integer Division) 12 % 10 gives remainder 2 12 % 10 gives remainder 2 Remove last digit Remove last digit –12/10 = 1.2 –1 (Truncation due to Integer Division) Final digit remains Final digit remains

32 Code #include #include main ( ) { int number; int digit; cout << “Please enter a 4 digit integer : ”; cin >> number; digit = number %10; cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’ number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit; }


Download ppt ""Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password."

Similar presentations


Ads by Google