Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared By: K. U. Khimani Assistant Professor IT Department.

Similar presentations


Presentation on theme: "Prepared By: K. U. Khimani Assistant Professor IT Department."— Presentation transcript:

1 Prepared By: K. U. Khimani Assistant Professor IT Department

2 C is robust language because of using C operators and built-in functions, programmer can develop any complex program. C is portable. C is modular language. The large problems are divided into sub problems (modules). Because of the modularity support, C is also called as structured programming language. Supports Bit-wise operations which allow the programmer to do the operations on data bit- by-bit, which is the feature of machine language. Because of this facility, C is also known as ‘middle level language’.

3

4 Documentation Section: It consists of a set of comment lines giving the name of the program, the author details or other, which the programmer would like to use later. Link Section: It provides instruction to the compiler to link functions from the system library.

5 Definition Section: It defines all symbolic constants. Global Declaration Section: There are some variables that are used in more than one function. Such variables are called Global Variables. They are declared in this section. This section also declares all the user defined functions.

6 Main() Function Section: It’s a main function where program execution begins. Every C program should contain exact one main() function. This section contains two parts: Declaration part : It declares all the variables used in the executable part. Executable part : There is at least one executable statement in the executable part. Note : 1)These two parts must appear between the closing brace. 2)The closing brace of the main() function section is the logical end of the program. 3) All statements within these two parts ends with a semicolon.

7 Subprogram Section: It contain all the user defined functions that are called in the main function. UDFs are generally placed immediately after the main() Function, although they may appear in any order.

8 C program can be viewed as a group of building blocks called functions. A function is a subroutine that may include one or more statements designed to perform a specific task.

9 Comments are part of Documentation Section. #include void main() { /* Our first simple C basic program */ (Comment Line) printf(“Hello World! “); getch(); } In the above example, the line written between /* and */ is comment line.

10 A header file is a file with extension.h which contains C function declarations. #include SYNTAX : #include #-------- is a pre-processor directive. Library Functions are grouped category-wise and stored in different files known as Header Files.

11 In the C programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

12 C Supports three classes of data types: Primary or Fundamental Data Types Derived Data Types User-Defined Data Types

13 All C compilers support four fundamental(primary) data types namely... Integer (int) Character (char) Float (float) Double (double)

14 TypeSizeRange of ValuesAccess Specifier char1 Byte-128 to 127%c unsigned char1 Byte0 to 255%c int (or signed int)2 Bytes-32768 to 32767%d unsigned int2 Bytes0 to 65535%u short1 Byte-128 to 127%d unsigned short1 Byte0 to 255%u long4 Bytes-2147483948 to 2147483647%ld unsigned long4 Bytes0 to 4294967295%lu float4 Bytes-2^31 to 2^31 - 1%f double8 Bytes-2^63 to 2^63 – 1%lf long double10 Bytes-2^79 to 2^79 – 1%Lf

15 In a C Program, the smallest individual units are known as C Tokens.

16 Keywords and Identifiers : Every C word is classified as either a keyword or an identifier. All the keywords have meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. All the keywords must be written in lower case.

17 Keywords and Identifiers : The list of all the keywords of ANSI C are listed in the below mentioned table. charswitchlongunioncontinuegotointwhile enumexternregistertypedefbreakconstfloatshort sizeofdodefaultifforsignedunsignedvoid staticstructelsecasevolatileauto double

18 Keywords and Identifiers : Identifiers refer to the names of variables, functions and arrays. These are user defined names and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore letter is also permitted in identifiers.

19 Rules for Identifiers : First character must be an alphabet (or underscore). Must consist of only letters, digits or underscore. Only first 31 characters are significant. Cannot use a keyword. Must not contain white space.

20 Constants : Constants in ‘C’ are applicable to the values, which do not change during execution of a program. There are several types of constants in ‘C’. NUMERICAL CONSTANT CHARACTER CONSTANT

21 Constants :

22 Numeric Constants : Numeric constant can be integer or real constant. Integer Constant: It can be positive or negative number. For example; 25, -59, etc. In the integer constant, special symbols such as space, comma, currency are not allowed. For example; 2,456 or 3 543 or $56 are not allowed.

23 Numeric Constants : Real Constant: Real constant is a number having fraction part written after decimal point. Real constant can also be represented in scientific notation. For Ex.; 4.6 -5.7 0.0098 0.7e^-3  is a scientific notation for 0.0007

