Basics of Data Classes in Programming

com101b lecture 2 classes of data n.w
1 / 24
Embed
Share

Learn about the two classes of data in programming: variables and constants. Explore the concept of variables, their declarations, rules for naming them, and how to assign initial values. Understand the importance of following naming conventions and avoiding reserved keywords in programming languages. References from the book "Programming in ANSI C" by Kumar & Agrawal (1992).

  • Data Classes
  • Programming Basics
  • Variable Declaration
  • Naming Conventions
  • Programming Concepts

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COM101B LECTURE 2: CLASSES OF DATA ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  2. A computer program manipulates two kinds of data: VARIABLES and CONSTANTS REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  3. VARIABLES A variable is an entity used by a program to store values used in the computation Every variable in C has a type and it MUST be declared before it is used. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  4. DECLARATIONS A declaration consists of a type name followed by a list of one or more variables of that type, separated by commas: int mint; char cherry; double trouble; float x, y; float x,y; can equivalently be written as: float x; float y; It is possible to assign an initial value to a variable in its declaration: float x = 1.0, y = 2.4, increment = 0.1; int a = 5; REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  5. RULES FOR NAMING A VARIABLE A variable name must begin with a letter or underscore, and may be made up of any combination of letters, underscores, or digits 0-9. Whitespace charecters are not permitted within a name. Some valid names: foo, r2d2, Agent707, ANSI_C, _myvar Some invalid names: 4ever: begining character is not a letter or an underscore, x2.5 : dot is not allowed in a var. name ANSI C: space is not allowed. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  6. RULES FOR NAMING A VARIABLE A variable name may use uppercase letters, lowercase letters or both. The language is case sensitive. Interest, interest, INTEREST, IntEreST : are all treated as different variable names REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  7. RULES FOR NAMING A VARIABLE C reserves certain names, called keywords, for specific meanings; hence they can not be used as variable names. The 32 standard keywords are: auto break case char const continuedefault do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union signed void volatile while REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  8. IDENTIFIERS Technically, a variable name is an identifier Identifiers are names given to various program entities Examples of identifiers other than variables: function names, enumaration constants, symbolic constants etc. The rules given for variable naming, applies to all identifiers. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  9. CONSTANTS A constant is an entity whose value does not change during program execution. Constants are of five different types: integer floating point character string enumeration REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  10. INTEGER CONSTANTS is a number that has an integer value can be specified in three notations: decimals, octals, hexadecimal. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  11. DECIMAL INTEGER CONSTANTS Consists of a sequence of one or more decimal digits 0 through 9. The first digit of the sequence can not start with 0, unless the decimal integer is 0. Examples: 0 255 32767 32768 65535 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  12. OCTAL INTEGER CONSTANTS Consists of the digit 0, followed by a sequence of one or more octal digits 0 through 7: 012 037 0177 0100 077777 corresponding to decimals: 10 31 127 4096 32767 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  13. HEXADECIMAL INTEGER CONSTANTS Consists of the digit 0 followed by x or X; followed by a sequence of one or more digits from 0 to 9, or letters a through f, or A through F. 0x1f 0X1F 0xFf 0xABC 0X10000 corresponding to the decimals: 31 31 255 2748 65536 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  14. FLOATING POINT CONSTANTS A floating-point constant is a number that has a value. Decimal numbers are used with a decimal point: 1.0 1. 0. Following are invalid fp constants: 1 (no decimal point) 1,000.0 (comma) 1 000.0 (space) .12 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  15. FP CONSTANTS CNTD. The scientific notation can be used to express real numbers (coeff)e(integer): 1.1x10^-8 -> 1.1e-8 2000000 -> 2.0E6 (upercase E is also OK) The type of a floating-point constant is double, unless suffixed with f or F. 1.2f (float), 3.7 (double) L or l is used as a suffix to declare long double: 3.45L, 1.4e-12L REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  16. CHARACTER CONSTANTS A single character enclosed with single quotation marks ( ) a H 6 Character constants are of type int. The value of the character constant is the numeric value of the machine s character set It can be ASCII or EBCDIC character set. Ex: Character ASCII = ] EBCDIC 0 48 240 a 97 129 % 37 108 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  17. NONGRAPHIC CHARACTERS Using apostrophe ( ) or the backslash (\): \ \\ Non-graphic characters: \a : audible alert \b : backspace \t : horizontal tab \n : newline \r : carriage return \v : verticle tab \f : form feed REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  18. CHARACTER CONSTANTS CNTD. \ooo : octal representation of the char contants A backslash is followed by 1,2 or 3 octal digits \xhh : hexadecimal representation of the char constants A backslah is followed by x and 1 or 2 hexadecimal digits Octal Hexadecimal Decimal ASCII \100 \x40 64 @ \135 \x5d 93 ] \0 : NULL character (ASCII value: 0) REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  19. STRING CONSTANTS A string constant (or in short string) consists of zero or more characters enclosed within double quotation marks: Hi, my name is Mehmet! What s your name? You can include nongraphic charcters as well\n \n\n\t\t* This is an item in my report!\n Double quotation marks( , ) are not part of the string. Of you want to include them, use \: I m using\ to include the \ in my string There is no limit on the length of a string REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  20. STRING CONSTANTS A string can be continued by putting a \ at the end of the line: This line is incomplete, \ I am continuing from the next line Adjacent string constants are concatenated as compile time: This line is incomplete, but will be concatenated with this string at compile time REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  21. STRING CONSTANTS The compiler automatically places a NULL character (\0) at the end of each string so that a program sacnning a string can find its end. The physical storage required is thus one character more than the number of characters enclosed within double quotation marks Note that a character Z is very different from a string Z REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  22. ARITHMETIC OPERATORS An operator is a symbol that causes specific mathematical or logical manipulations to be performed. Arithmetic operators: (+): addition (%): remainder (++): increment (-) : subtraction (+): unary plus (--): decrement (*): multiplication (-) : unary minus (/): division All the ops., except the remainder op, above operate on operands of any arithmetic type. % operator works with only integreal operands. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  23. ARITHMETIC OPERATORS The first five operators (+,-,*,/,%) are binary operators: work with two operands The result of (+,-,*,/) is the same in math. Exception: When a division is applied to integer data, the result is an integer %: returns the remainder of a division Ex: 12+9 = 21 12. + 9. = 21. 12-9 = 3 12. - 9. = 3.0 12*9 = 108 12. * 9. = 108. 12/9 = 1 12. / 9. = 1.33 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  24. ARITHMETIC OPERATORS The unary operators require one operand The unary minus applied to an operand is the negated value of the operand. The result of a unary plus applied to an operand is the value of the operand. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

More Related Content