Presentation is loading. Please wait.

Presentation is loading. Please wait.

CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.

Similar presentations


Presentation on theme: "CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types."— Presentation transcript:

1 CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types of constants: 1.Numeric constants 2.Character constants The const keyword is used to declare a constant, as shown below: int const a = 1; const int a =2; The keyword const can be declared before or after the data type.

2 NUMERIC CONSTANTS Numeric constants are of two types: i.Integer constants ii.Floating point constants

3 Integer Constants The whole numbers which have no fraction part (decimal point) or comma is called integer constants. An integer represents a value that is counted like the number of students in a class. Negative integer constant must be preceded by a negative sign but a plus sign is optional for non-negative integers.

4 Some examples of valid integer constants are: 0 765 -987 +4563 Following are invalid integer constants for the reason indicated: 6.6758 -56.8 18#3 082 1(blank) 9 34, 56 In above example, character (#), blank space,(.), (,),(-+) are illegal characters and 0 cannot be the first digits of decimal integer constants.

5 Floating Point Constants Floating point constants are also called real constants. Floating point constants are used to represent values that are measured like the height of a person which might have a value of 166.75 cm, as opposed to integer constants which are used to represent values that are counted.

6 A floating point constant consists of either a fraction form or the exponent or the both. It may have either sign (+,-). Some valid floating points are as given; 0.5, 11.0, 8905.2, 66668e2, -52.34,-0.123 The following are some invalid floating point constant; 85——————– Missing decimal point (.) -1/2——————- Illegal characters (/).59, 45.4—————Illegal character (.,,) and no digits to left of the decimal point. Some valid floating pint constants in the exponent forms are given below. 8.85e5——is equivalent to 885000. 6.6E2——–is equivalent to 660. 5.3e-6——-is equivalent to -530000.

7 CHARACTER CONSTANTS: Character constants are enclosed within single quotes i.e. apostrophes mark (‘) e.g. ‘a’, ‘cd’, ‘%’ represent character constants. It usually includes: Digits 0 through 9 Uppercase letters A through Z Lowercase letters a through z Punctuation symbols such as semicolon (;) Comma (,) Period (.) Special symbols such as +,-, =,> etc.

8 #include int main() { int const RED=5, YELLOW=6, GREEN=4, BLUE=5; printf("RED = %d\n", RED); printf("YELLOW = %d\n", YELLOW); printf("GREEN = %d\n", GREEN); printf("BLUE = %d\n", BLUE); getch(); } This will produce following results RED = 5 YELLOW = 6 GREEN = 4 BLUE = 5

9 VARIABLES A variable is a name of data storage location in computer memory that is used to store a certain kind of data. Variable has given a name for reference. In other words, when a variable is used in a computer program, the computer associates it with a particular memory location.

10 NAMING VARIABLES The name of variable can be called identifier or variable name. It has to follow these rules: The name can contain letters, digits and the underscore but the first letter has to be a letter or the underscore. Be avoided underscore as the first letter because it can be clashed with standard system variables. The underscore may be used to improve readability of the variable name e.g. subject_name. There is no restriction on the length of a variable name. The length of name can be up to 247 characters long in C but 31 characters are usually sufficient.

11 C language has a very small set of keywords such as int, do, if, while etc. which are part of the programming language. Keywords cannot be used as a variable name. These keywords are also known as reserved words. All the reserved words of c programs must be written as lower case letters, e.g. the reserved word if should not be written as IF or If. Both the upper and lower case letters are allowed in naming variables. An uppercase letter is considered different from a lower case letter. For example, the variable AVG is different from Avg or avg. There are four basic types of variable in C; they are: char, int, double, and float.

12 INTEGER VARIABLES Declaring Integer variables: In C programming language, declaring a variable is also defining a variable. All the variables used in a C program must be declared. The int variable can be declared as follows: – int counter; The declaration consists of the data type, int, followed by the variable name, counter. If you have more than one variable of the same type, you can separate the variables names with commas as illustrated below: – int counter, sum, age;

13 The variable declaration always ends with a semicolon (;). Declaring a variable tells the computer the name of the variable and its type. This enables the compiler to recognize the valid variable names in your program. Initialising Variables: You can also initialise a variable when you declare it, for example: – int x=10;

14 Type Qualifiers Type qualifiers refer to the number of bytes used for storing the integer on a particular computer. Declaration of an integer variable can be preceded by the type qualifiers short, long, unsigned or signed. Some examples are: short int num; long int sum,avg; unsigned int count; short unsigned int count;

15 To help promote safety in programs, values can be marked as being constant with the const type qualifier. Compilers must diagnose, usually with an error, attempts to modify such variables. const variables must be initialised at the point of declaration. int const black = 12; const int black = 12; The const type qualifier declares an object to be no modifiable.

16 The number of bytes set aside by the compiler for the type qualifiers are as given below: Variable DeclarationNo. of BytesRange int2-32,768 to +32,767 long int4-2,147,483,648 to -2,147,483,647 unsigned int20 to 65,535

17 Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. The advantage of unsigned integers is to allow a greater range of positive values (e.g. 0 to +65535), whereas signed integers allow only up to half the same number as positive integers and the other half as negative integers (e.g. −32768 to +32767).

18 When declaring a short int, long int, it is permissible to omit the int. The following two declarations are equivalent. – long int brown; – long brown;

19 FLOATING POINT VARIABLES Floating point variables are used for floating point numbers. Floating point variables are stored in memory in two parts. The first part is the mantissa and the second part is the exponent. The mantissa is the value of the number and the exponent is the power to which it is raised. Some examples of floating point variable declaration statement are: Float height; Double float a,b,total; Long double float size,x,y;

20 The number of bytes set aside by the compiler for the above floating point qualifiers are as given below: (consult from the book) Variable Declaration No. of BytesRange float4 to (with 6 to 7 digits of precision) double float4 to (with 16 digits of precision) long double float2 to (with twice the precision of double)

21 Here, precision means the number of digits after the decimal point. Note that the word float may be omitted when preceded by a double, long double type qualifier in the declaration statements. The following two declarations are equivalent. – long double float f; – long double f;

22 There is another method of representing floating point numbers known as exponential notation. For example, the number 23,688 would be represented as 2.3688e4. Here, 2.3688 is the value of the number (mantissa) and 4 is the exponent, the power to 10 to which the number will be raised. The exponent can also be negative. For example, the number 0.0005672 is represented as 5.672e- 4 in exponential notation. The exponential form transfers the number one digit to the left of decimal point, followed by e and the appropriate power of 10.

23 In C, a floating point number of type float is stored in four bytes. Three bytes for the mantissa and one byte for the exponent and provides 6 or 7 digits of precision. Consider the C program to calculate the area of rectangle along with description at page 19 of the course text book.

24 CHARACTER VARIABLES Character variables are used to store character constants. Examples of declaration of character variables are: Char letter, c, answer; The compiler set aside one byte of memory for storing a character in a character variable. A character variable can only store one character.


Download ppt "CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types."

Similar presentations


Ads by Google