Constants, variables and data types.pdf
Document Details
Uploaded by Deleted User
National Institute of Technology Karnataka
Tags
Full Transcript
CONSTANTS, VARIABLES AND DATA TYPES Dr. SONALI CHAKRABORTY DEPARTMENT OF MATHEMATICALAND COMPUTATIONAL SCIENCES NATIONAL INSTITUTE OF TECHNOLOGY KARNATAKA SURATHKAL, MANGALORE – 575025, INDIA Dr. Sonali Chakraborty Email: chakrabartysonali@gm...
CONSTANTS, VARIABLES AND DATA TYPES Dr. SONALI CHAKRABORTY DEPARTMENT OF MATHEMATICALAND COMPUTATIONAL SCIENCES NATIONAL INSTITUTE OF TECHNOLOGY KARNATAKA SURATHKAL, MANGALORE – 575025, INDIA Dr. Sonali Chakraborty Email: [email protected] 1 INTRODUCTION A programming language is designed to help process certain kinds of data consisting of numbers, characters and strings to provide useful information The task of processing data is accomplished by executing a sequence of precise instructions called program These instructions are formed using certain symbols and words according to some rigid rules known as syntax rules (grammar) Every program instruction must confirm precisely to the syntax rules of language Dr. Sonali Chakraborty Email: [email protected] 2 CHARACTER SET The characters that can be used to form words, numbers and expressions depend upon the computer on which the program is run The characters in C are grouped into the following categories: 1. Letters 2. Digits 3. Special Characters 4. White spaces C TOKENS ✓ Individual words and punctuation marks are called tokens. ✓ Smallest individual units are known as C tokens KEYWORDS ✓ All keywords have fixed meaning and these meanings cannot be changed ✓ Keywords serve as basic building blocks for program statements. ✓ There are certain words reserved for doing specific task, these words are known as reserved word or keywords ✓ These words are predefined and always written in lower case or small ✓ letter ✓ These keywords cannot be used as a variable name as it assigned with fixed meaning IDENTIFIERS ✓ Identifiers are user defined word used to name of entities like variables, arrays, functions, structures etc. ✓ Rules for naming identifiers are: ✓ 1) Must consists of alphabets (both upper and lower case), digits and ✓ underscore (_) sign. ✓ 2) First characters must be an alphabet or underscore. ✓ 3) Only first 31 characters are significant. ✓ 4) Cannot use a keyword. ✓ 5) Must not contain whitespace. ✓ C is a case sensitive, the upper case and lower case considered ✓ differently, for example code, Code, CODE etc. are different identifiers. ✓ identifiers are generally given in some meaningful name such as value, ✓ net_salary, age, data etc. ✓ Some invalid identifiers are 5cb, int, res#, avg no etc. CONSTANTS ✓ Fixed values that do not change during the execution of a program INTEGER CONSTANTS An integer constant refers to a sequence of digits INTEGER CONSTANTS contd… ❑ Real Constants These numbers are decimal notation, having whole number followed by a decimal point and the fractional part It is possible to omit digits before the decimal part, or digits after the decimal part A real number may expressed in exponential notation mantissa e exponent The mantissa is either a real number expressed in decimal notation or integer The exponent is an integer number with an optional + or – sign The letter e can be written in either uppercase or lowercase Embedded white space is not allowed. The e notation is called floating-point form. Floating –point constants are represented as double-precision quantities. f or F --- Single-precision l or L --- Double-precision Valid real numbers: 78688L,25636l,444.643f,321.55F,2.5e+05,374e-04 Invalid real numbers: 1.5E+0.5,$25,ox7B,7.5 e -0.5,1,00,00.00 CHARACTER SET BACKSLASH CHARACTER CONSTANTS used in output functions - each one of them represents one character. - these characters combinations are known as escape sequences SYMBOLIC CONSTANTS Symbolic constant is defined as follows #define symbolic_name value-of-constant Example: #define PI 3.14159 #define MAXMARKS 100 ❑ Rules 1) Symbolic names have the same form as variable names. 2) No blank space between the # sign define is permitted. 3) # must be the first character in the line. 4) A blank space is required between #define and symbolic name and between the symbolic name and constant. 5) #define statements must not end with a semicolon. 6) After definition, the symbolic name should not be assigned any other value within the program by using an assignment statement. 7) Symbolic names are not declared for data types. Its data type depends on the type of constant. 8) #define statements may appear anywhere in the program but before it is referenced in the program. VARIABLES Variable is a data name which is used to store data value The value of the variable can be change during the execution The rule for naming the variables is same as the naming identifier ❑ Rules : 1) They begin with a letter. Some systems permit underscore as the first character. 2) Upto 31 characters (ANSI) 3) Case Sensitive ie. Uppercase and lowercase are significant. A ≠ a 4) It should not be a keyword 5) White space is not allowed. Examples: Valid variable names: sum, Sum, SUM, a1,odd_sum Invalid Variable names: 1a,odd-sum,a%,1st,(area) DATA TYPES Data types refer to an extensive system used for declaring variables or functions of different types before its use The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted PRIMARY OR FUNDAMENTAL DATA TYPES INTEGERS Integer occupy one word of storage, and word sizes of machines vary (16 or 32 bits) The signed integer uses one bit for sign and 15 bits for the magnitude of the number Smallest to largest ❑ FLOATING POINT TYPE Floating point numbers are stored in 32-bits, with 6 digits of precision. CHARACTER TYPE A single character can be defined as a character (char) type data. Characters are stored in 8 bits of internal storage. The qualifier signed or unsigned may be explicitly applied to char DECLARATION OF VARIABLES ❑ Declaration does two things 1. It tells the compiler what the variable name is 2. It specifies what type of data the variable will hold ❑ Primary type declaration A variable can be used to store a value of any data type. data_type v1,v2,v3,….,vn; v1,v2,v3,…vn are the name of the variables Variables are separated by commas Declaration statement end with a semicolon Example: int count; float total; double ratio; DECLARATION OF VARIABLES contd… ❑ User-defined type declaration 1. “type definition” – allows users to define an identifier that would represent an existing data type 2. The user-defined data type identifier can be used to declare variables later. typedef type identifier; Where type refers to an existing data type, identifier refers to the new name given to the data type. The existing data type may belong to any class of type, including the user defined type Example: typedef int units; typedef float marks; Here, units symbolizes int and marks symbolizes float. units batch1,batch2; marks name; Advantage of typedef Meaningful data type names can be created for increasing the readability of the program DECLARATION OF VARIABLES contd… 2. “enumerated” Enum identifier (value1,value2,…..,valuen); The ‘identifier’ is a user-defined enumerated data type which can be used to declare variables That can have one of the values enclosed within the braces – enumerated constants enum identifier v1,v2,…..,vn; The enumerated variables v1,v2,…..,vn can have one of the values value1,value2,…..,valuen. V1=value3; V3=value5; ❑ Example: Enum day(Moday,Tuesday,…..,Sunday); Enum day week_st,week_end; Week_st=Monday; Week_end=Friday; DECLARATION OF STORAGE CLASS Storage class provides the information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized. i. Global variables: Global variables are known throughout the program. The variables hold their values throughout the programs execution. Global variables are created by declaring them outside of any function. It need not be declared in other function. A global variable is also known as external variable. Global variables are defined above main () in the following way: int n, sum; int m,l; char letter; main() { } it is also possible to pre-initialize global variables using the = operator for assignment. DECLARATION OF STORAGE CLASS contd… ❑ Example: float sum = 0.0; int n= 0; char c=`a'; main() { } This is the same as: float sum; int n; char letter; main() { sum = 0.0; n= 0; c=`a'; } is more efficient. DECLARATION OF STORAGE CLASS contd… C also allows multiple assignment statements using = Example: a = b = 1; This is same as a = 1; b = 1; Note : The assignment is valid if all the variable types are the same data type ii. Local variables: Variable that are declared inside a function are called “local variables”. Local variables are referred to as “automatic variables”. Local variables may be referenced only by statements that are inside the block in which the variables are declared. Local variables exist only while the block of code in which they are declared is executing. Local variables are not known to other function block. Any changes are made in local variables does not affect its value in the other. DECLARATION OF STORAGE CLASS contd… Example: void func1 (void) { Int n; n = 0; } void func2 (void) { int n; n = 99; } The integer variable n is declared in func1 () & func2 (). n is only known to the code with in the same block as variable declaration DECLARATION OF STORAGE CLASS contd… ❑ Storage class There are four types of storage class specifiers 1. auto 2. register 3. static 4. extern DECLARATION OF STORAGE CLASS contd… ❑ Declaring a variable as constant The value of the variable to remain constant during the program execution. The variable can be declared with the qualifier const at the time of initialization. const data_type variable_name=constant; Example const int n=50; The value of n cannot be modified ❑ Assigning Values to Variables Variablename = constant; Example: n=5; count=count+1; datatype variable_name=constant; Example: int n=5; float x=5.5; Dr. Sonali Chakraborty Email: [email protected] 23