L6-L9-Variables, Data types, sizes and constants .pptx
Document Details
Uploaded by StableVibrance6755
2024
Tags
Full Transcript
C Program, Variables, Data types, sizes and constants Objectives To learn and appreciate − General Structure of C program − C Tokens − Variables − Declarations − Data Types and Sizes − Arithmetic Operators − Relational and Logical Ope...
C Program, Variables, Data types, sizes and constants Objectives To learn and appreciate − General Structure of C program − C Tokens − Variables − Declarations − Data Types and Sizes − Arithmetic Operators − Relational and Logical Operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 2 − Type conversions Session outcome At the end of session student will be able to learn and understand − General structure of C program − C Tokens − Variables − Declarations − Data Types and Sizes − Arithmetic Operators − Relational and Logical Operators − Type conversions − Increment and Decrement Operators 3 − Bitwise Operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 General Structure of C program 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 4 C program for reading a number and display it on the screen //Program to read and display a number #include int main() { int num; printf("\nEnter the number: "); scanf("%d", &num); printf(“The number read is: %d", num); return(0); } 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 5 Adding two integers #include int main( void ) { int sum; int integer1; int integer2; printf( "Enter first integer\n" ); scanf( "%d", &integer1 ); printf( "Enter second integer\n" ); scanf( "%d", &integer2 ); sum = integer1 + integer2; printf( "Sum is %d\n", sum ); return 0; } 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 6 C Character set Character set is a set of valid characters that a language can recognize. C character set consists of letters, digits, special characters, white spaces. (i)Letters ‘a’, ‘b’, ‘c’,………..’z’ Or ‘A’, ‘B’, ‘C’,……….’Z’ (ii)Digits 0, 1, 2,……………………9 (iii)Special characters ;, ?, >, % ‘9’ … Array name … … & … “hello”… { } 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 8 Keywords These are some reserved words in C which have predefined meaning to compiler called keywords. Keywords are not to be used as variable and constant names. All keywords have fixed meanings and these meanings cannot be changed. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 9 Compiler specific keywords Some commonly used keywords are given below: 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 10 Variables Variables are data storage locations in the computer’s memory. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 11 Variables Variables are the symbolic names for storing computational data. Variable: a symbolic name for a memory location In C variables have to be declared before they are used Ex: int x A variable may take different values at different times during execution. Declarations reserve storage for the variable. 26/09/2024 Value is assigned toCSE 1171 the variable Programming for Problem Solving by initialization (PPS) - 2024 or 12 Variable declarations 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 13 Variable Names- Identifiers Symbolic names can be used in C for various data items used by a programmer. A symbolic name is generally known as an identifier. An identifier is a name for a variable, constant, function, etc. The identifier is a sequence of characters taken from C character set. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 14 Variable names Rules for valid variable names (identifiers) : Name must begin with a letter or underscore ( _ ) and can be followed by any combination of letters, underscores, or digits. Key words cannot be used as a variable name. C is case-sensitive: sum, Sum, and SUM each refer to a different variable. Variable names can be as long as you want, although only the first 63 (or 31) characters might be significant. Choice of meaningful variable names can increase the readability 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 15 Variable names Examples of valid variable names: 1) Sum 2) _difference 3) a 4) J5x7 5) Number_of_moves Examples of invalid variable names: 1) sum$value 2) 3val 3) int 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 16 Declaring variables C imposes to declare variables before their usage. Advantages of variable declarations: Putting all the variables in one place makes it easier for a reader to understand the program. Thinking about which variables to declare encourages the programmer to do some planning before writing a program. The obligation to declare all variables helps prevent bugs of misspelled variable names. Compiler knows the amount of memory needed for storing the variable. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 17 Primary (built-in or Basic)Data types INTEGER CHARACTER SIGNED TYPE UNSIGNED TYPE INT UNSIGNED INT SIGNED CHARACTER SHORT INT UNSIGNED SHORT INT UNSIGNED CHARACTER LONG INT UNSIGNED LONG INT FLOATING POINT VOID TYPE FLOATING POINT TYPE FLOAT VOID DOUBLE LONG DOUBLE 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 18 Data types Basic data types: int, float, double, char, and void. int: can be used to store integer numbers (values with no decimal places). float: can be used for storing floating-point numbers (values containing decimal places). double: the same as type float, and roughly twice the size of float. char: can be used to store a single character, such as the letter a, the digit character 6, or a semicolon. void: is used to denote nothing or empty. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 19 Integer Types The basic integer type is int The size of an int depends on the machine and on PCs it is normally 16 or 32 or 64 bits. modifiers (type specifiers) short: typically uses less bits long: typically uses more bits Signed: both negative and positive numbers Unsigned: only positive numbers 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 22 SIZE AND RANGE OF VALUES FOR 16-BIT MACHINE (INTEGER TYPE) Type Size Range short int or signed short int 8 -128 to 127 short unsigned int 8 0 to 255 int or signed int 16 -32,768 to 32,767 Integer unsigned int 16 0 to 65,535 long int or -2,147,483,648 to signed long int 32 Long 2,147,483,647 unsigned long int 32 0 to 4,294,967,295 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 23 SIZE AND RANGE OF VALUES What will be output? The above program will print -1 as its output because it will be out of range. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 24 SIZE AND RANGE OF VALUES 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 25 SIZE AND RANGE OF VALUES 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 26 The char type A char variable can be used to store a single character. A character constant is formed by enclosing the character within a pair of single quotation marks. Valid examples: 'a’. Character zero ( ‘0’ ) is not the same as the number (integer constant) 0. The character constant ‘\n’—the newline character—is a valid character constant. It is called as an escape character. There are other escape sequences like, 26/09/2024 \t for tab, \v for CSE 1171 Programming for Problem Solving (PPS) - 2024 27 vertical tab, \n for new line etc. Character Types Character type char is related to the integer type. Modifiers(type specifiers) unsigned and signed can be used char 1 byte (-128 to 127) signed char 1 byte (-128 to 127) unsigned char 1 byte (0 to 255) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 28 Assigning values to char char letter; letter = ‘A'; letter = A; letter = “A"; letter = 65; 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 29 Floating-Point Types Floating-point types represent real numbers Integer part Fractional part The number 108.1517 breaks down into the following parts 108 - integer part 1517 - fractional part There are three floating-point type specifiers float double long double 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 30 SIZE AND RANGE OF VALUES FOR 16-BIT MACHINE (FLOATING POINT TYPE) Type Size 32 bits Single Precision Float 4 bytes 64 bits Double Precision double 8 bytes Long Double 80 bits long double Precision 10 bytes 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 31 void 2 uses of void are To specify the return type of a function when it is not returning any value. To indicate an empty argument list to a function. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 32 Summary We have learnt about − General Structure of C program − C Tokens − Variables − Declarations − Data Types and Sizes − Arithmetic Operators − Relational and Logical Operators − Type conversions − Increment and Decrement Operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 35 − Bitwise Operators Objectives To learn and appreciate − Arithmetic Operators − Relational and Logical Operators − Type conversions − Increment and Decrement Operators − Bitwise Operators − Assignment Operators and Conditional Expressions − Precedence and Order of Evaluation 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 36 Session outcome At the end of session student will be able to learn and understand − Arithmetic Operators − Relational and Logical Operators − Type conversions − Increment and Decrement Operators − Bitwise Operators − Assignment Operators and Conditional Expressions − Precedence and Order of Evaluation 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 37 Operators The different operators are: Arithmetic Relational Logical Increment and Decrement Bitwise Assignment Conditional 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 38 Binary Arithmetic Operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 39 Binary Arithmetic Operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 40 Arithmetic Operators The binary arithmetic operators are +, -, *, / and the modulus operator %. The / operator when used with integers truncates any fractional part i.e. E.g. 5/2 = 2 and not 2.5 Therefore % operator produces the remainder when 5 is divided by 2 i.e. 1 The % operator cannot be applied to float or double E.g. x % y wherein % is the operator and x, y are operands 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 41 The unary minus operator #include int main () { int a = 25; int b = -2; printf(“%d\n”,-a); printf(“%d\n”,-b); return 0; } 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 42 Working with arithmetic expressions Basic arithmetic operators: +, -, *, /, % Precedence: One operator can have a higher priority, or precedence, over another operator. The operators within C are grouped hierarchically according to their precedence (i.e., order of evaluation) Operations with a higher precedence are carried out before operations having a lower precedence. High priority operators * / % Low priority operators + - Example: * has a higher precedence than + a + b * c a+(b*c) If necessary, you can always use parentheses in an expression to force the terms to be evaluated in any desired order. Associativity: Expressions containing operators of the same precedence are evaluated either from left to right or from right to left, depending on the operator. This is known as the associative property of an operator. Example: + has a left to right associativity For both the precedence group described above, associativity is “left to right”. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 43 Working with arithmetic expressions #include int main () { int a = 100; int b = 2; int c = 25; int d = 4; int result; result = a * b + c * d; printf(“ Result1: %d\n”,result); result = a * (b + c * d); printf(“ Result2: %d\n”,result); return 0; } 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 44 Relational Operator operators Meaning == Is equal to != Is not equal to < Is less than Is greater than >= Is greater or equal The relational operators have lower precedence than all arithmetic operators: a < b + c is evaluated as a < (b + c) ATTENTION ! the “is equal to” operator == and the “assignment” operator = 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 45 Relational operators An expression such as a < b containing a relational operator is called a relational expression. The value of a relational expression is one, if the specified relation is true and zero if the relation is false. E.g.: 10 < 20 is TRUE 20 < 10 is FALSE A simple relational expression contains only one relational operator and takes the following form. ae1 relational operator ae2 ae1 & ae2 are arithmetic expressions, which may be simple constants, variables or combinations of them. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 46 Relational operators 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 47 Relational operators The arithmetic expressions will be evaluated first & then the results will be compared. That is, arithmetic operators have a higher priority over relational operators. > >= < (i+5) k!=3 true 1 j==2 false 0 26/09/2024 false 0 CSE 1171 Programming for Problem Solving (PPS) - 2024 48 Logical operators Truth Table Operator Symbol Example AND && expression1 && expression2 OR || expression1 || expression2 NOT ! !expression1 The result of logical operators is always either 0 (FALSE) or 1 (TRUE) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 49 Logical operators Expressions Evaluates As (5 == 5)&&(6 != 2) True (1) because both operands are true (5 > 1) || (6 < 1) True (1) because one operand is true (2 == 1)&&(5 ==5) False (0) because one operand is false ! (5 == 4) True (1) because the operand is false !(FALSE) = TRUE !(TRUE) = FALSE 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 50 Increment and Decrement operators (++ and -- ) The operator ++ adds 1 to the operand. The operator -- subtracts 1 from the operand. Both are unary operators. Ex: ++i or i++ is equivalent to i=i+1 They behave differently when they are used in expressions on the R.H.S of an assignment statement. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 51 Increment and Decrement operators (++ and -- ) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 52 Increment and Decrement operators (++ and -- ) 1. Pre-Increment 2. Post-Increment result=++var result=var++ 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 53 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 54 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 55 Increment and Decrement operators Ex: m=5; y=++m; Prefix Mode In this case, the value of y and m would be 6. m=5; y=m++; Postfix Mode Here y continues to be 5. Only m changes to 6. Prefix operator ++ appears before the variable. Postfix operator ++ appears after the variable. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 56 Increment and Decrement operators 1. Pre-Decrement Operator 2. Post-Decrement Operator 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 57 Increment and Decrement operators Don’ts: Attempting to use the increment or decrement operator on an expression other than a modifiable variable name or reference. Example: ++(5) is a syntax error ++(x + 1) is a syntax error 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 58 Bitwise Operators Bitwise Logical Operators Bitwise Shift Operators Ones Complement operator 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 59 Bitwise Logical operators &(AND),|(OR),^(XOR) op op 1 2 & | ^ These are binary operators and 1 1 1 1 0 require two integer operands. 1 0 0 1 1 These work on their operands bit 0 1 0 1 1 by bit starting from LSB (rightmost bit). 0 0 0 0 0 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 60 Example Suppose x = 10, y = 15 z=x&y sets z=10 like this 0000000000001010 x 0000000000001111 y 0000000000001010 z = x & y Same way |,^ according to the table are computed. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 61 Bitwise Shift operators These are used to move bit patterns either to the left or right. They are used in the following form opn here op is the operand to be shifted and n is number of positions to shift. 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 62 Bitwise Shift operator: >> causes all the bits in operand op to be shifted to the right by n positions. The rightmost n bits will be lost and the left most vacated bits are filled with 0’s if number is unsigned integer 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 64 Examples ( left shift) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 65 Examples ( Right shift) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 66 Examples Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011 x1 Add 0000 0000 0000 0101 ZEROS 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 67 Examples Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011 whose equivalent value in decimal number system is 11. x2 Add Z 0000 0000 0000 0010 =2 EROS Note: x=y1; same as x=y/2 (Division) 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 68 Bitwise Shift operators Op and n can be constants or variables. There are 2 restrictions on the value of n n cannot be –ve n should not be greater than number of bits used to represent Op.(E.g.: suppose op is int and size is 2 bytes then n cannot be greater than 16). 26/09/2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 69 Example int main() { int var = 2; printf("var * 2 = %d \n",var a *c) ((b=2)==a) Evaluate the following: 1. ( (5 == 5) && (3 > 6) ) 2. ( (5 == 5) || (3 > 6) ) 3. 7==5 ? 4 : 3 4. 7==5+2 ? 4 : 3 5. 5>3 ? a : b 6. K = (num > 5 ? (num =c) 1 iii. (b+4 > a *c) 0 iv. ((b=2)==a) 1 2. Evaluate the following: i. ( (5 == 5) && (3 > 6) ) 0 ii. ( (5 == 5) || (3 > 6) ) 1 iii. 7==5 ? 4 : 3 3 iv. 7==5+2 ? 4 : 3 4 v. 5>3 ? a : b 2 vi. K = (num > 5 ? (num