Data Types Variables And Constants Chapter 3 Introduction
Data Types, Variables And Constants Chapter 3
Introduction • Steps in learning English Language Alphabets Words Sentences Paragraph • Steps in learning C Alphabets Digits Special Symbol Data Types Constants Variables Keywords Instructions Program
The Character Set • Letters • Digits • Special Characters
Letters C language comprises the following set of letters to form a standard program. They are : A to Z in Capital letters. a to z in Small letters.
Digits • C language comprises the following sequence of numbers to associate the letters. 0 to 9 digits.
Special Characters ~ Tilde ! Exclamation mark # Number sign $ Dollar sign % Percent sign ^ Caret & Ampersand * Asterisk ( Left parenthesis ) Right parenthesis _ Underscore + Plus sign | Vertical bar Backslash ` Apostrophe - Minus sign = Equal to sign { Left brace } Right brace [ Left bracket ] Right bracket : Colon “ Quotation mark ; Semicolon < Opening angle bracket > Closing angle bracket ? Question mark , Comma . Period / Slash
Data Types • A data type in programming is used for identifying one of various types of data such as floating-point, integer, char etc. . • C has different data types for different types of data and can be broadly classified as : • Primary data types • Secondary data types
Primary Data types • • • Character Integer Float Double Void
Secondary Data Types • • • Array Pointer Structure Union Enum etc.
Data Type - Ranges Data Type signed char Unsigned char short signed int short unsigned int long unsigned int Float Double long double Bytes 1 1 2 2 4 4 4 8 10 Default Range Format -128 to 127 %c 0 to 255 %c -32768 to 32767 %d 0 to 65535 %u -2147483648 to 2147483647 %ld 0 to 4294967295 %lu -3. 4 e 38 to +3. 4 e 38 %f -1. 7 e 308 to +1. 7 e 308 %lf -1. 7 e 4932 to +1. 7 e 4932 %Lf
Constants And Variables
Constants • The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. • A constant is an entity that does not change.
Variables • A variable is an entity that may change it value • A variable is a name give to the space in the memory for holding data such as integers, characters, floating point numbers, strings, etc. Using a variable name is more convenient than using the memory locations.
Keywords • A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.
Keywords auto Break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Type Modifiers
C - Storage Classes
Introduction • 'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. • Scope of a variable is the boundary within which a variable can be used. • Storage class defines the scope and lifetime of a variable. • There are two memory locations in a computer system where variables are stored as : Memory and CPU Registers.
Introduction(Contd. , ) • There are following storage classes which can be used in a C Program – auto – register – static – extern
Functions of storage class • Set initial value of a variable or if not specified then setting it to default value. • Defining scope of a variable. • To determine the life of a variable.
Automatic Storage Class • • Keyword : auto Storage Location : Main memory Initial Value : 0 Life : Control remains in a block where it is defined. • Scope : Local to the block in which variable is declared.
Syntax : auto [data_type] [variable_name]; Example : auto int a; Program: void main() { auto int i=10; clrscr(); { auto int i=20; printf("nt %d", i); } printf("nnt %d", i); getch(); }
Note • auto is the default storage class for all local variables. int Count; auto int Month; • The example above defines two variables with the same storage class. auto can only be used within functions, i. e. local variables.
Register Storage Class • • Keyword : register Storage Location : CPU Register Initial Value : Garbage Life : Local to the block in which variable is declared. • Scope : Local to the block.
Syntax : register [data_type] [variable_name]; Example : register int a; Program void main() { register int i=10; clrscr(); { register int i=20; printf("nt %d", i); } printf("nnt %d", i); getch(); }
Note • When the calculations are done in CPU, then the value of variables are transferred from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes. • Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again. • It is not applicable for arrays, structures or pointers. • It cannot used with static or external storage class. • Unary and address of (&) cannot be used with these variables as explicitly or implicitly.
Note • register is used to define local variables that should be stored in a register instead of RAM. • This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location). register int Miles;
Note(Contd. , ) • Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implimentation restrictions.
Static Storage Class • Keyword : static • Storage Location : Main memory • Initial Value : Zero and can be initialize once only. • Life : depends on function calls and the whole application or program. • Scope : Local to the block.
Syntax : static [data_type] [variable_name]; Example : static int a; Program: void main() { int i; void incre(void); clrscr(); for (i=0; i<3; i++) incre(); getch(); } void incre(void) { int avar=1; static int svar=1; avar++; svar++; printf("nn Automatic variable value : %d", avar); printf("t Static variable value : %d", svar); }
Note • There are two types of static variables as : a) Local Static Variable b) Global Static Variable • Static storage class can be used only if we want the value of a variable to persist between different function calls.
Note • static is the default storage class for global variables. The two variables below (count and road) both have a static storage class. static int Count; int Road; { printf("%dn", Road); }
Note • static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in. • static can also be defined within a function. If this is done the variable is initalised at run time but is not reinitalized when the function is called. This inside a function static variable retains its value during vairous calls.
void func(void); static count=10; /* Global variable - static is the default */ main() { while (count--) { func(); } } void func( void ) { static i = 5; i++; printf("i is %d and count is %dn", i, count); }
External Storage Class : • • • Keyword : extern Storage Location : Main memory Initial Value : Zero Life : Until the program ends. Scope : Global to the program.
Syntax : extern [data_type] [variable_name]; Example : extern int a; Program: extern int i=10; void main() { int i=20; void show(void); clrscr(); printf("nt %d", i); show(); getch(); } void show(void) { printf("nnt %d", i); }
Note • extern is used to give a reference of a global variable that is visible to ALL the program files. • When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined. • When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. • Just for understanding extern is used to decalre a global variable or function in another files.
Example File 1: main. c int count=5; main() { write_extern(); } File 2: write. c void write_extern(void); extern int count; void write_extern(void) { printf("count is %in", count); }
- Slides: 39