Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - - Presentation Layout from Lecture 1 Background.

Similar presentations


Presentation on theme: "Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - - Presentation Layout from Lecture 1 Background."— Presentation transcript:

1 Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com Presentation Layout from Lecture 1 Background Sample program Components of a C program Data types

2 C Data Types Summary A.M. Gamundani 2012 - Email:- amgamundani@gmail.com 2

3 Integer types:  Integers are whole numbers with a range of values, range of values are machine dependent.  Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32767 (that is, -2 15 to +2 15 -1).  A signed integer use one bit for storing sign and rest 15 bits for number.  To control the range of numbers and storage space, C has three classes of integer storage namely: short int, Int long int. 3 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

4 Integer types:  All three data types have signed and unsigned forms.  A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number.  Therefore the range of an unsigned integer will be from 0 to 65535.  The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space. Syntax: int ; like int num1; short int num2; long int num3; 4 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

5 Integer types: 5 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

6 Floating Point Types:  Used to store fractional numbers (real numbers) with 6 digits of precision.  Floating point numbers are denoted by the keyword float.  When the accuracy of the floating point number is insufficient, we can use the double to define the number.  The double is same as float but with longer precision and takes double space (8 bytes) than float.  To extend the precision further we can use long double which occupies 10 bytes of memory space. Syntax: float ; like float num1; double num2; long double num3; 6 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

7 Floating Point Types: 7 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

8 Character Type:  Character type variable can hold a single character.  As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges.  Unsigned characters have values between 0 and 255, signed characters have values from –128 to 127. Syntax: char ; like char ch = ‘a’; Example: a, b, g, S, j. 8 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

9 Void Type:  The void type has no values therefore we cannot declare it as variable as we did in case of integer and float.  The void data type is usually used with function to specify its type. 9 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

10 Data Types in C, Size & Range of Data Types 10 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

11 Variables  A variable is a named data storage location in your computer's memory.  By using a variable's name in your program, you are, in effect, referring to the data stored there. 11 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

12 Variable Names  In C, variable names must adhere to the following rules: The name can contain letters, digits, and the underscore character (_). The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended. Case matters (that is, upper- and lowercase letters). Thus, the names count and Count refer to two different variables. C keywords can't be used as variable names. A keyword is a word that is part of the C language. 12 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

13 Variable Names - Examples Variable NameLegality PercentLegal y2x5__fg7hLegal annual_profitLegal _1990_taxLegal but not advised savings#accountIllegal: Contains the illegal character # Double Illegal: Is a C keyword 9winter Illegal: First character is a digit 13 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

14 Dos and Don’ts of Variable Names  DO use variable names that are descriptive.  DO adopt and stick with a style for naming your variables.  DON'T start your variable names with an underscore unnecessarily.  DON'T name your variables with all capital letters unnecessarily. 14 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

15 Numeric Variable Types  C provides several different types of numeric variables that have different numeric values and varying memory storage requirements.  Use appropriate variable types, to ensure that your program runs as efficiently as possible.  C's numeric variables fall into the following two main categories: Integer variables hold values that have no fractional part (that is, whole numbers only). Integer variables come in two flavors: signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0). Floating-point variables hold values that have a fractional part (that is, real numbers). 15 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

16 C's numeric data types Table. 16 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com Variable TypeKeywordBytes RequiredRange Characterchar1-128 to 127 Integerint2-32768 to 32767 Short integershort2-32768 to 32767 Long integerlong4-2,147,483,648 to 2,147,438,647 Unsigned characterunsigned char10 to 255 Unsigned integerunsigned int20 to 65535 Unsigned short integerunsigned short20 to 65535 Unsigned long integerunsigned long40 to 4,294,967,295 Single-precisionfloat41.2E-38 to floating-point3.4E38 1 Double-precisiondouble82.2E-308 to floating-point1.8E308 2 1 Approximate range; precision = 7 digits. 2 Approximate range; precision = 19 digits.

17 Variable Declarations  Before you can use a variable in a C program, it must be declared.  A variable declaration tells the compiler the name and type of a variable and optionally initializes the variable to a specific value.  If your program attempts to use a variable that hasn't been declared, the compiler generates an error message. A variable declaration has the following form: typename varname; 17 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

18 Example (s) int count, number, start; /* three integer variables */ float percent, total; /* two float variables */ 18 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

19 The typedef Keyword  used to create a new name for an existing data type.  In effect, typedef creates a synonym.  For example, the statement typedef int integer;  creates integer as a synonym for int.  You then can use integer to define variables of type int, as in this example: integer count;  Note that typedef doesn't create a new data type; it only lets you use a different name for a predefined data type. 19 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

20 User defined type declaration  C language supports a feature where user can define an identifier that characterizes an existing data type.  This user defined data type identifier can later be used to declare variables.  In short its purpose is to redefine the name of an existing data type. Syntax: typedef ; like typedef int number; Now we can use number in lieu of int to declare integer variable.  For example: “int x1” or “number x1” both statements declaring an integer variable.  We have just changed the default keyword “int” to declare integer variable to “number”. 20 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

21 Initializing Numeric Variables  When you declare a variable, you instruct the compiler to set aside storage space for the variable.  However, the value stored in that space--the value of the variable--isn't defined.  Before using a variable, you should always initialize it to a known value. You can do this independently of the variable declaration by using an assignment statement, as in this example: int count; /* Set aside storage space for count */ count = 0; /* Store 0 in count */  Note that this statement uses the equal sign (=), which is C's assignment operator  You can also initialize a variable when it's declared. int count = 0;double percent = 0.01, taxrate = 28.5;  Be careful not to initialize a variable with a value outside the allowed range. int weight = 100000;unsigned int value = -2500;  The C compiler doesn't catch such errors. 21 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

22 Dos and Don’ts of Numeric Variables  DO understand the number of bytes that variable types take for your computer.  DO use typedef to make your programs more readable.  DO initialize variables when you declare them whenever possible.  DON'T use a variable that hasn't been initialized. Results can be unpredictable.  DON'T use a float or double variable if you're only storing integers. Although they will work, using them is inefficient.  DON'T try to put numbers into variable types that are too small to hold them.  DON'T put negative numbers into variables with an unsigned type 22 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

23 What are Arrays:  Array are special type of variables which can be used to store multiple values of same data type.  Those values are stored and accessed using subscript or index.  Arrays occupy consecutive memory slots in the computer's memory. 23 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

24 What are Arrays:  Arrays occupy consecutive memory slots in the computer's memory.  x[0], x[1], x[2],..., x[9]  If an array has n elements, the largest subscript is n-1.  Multiple-dimension arrays are provided. The declaration and use look like: int name[10] [20]; n = name[i+j] [1] + name[k] [2];  Subscripts can be arbitrary integer expressions.  Multi-dimension arrays are stored by row so the rightmost subscript varies fastest.  In above example name has 10 rows and 20 columns. 24 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com

25 What are Arrays:  To be continued... 25 A.M. Gamundani 2012 - Email:- amgamundani@gmail.com


Download ppt "Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - - Presentation Layout from Lecture 1 Background."

Similar presentations


Ads by Google