24 Character Constants : Single Character Constant: They are enclosed within single quote ‘’. Characters are represented in computer memory using a coding scheme known as ASCII code. Ex. ‘f’, ‘H’, ‘7’, ‘+’, etc. Note: Backslash constants are the special type of character constants which consists of two characters. For ex. ‘\n’, ‘\t’, etc.

25 Character Constants : String Constant: It is a sequence of characters which are enclosed in double quotes. For ex. “Madhur” “Windows” “987”

26 A variable is a data name that may be used to store a data value. A variable may take different values at different times during execution of the program. A variable name is chosen by the programmer in a meaningful way so as to reflect its function or nature in the program. Variable names may consist of letters, digits and underscore(_) character.

27 They must begin with a letter. Some systems permit underscore as the first character. Variable name must be less than 32 characters. However, length should not be normally more than eight characters, since only the first eight characters are treated as significant by the compilers. It should not be a keyword. White space is not allowed. Uppercase & Lowercase are significant. That is; Total, total and TOTAL are considered to be different variable names.

28 Some examples of Valid Variable names are: John sum1 x1 T_value, etc. Invalid Example Include: 123 (area) 25 th % char group one Price$, etc.

29 An operator indicates an operation to be performed on operand that yields a new value. An operand is a data item on which operators perform operations. C is rich in use of different operators: Arithmetic Relational Logical Assignment Increment and Decrement Conditional Bitwise Special

30 Arithmetic Operators: +,-,*,/ and % are the arithmetic operators that perform addition, subtraction, multiplication, division and modulo operations, respectively. Example of modulo division operation(that gives you reminder value when you perform division operation): 5 % 2 = 1 15 % 3 = 0 38 % 13 = 12

31 Relational Operators: We often compare two quantities and depending on their relation, take certain decisions. For example, we may compare age of two persons, or the price of two items, and so on. These comparisons can be done with the help of relational operators. The value of a relation expression is either 1 or 0. It is 1 if the specified relation is true and 0 if the specified relation is false.

32 OperatorMeaning <Is less than >Is greater than <=Is less than or equal to >=Is greater than or equal to ==Is equal to !=Is not equal to

33 Logical Operators: C has following three logical operators: && meaning Logical AND || meaning Logical OR ! meaning Logical NOT The Logical operators && and || are used when we want to test more than one condition and make decisions. An example is: a>b &7 x==10 An expression of this kind, which combines two or more relational expressions, is termed as a logical expression or a compound relational expression.

34 Assignment Operators: Assignment operators are used to assign the result of an expression to a variable. We have seen the usual assignment operator, ‘=’. In addition, C has a set of ‘shorthand’ assignment operators of the form v op=exp; is equivalent to v = v op (exp); For example, x += y+1; is equivalent to x = x+(y+1);

35 Shorthand Assignment OperatorStatement with simple assignment operator a += 1a = a + 1 a -= 1a = a – 1 a *= n+1a = a * (n+1) a /= n+1a = a / (n+1) a %= ba = a % b

36 Increment & Decrement Operators: C allows two very useful operators not generally found in other languages. These are the increment and decrement operators: ++ and --. The operator ++ adds 1 to the operand, while -- subtracts 1. Both are unary operators and takes the following form: ++m; or m++; is equivalent to m=m+1; --m; or m--; is equivalent to m=m-1;

37 Increment & Decrement Operators: While m++ and ++m mean the same thing when they form statements independently, they behave differently when they are used in expressions on the right-hand side of an assignment statement.

38 Increment & Decrement Operators: Consider the following: m=5; y=++m; In this case, the value of y and m would be 6. Suppose, if we rewrite this statement as m=5; y=m++; Then, the value of y would be 5 and m would be 6.

39 Increment & Decrement Operators: A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left. On the other hand, a postfix operator first assigns the value to the variable on left and then increments the operand.

40 Increment & Decrement Operators: Solve the expression in C language compiler,assuming x and z are integer variables: z = x++ + x++ + ++x; where x=7 z = x++ + x++ + ++x; //post increment of x=2 z = x + x + ++x //++x; increase the value of x one so x=8 /*Now all pre and post increment/decrement are solved so we write the expression in normal mathematical expression*/ z = x + x + x; //put the current value of x i.e. 8 z = 8 + 8 + 8; z = 24; so, x = 8 + 2;  x = 10;

