Here are the answers:
int money$owed; - Incorrect (cannot include $)
int total_count; - Correct
int score2; - Correct
int 2ndscore; - Incorrect (cannot start with number)
int long; - Incorrect (long is a keyword)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
15 views
Eightarray
Here are the answers:
int money$owed; - Incorrect (cannot include $)
int total_count; - Correct
int score2; - Correct
int 2ndscore; - Incorrect (cannot start with number)
int long; - Incorrect (long is a keyword)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25
DATA TYPE
BOLA AKANDE COSC101 DATA TYPES
The data type of an object in memory determines the set
of values it can have and what operations that can be performed on it. Examples of data types, int, float, char etc. Thus if a data type is an integer or float you can perform arithmetic operations on it. It also signifies the way data is represented.
C is a strongly typed language because variables i.e. data
must be declared before use. DATATYPES C has a small family of datatypes. Numeric (int, float, double)
Character (char)
User defined (struct,union,array)
NUMERIC DATA TYPES int: Data type int is used for whole numbers. int specifies integer. int occupies 4 bytes in the memory and it can be as low as 2 bytes. float: it is used for specifying floating point numbers i.e. decimals, it specifies the fractional part of a number up to a single precision. It occupies 4 bytes memory space. NUMERIC DATA TYPES double: it specifies a double-precision fractional number. Thus its used for large precision. It has the largest range in the computer. It occupies 8 to 16bytes CHAR char: it’s a data type for specifying one character. It has the smallest range. It occupies 1 byte. In ANSI C we have character constant, arrays of characters, and other character handling function. NOTE THESE………. float and double can also store whole numbers Note that precision means number of digits used in expressing a value. Also the space a data type occupies in the memory (i.e. its range) is machine dependent. Real numbers is the same thing as float. DERIVED DATA TYPES There are other data types which are defined by users and called derived types. There are infinite number of derived types, common examples are arrays, structures, and pointers. They are constructed to meet user’s specific needs. WHAT ARE VARIABLES? Variables Named memory location Their value can change during the execution of a program In ANSI C programs, all variables must be declared prior to their use. This property makes C to be a strongly typed language. VARIABLES A variable is as named link/reference to a value stored in the system’s main memory or it is an expression that can be evaluated. Consider: int x=0,y=0; int y=x+2;. x, y are variables
y = x +2 is an expression,+ is an operator. SYNTAX FOR VARIABLE DECLARATION
Note that variables are declared by the association of a
data type with a variable name. Variable declaration syntax is as follows:
<data-type><identifier or variable name>;
For example int x; float area; double temp; char c; INITIALIZING VARIABLES Intialization means declaring the first or starting value or constant value of a variable that has been previously declared or has just been declared. By default a variable which is not intialize will take a 0 value, thus if you don’t want your variable to be 0 then you must initialized it. Point of initialization: at the point of declaration e.g. int n=3; INITIALIZING VARIABLES char x; /∗ uninitialized ∗/ char x=’A’; /∗ intialized to ’A’∗/ char x=’A’,y=’B’; /∗multiple variables initialized, meaning variable x will take character A while variable y will take character B∗/ char x=y=’Z’; /∗multiple initializations this means character Z is assigned to variables x and y∗/ HOW?
Variables is intialized through assignment operator i.e. =
Decalration and intialization can be done thus: int a,b,c =0, d=4; OR int a=0; =+ MEMORY CONCEPTS Variable names such as r, and area actually correspond to locations in the computer’s memory. Every variable has a name, a type and a value. when the statement scanf( "%f", &r ); /* read a floating value */ isexecuted, the value typed by the user is placed into a memory location to which the name r has been assigned. Suppose the user enters the number 2 as the value for r. The computer will place 2 into location r The statement area = PI * r * r; /* assign value to area */ thatperforms the multiplications also replaces whatever value was stored in area. QUIZ
Pick out the errors
# include <stdio.h> int main (void) { int h=10; float y float x h*y=x printf(“the product is ” +x); } EXTERNAL AND LOCAL VARIABLES Differences: The scope of external variable is from the point of declaration to the end of the program. Scope of local or automatic variable is only within the function where it is declared. External variable remains in existence permanently i.e. they retain their values even if after the function that set them have returned. Local variables are temporal; they only exists when the function that sets them exists. IDENTIFIER An identifier refers directly to the data stored in a particular memory location Variable names are known as identifiers Identifiers can contain letters, digits, underscore but it must not start with a digit. In ANSI C the first 63 characters of an identifier is significant. This is to increase portability and reduce errors. RULES FOR NAMING IDENTIFIERS Rules govern the names that can be used as identifiers. Do not use C keywords (reserved) as identifiers Identifiers are case sensitive Identifiers must not start with a numeric or digit character Identifiers must not include punctuation, control, or special characters It must not include space. An identifier can start with an underscore e.g._age For external variables, only the first 31 characters of identifiers are significant Do not use an hyphen, the compiler treat it as minus. NAMING IDENTIFIER CONTD Do not use C keywords(reserved) as identifiers C has only 32 keywords auto, double, int, struct, long, enum, register, typedef, char, extern, union, const, float, short, unsigned, signed, void, volatile, if, else while , for, do, double etc. These keywords cannot be redefined for they already have an intended meaning or interpretation. Identifiers are case sensitive Case sensitivity means that an uppercase letter is not considered to be the same as its lowercase equivalent For example, identifiers EMPL_NO and empl_no are different. NAMING IDENTIFIER CONTD Identifiers must not start with a numeric character 5age is illegal X Identifiers must not include punctuation, control, or special characters ?. @, ” are illegal X Standard C practice is to use lowercase letters for variable names n=0; And you use uppercase for symbolic constants PI = 3.142 NAMING IDENTIFIERS Names that begin with the underscore character are traditionally used in libraries for values or functions, so it is better to avoid using names that begin with an underscore in simple application programs. note also that library functions like printf() are not part of C keywords. This is because they can be redefined though they have been predefined. EXERCISE Pop quiz (correct/incorrect): int money$owed;