Chapter 2 Constants Variables and Data Types 1
Chapter 2 Constants, Variables, and Data Types 1
2. 1 introduction A programming language is designed to help process certain kinds of data consisting of numbers, characters and strings and to provide useful output known as information. The task of processing data is accomplished by executing a sequence of precise instructions called a program. These instructions are formed using certain symbols and words according to some rigid rules known as syntax rules (or grammer).
2. 2 Character Set The characters in C are grouped into the following categories: 1. Letters A……Z 2. Digits a……z 0…… 9 3. Special characters , &. ^ ; * 4. White spaces Blank space New line : - ? + Horizontal tab Form feed ‘ < Carriage return
2. 3 C Tokens C TOKENS Keywords Constants Identifiers Strings Operators Special Symbols
2. 4 Keyword and Identifiers Keywords serve as basic building blocks for program statements. auto continue enum if short switch volatile break default extern int signed typedef while case do float long sizeof unsigned char double for register static union const else goto return struct void
Identifiers refer to names of variables, functions and array. Identifiers are user-defined names consist of a sequence of letters and digits, with a letter as first character. Rules for Identifiers 1. First character must be an alphabet (or underscore). 2. Must consist of only letters, digits or underscore. 3. Only first 31 characters are significant. 4. Cannot use a keyword. 5. Must not contain white space. Example: sum Sum M. D. John day Date student_name #33 lotus_1_2_3 char a>b _above $123 3 days
Example Use of identifiers. main() { int price, PRICE; price=1; PRICE=100; printf(“The value of %d(PRICE)is bigger than the value of %d(price). ”, PRICE, price); } Output The value of 100(PRICE)is bigger than the value of 1(price).
2. 5 Constants CONSTANTS Numeric constants Integer constants Real constants Character constants Single character constants String constants
Integer Constants An integer constant refers to a sequence of digits. There are three types: 1. decimal integers Consist of a set of digits, 0 through 9, preceded by an optional – or + sign. Example: 123 -321 0 654321 +78 2. octal integer constant Consists of any combination of digits from the set 0 through 7, with a leading 0. Example: 037 0 0435 0551 3. hexadecimal integer A sequence of digits preceded by 0 x or 0 X. Example: 0 X 2 0 x 9 F 0 Xbcd 0 x Larger integer constants Appending qualifiers such as U, L and UL Example: 56789 U or 56789 u 987612347 UL or 98761234 ul 9876543 L or 9876543 l
Example 2. 1 Representation of integer constants on a 16 -bit computer. main() { printf(“Integer valuesnn”); printf(“%d %d %dn”, 32767+1, 32767+10); printf(“n”); printf(“Long integer valuesnn”); printf(“%ld %ldn”, 32767 L+1 L, 32767 L+10 L); } Output Integer values 32767 -32768 -32759 Long integer values 32767 32768 32777
Real Constants Decimal notation 0. 0083 -0. 75 435. 36 +247. 0 215. . 95 Exponential notation 0. 65 e 4 12 e-2 1. 5 e+5 3. 18 E 3 -1. 2 E-1 -. 70 +. 5 Single Character Constants A single character constant contains a single character enclosed in pair of single quote marks. ‘ 5’ ‘X’ ‘; ’ ‘ ’ String Constants A string constant is a sequence of characters enclosed in double quotes. “Hello!” “ 1987” “WELL DONE” “? . . . !” “ 5+3” “X”
Example Defining Symbolic Constants. #define WEEK 5 7 main() { int number, total; number=4; total=WEEK*number; printf(“In %d weeks->%d days. ”, number, total); } Output In 4 weeks->28 Indays. 4 weeks->20 days.
Example Defining Symbolic Constants. #define PI 3. 1415926 3. 14 main() { double r=18. 5, area, length; area=PI*r*r; length=2*PI*r; printf(“area=%6. 2 f, length=%6. 2 f”, area, length); } Output area=1074. 66, length=116. 18 area=1075. 21, length=116. 24
Backslash Character Constants C supports some special backslash character constants that are used in output functions. a b f n r t bell back space form feed new line carriage return horizontal tab v ’ ” ? \ vertical tab single quotes double quote question mark backslash null These characters combinations are known as escape sequences.
Example Escape Sequences. main() { printf(“nctlanguagsber. Cn”); printf(“istusef165x 6 cn”); } Output C is language useful
2. 6 Variables A variable is a data name that may be used to store a data value. Constants remain unchanged, while a variable may take different at different times during execution. Example: Average, height, Total, Counter_1, class_strength 1. Name 2. Type 3. Value
Variable names subject to the following conditions: 1. They must begin with a letter (or underscore). 2. ANSI standard recognizes a length of 31 characters. 3. Uppercase and lowercase are significant. 4. It should not be a keyword. 5. White space is not allowed. Examples: Valid? Invalid? John Value T_raise Delphi x 1 ph_value mark sum 1 distance 123 (area) % 25 th
2. 8 Declaration of variables After designing suitable variable names, we must declare them to the compiler. 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. The syntax for declaring a variable is as follows: data-type v 1, v 2, …, vn; Example int count; int number, total; double sum;
Declaration of variables in program main()/*……………Program name………………*/ { } /*……………Declaration………………*/ float x, y; int code; short int count; long int amount; double sum; unsigned n; char c; /*……………Computation………………*/ …………………… /*……………Computation………………*/
2. 10 Assigning Values to Variables value = amount + inrate * amount; while (year <= PERIOD) { …………………… year = year + 1; } Target variable
Assignment Statement Form variable_name = constant; Example initial_value = 0; Initialization final_value = 100; data-type variable_name = constant; balance = 75. 84; Example yes = ‘x’; int final_value = 100; double balance = 75. 84; char yes = ‘x’; Another Example p = q = s = 0; x = y = z = MAX;
Example Character Variable. main() { char a 1, a 2; a 1=‘H’; a 2=‘i’; printf(“%c%cn”, a 1, a 2); } Output Hi
Example Uppercase to Lowercase. main() { char a 1, a 2; a 1=‘A’; a 2=‘B’; a 1=a 1+32; a 2=a 2+32; printf(“A->%c, B->%cn”, a 1, a 2); } Output A->a, B->b
Example Printing String. main() { printf } (“I like C programming. ”); (“Do you like it? n”); (“I like it very much. ”); (“Good!!!”); Output I like C programming. Do you like it? I like it very much. Good!!!
Reading Data from Keyboard General format of scanf is as follows: scanf(“control string”, &variable 1, &variable 2, ……); contains the format of data being received Example: an operator that specifies the variable name’s address scanf(“%d”, &number);
Example 2. 3 The program illustrates the use of scanf function. main() { int number; printf(“Enter an integer numbern”); Output scanf(“%d”, Enter &number); an integer number } 54 if(number < 100) Your number is smaller than 100 printf(“Your number is smaller than 100nn); else Enter an integer number printf(“Your number contains more than two 108 digitsn”); Your number is more than two digits
Example 2. 4 Output Input amount, interest rate, and period 0. 14 5 1 can be converted into 310000 in chapter The program a main() { more flexible interactive program using scanf function. 1 Rs 11400. 00 int year, period; Rs 12996. 00 float amount, 2 inrate, value; 3 Rs 14815. 44 Rs 16889. 60 printf(“Input 4 amount, interest rate, and periodn”); 5 Rs &amount, 19254. 15 &inrate, &period); scanf(“%f %f %d”, printf(“n”); year = 1; Input amount, interest rate, and period while (year <=20000 period){ 0. 12 7 value = amount + inrate * amount; 22400. 00 printf(“%2 d 1 Rs. Rs%8. 2 fn”, year, value); 2 Rs 25088. 00 amount = value; year = year 3 + Rs 1; 28. 98. 56 4 Rs 31470. 39 } } 5 Rs 35246. 84 6 Rs 39476. 46 7 Rs 44213. 63
- Slides: 28