Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - Email:-...
-
Upload
juliana-campbell -
Category
Documents
-
view
212 -
download
0
Transcript of Lecture 3 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - Email:-...
Lecture 3Introduction to Computer Programming
CUIT 101
1A.M. Gamundani 2012 - Email:- [email protected]
C Data Types Summary
A.M. Gamundani 2012 - Email:- [email protected] 2
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, -215 to +215-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.
3A.M. Gamundani 2012 - Email:- [email protected]
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 <variable name>; likeint num1;short int num2;long int num3;
4A.M. Gamundani 2012 - Email:- [email protected]
Integer types:
5A.M. Gamundani 2012 - Email:- [email protected]
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 <variable name>; likefloat num1;double num2;long double num3;
6A.M. Gamundani 2012 - Email:- [email protected]
Floating Point Types:
7A.M. Gamundani 2012 - Email:- [email protected]
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 <variable name>; likechar ch = ‘a’;
Example: a, b, g, S, j.
8A.M. Gamundani 2012 - Email:- [email protected]
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.
9A.M. Gamundani 2012 - Email:- [email protected]
Data Types in C, Size & Range of Data Types
10A.M. Gamundani 2012 - Email:- [email protected]
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.
11A.M. Gamundani 2012 - Email:- [email protected]
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.
12A.M. Gamundani 2012 - Email:- [email protected]
Variable Names - ExamplesVariable Name LegalityPercent Legaly2x5__fg7h Legalannual_profit Legal_1990_tax Legal but not advisedsavings#account Illegal: Contains the illegal character #
Double Illegal: Is a C keyword9winter Illegal: First character is a digit
13A.M. Gamundani 2012 - Email:- [email protected]
Dos and Don’ts of Variable NamesDO 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.
14A.M. Gamundani 2012 - Email:- [email protected]
Numeric Variable TypesC 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).
15A.M. Gamundani 2012 - Email:- [email protected]
C's numeric data types Table.
16A.M. Gamundani 2012 - Email:- [email protected]
Variable Type Keyword Bytes Required Range
Character char 1 -128 to 127
Integer int 2 -32768 to 32767
Short integer short 2 -32768 to 32767
Long integer long 4 -2,147,483,648 to 2,147,438,647
Unsigned character unsigned char 1 0 to 255
Unsigned integer unsigned int 2 0 to 65535
Unsigned short integer unsigned short 2 0 to 65535
Unsigned long integer unsigned long 4 0 to 4,294,967,295
Single-precision float 4 1.2E-38 to
floating-point 3.4E381
Double-precision double 8 2.2E-308 to
floating-point 1.8E3082
1Approximate range; precision = 7 digits.2Approximate range; precision = 19 digits.
Variable DeclarationsBefore 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;
17A.M. Gamundani 2012 - Email:- [email protected]
Example (s)
int count, number, start; /* three integer variables */
float percent, total; /* two float variables */
18A.M. Gamundani 2012 - Email:- [email protected]
The typedef Keywordused to create a new name for an existing data type.
In effect, typedef creates a synonym.
For example, the statementtypedef 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.
19A.M. Gamundani 2012 - Email:- [email protected]
User defined type declarationC 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 <type> <identifier>; liketypedef 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”.
20A.M. Gamundani 2012 - Email:- [email protected]
Initializing Numeric VariablesWhen 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. 21A.M. Gamundani 2012 - Email:- [email protected]
Dos and Don’ts of Numeric VariablesDO 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
22A.M. Gamundani 2012 - Email:- [email protected]
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.
23A.M. Gamundani 2012 - Email:- [email protected]
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.
24A.M. Gamundani 2012 - Email:- [email protected]