41 Solve the following expression : z = ++x + y-- - ++y - x-- - x-- - ++y - x-- where x = 7 and y = -3 Sol. z=++x + y-- - ++y - x-- - x-- - ++y - x--; /* post decrement of x=3 and post decrement of y=1*/ z= ++x + y - ++y - x - x - ++y - x; /* its unary operator so we solve it from right to left*/ z=++x + y - ++y - x - x - y - x //now y=-3+1=-2 z=++x + y - y - x - x - y - x //now y=-2+1=-1 z=x + y - y - x - x - y - x //now x=7+1=8 /*now all increment and decrement operators all solved so we put the current value of x and y.*/ z= 8 + (-1) - (-1) -7 - 7 - (-2) - 7 z= 8 - 1 + 1 - 7 - 7 + 2 - 7 z= - 11

42 Conditional(Ternary) Operator: A ternary operator pair “?:” is available in C to construct conditional expressions of the form exp1 ? exp2 : exp3 Where exp1, exp2 and exp3 are expressions.

43 Conditional(Ternary) Operator: This operator works as follows: Exp1 is evaluated first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and becomes the value of the expression. Note that only one of the expression (either 2 or 3) is evaluated.

44 Conditional(Ternary) Operator: For Example, a=10; b=15; x = (a>b) ? a : b; In this example, x will be assigned the value of b. this can be achieved using if…else statement too.

45 Bitwise Operator: They are for the manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left.

46 Bitwise OperatorMeaning &Bitwise AND |Bitwise OR ^Bitwise exclusive OR <<Shift left >>Shift right

47 Special Operator: C supports some special operators of interest such as comma operator and sizeof operator. The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right most expression is the value of combined expression.

48 Special Operator: For example, value = (x=10,y=5,x+y); first assigns 10 to x, then assigns 5 to y, and finally assigns 15 to value. Since comma operator has the lowest precedence of all operators, the parenthesis are necessary.

49 Special Operator: The sizeof operator is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. Examples:m=sizeof(sum); n=sizeof(long); This operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer.

50 An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. C can handle any complex mathematical expression. Remember that C does not have an operator for exponentiation.

51 Expressions are evaluated using an assignment statement of the form: variable = expression; Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first and the result then replaces the previous value of the variable on the left(If there is no previous value assigned in a variable, there is a garbage value.). All the variables used in the expression must be assigned values before evaluation is attempted.

52 Example: x = a* b –c; y = b / c *a; z = a – b / c + d; The blank space around an operator is optional and adds only to improve readability.

53 An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators. There are two distinct priority levels of arithmetic operators in C. High Priority * / % Low Priority + -

54 Precedence: It refers to the priority given to the operator for a process. Higher priority operators are executed first. For example: 8+9*2–10 = 16 Associativity: When an expression has operators with equal precedence, the associativity property decides which operation to be carried out first. Associativity means the direction of execution.

55 When two operators of the same priority are found in the expression, precedence is given to the left most operators. For example: x= 5*4 + 8/2; Here, 5*4 is solved first. Although * and / have the same priorities, the operator * occurs before /.

56 If there are more sets of parentheses in the expression, the innermost parenthesis is solved first, followed by the second and so on. For example: (8 / ( 2 * (2*2))); Here, the innermost bracket is evaluated first, i.e. 2*2=4; then the second innermost bracket is evaluated. 2 is multiplied with the result of the innermost bracket. The answer of 2*4=8; then the outermost bracket is evaluated. 8 is divided by 8 and gives the result 1.

57 Implicit Type Conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without losing any significance. This automatic conversion is known as implicit type conversion.

58 Implicit Type Conversion If the operands are of different types, the ‘lower’ type is automatically converted to the ‘higher’ type before the operation proceeds. The result is of higher type.

59 Implicit Type Conversion Example: int i,x; float f; double d; long 1; x = 1/i + i*f – d;

60 Explicit Type Conversion (Type Casting) There are instances when we want to force a type conversion in a way that is different from the automatic conversion. Consider the following example: ratio = female_number / male_number; Since female_number and male_number are declared as integers in the program, the decimal part of the result of division would be lost and ratio would represent a wrong figure.

61 Explicit Type Conversion (Type Casting) This problem can be solved by converting locally one of the variables to the floating point as shown below: ratio = (float) female_number / male_number; The operator (float) converts female_number to floating point for the purpose of evaluation of the expression.


Download ppt "Prepared By: K. U. Khimani Assistant Professor IT Department."

Similar presentations


Ads by Google