Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:1 INTRODUCTION TO ‘C’ LANGUAGE Chapter 2.

Similar presentations


Presentation on theme: "Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:1 INTRODUCTION TO ‘C’ LANGUAGE Chapter 2."— Presentation transcript:

1 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:1 INTRODUCTION TO ‘C’ LANGUAGE Chapter 2

2 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:2 Origin of C Application Language used Commercial Application COBOL Engineering and Scientific Related Applications FORTRAN

3 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:3 YearLanguage Developed By Remarks 1960ALGOL International Committee Too general, Too abstract 1963CPL Cambridge University Hard to learn, difficult to understand 1967BCPL Martin Richards at Cambridge University Could deal with only specific problems

4 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:4 YearLanguage Developed By Remarks 1970B Ken Thompson at AT&T Bell Labs Could deal with only specific problems 1972C Dennis Ritchie at AT&T Bell Labs Lost Generality of BCPL and B restored

5 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:5 Types of Programming Language All the programming languages can be divided into two categories: Problem Oriented Languages or High level Languages: These languages have been designed to give a better programming efficiency i.e. faster program development and slower program execution. e.g Fortran, Basic, Pascal.

6 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:6 Machine Oriented Language or Low Level Languages: These languages have been designed to give a better machine efficiency i.e. faster program execution and slower program development. e.g. Assembly language and Machine language.

7 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:7 What type of Language C is?  C stands in between these two languages.  C is called as a middle level language.  It was designed to have both a relatively good programming efficiency and a relatively good machine efficiency.

8 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:8 Definition of C  C is a programming language developed at AT&T Bell laboratories of U. S. A. in 1972.  It was designed and written by a man named Dennis Ritchie.  C seems so popular is because it is reliable, simple and easy to use.

9 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:9 Starting with ‘C’  Communicating with a computer involves speaking the language the computer understands.  English is the language for communicating with a computer.  Method of learning English is to first learn the alphabets or characters used in the language.

10 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:10  Combine these alphabets to form words.  Combine these words to form sentences.  Sentences are combined to form paragraph. Alphabets Words Sentence Paragraph Alphabets Words Sentence Paragraph

11 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:11  Instead of directly learning how to write programs, we should first know what alphabets, digits and special symbols used in C.  Then combine these to form constant, variables and keywords.

12 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:12 Alphabets Digits Special Symbols Constants Variables Keyword Instruction Program Combine these to form instruction. Combine these to form instruction. A group of instruction is nothing but a program.A group of instruction is nothing but a program.

13 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:13 C Character Set A Character denotes any alphabet, digit or special symbol used to represent information. Alphabets A, B, ……………. Z a, b, …………….y, z. a, b, …………….y, z. Digits 0, 1, 2, ……………9. Special symbols ~, @, %, ^, ()………

14 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:14 Constant, Variables and Keywords The alphabet, digits and special symbols when properly combined form constants, variables and keywords. Constant A constant is a quantity that doesn’t change. This quantity can be stored at a locations in the memory of the computer.

15 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:15 Variables A quantity whose value may change during program execution. 3X + Y = 20 Where, 3 and 20 cannot change, hence they are constants whereas the quantities X and Y can vary or change hence are called variables.

16 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:16 Types of C constant 1)Primary Constant 2)Secondary Constant C Constants Primary Constants Secondary Constants Integer Constant Array, Pointer, Real Constant Structure, Union, Character Constant Enum etc.

17 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:17 Rules for constructing Integer Constants  An integer constant must have at least one digit.  It must not have a decimal point.  It could be either positive or negative.  If no sign precedes an integer constant it is assumed to be positive.

18 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:18 Rules for constructing Integer Constants  No commas or blanks are allowed within an integer constant.  The allowable range for integer constant is -32768 to + 32767.  Integer occupies 2 bytes in memory i.e. 16 bits. E.g. 426, +782, -8000

19 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:19 Rules for constructing Real Constants  A real constant must have at least one digit.  It must have a decimal point.  It could be either positive or negative.  If no sign precedes a real constant it is assumed to be positive.

20 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:20 Rules for constructing Real Constants  No commas or blanks are allowed within an integer constant.  The allowable range for real constant is -3.4e+38 to +3.4e+38.  Real occupies 4 bytes in memory i.e. 32 bits. E.g. 1.2, -7.82

21 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:21 Rules for constructing Character Constants  A character constant is either a single alphabet, a single digit or a single special symbol enclosed within a single inverted commas.  Example ‘A’ is a valid character constant whereas ‘A’ is not a valid character constant.  Character occupy 1 byte of memory.

22 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:22 Rules for constructing Character Constants Valid Character constants are: ‘A’, ‘1’, ‘E’, ‘=‘ VARIABLE NAMES These are the names given to the location in the memory of a computer where different constants are stored.

23 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:23 VARIABLE NAMES CONTINUED EXAMPLE  A constant stored in a memory location with an integer variable name must be an integer constant.  A constant stored in a memory location with a real variable name must be a real constant.  A constant stored in a memory location with a character variable name must be a character constant.

24 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:24 Rules for constructing variable names  A variable name is any combination of 1 to 8 alphabets, digits or underscores.  Some compilers allow variable names whose length could be upto 40 characters, but we stick to the rule of maximum 8 characters.  The first character in the variable name must be an alphabet.

25 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:25 Rules for constructing variable names  No commas or blank spaces are allowed within a variable name.  No special symbols only underscore is allowed from special symbols. Example: emp_name, emp_age, emp_sal Basic Data Types Integer (int), Float (float), Character (char)

26 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:26 Rules for constructing variable names  C is able to distinguish between the variable names by making it compulsory for you to declare the type of any variable name which you are going to use in the program.  The type declaration is done at the beginning of the program. Example char emp_name int emp_age float emp_sal

27 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:27 Keywords  Keywords are the words whose meaning has already been explained to the C compiler.  The keywords cannot be used as a variable name.  Keywords are also called as ‘Reserve Words’.  There are total 32 keywords available in C.

28 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:28 INSTRUCTIONS There are basically four types of instructions: 1)Type Declaration Instructions 2)Input/Output Instruction 3)Arithmetic Instruction 4)Control Instructions

29 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:29 TYPE DECLARATION INSTRUCTION  This instruction is used to declare the type of variables being used in the program.  Any variable used in the program must be declared before using it in any statement.  The type declaration is usually done at the beginning of the program.  Example: char emp_name int emp_age int emp_age float emp_sal float emp_sal

30 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:30 INPUT OUTPUT INSTRUCTIONS This instruction is used to perform the function of supplying input data to a program and obtaining the output results from it.

31 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:31 ARITHMETIC INSTRUCTIONS  A C arithmetic instruction consist of a variable name on the left hand side of = and variable name and constant on the right hand side of =.  The variables and constants on the right hand side of = are connected by arithmetic operators +, -, * and /. a + b = c

32 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:32 EXAMPLE int ab; float alpha, beta, gamma; ab = 1200; alpha = beta * gamma + 3.2 * 2/5;

33 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:33 Where, ab = integer variable alpha, beta, gamma = real variable 1200, 2, 5 = integer constants 3.2 = real constant = = assignment operators +, -, *, / = arithmetic operators

34 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:34 TYPES OF ARITHMETIC STATEMENT There are basically three types of arithmetic statement: 1)Integer Mode Arithmetic Statement 2)Real Mode Arithmetic Statement 3)Mixed Mode Arithmetic Statement

35 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:35 INTEGER MODE ARITHMETIC STATEMENT  This is an arithmetic statement in which all operands are either integer variables or integer constants.  The variables and constants together are called ‘operands’.  Example: ab * 34;

36 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:36 REAL MODE ARITHMETIC STATEMENT  This is an arithmetic statement in which all operands are either real variables or real constant.  Example: ab * 3.4

37 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:37 MIXED MODE ARITHMETIC STATEMENT  This is an arithmetic statement in which some of the operands are integer and some of the operands are real.  Example: 4 * 4.5

38 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:38 INTEGER AND FLOAT CONVERSION  An arithmetic operation between an integer and integer always yields an integer result.  An arithmetic operation between a real and real always yields a real result.  An arithmetic operation between an integer and real always yields a real result.

39 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:39 TYPE CONVERSION EXAMPLE: int a; float b; a = 3.5; b = 3;

40 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:40  The value 3.5 cannot be stored in a since it is of type int. In such a case the float is demoted to an int.  The value stored in a is going to be 3.  Similarly, the value 3 cannot be stored in b since it is of type float. In such a case integer is promoted to float.  The value stored in b is going to be 3.000000.

41 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:41 CONTROL INSTRUCTIONS  This instruction is used to control the sequence of execution of various statements in a C program.  There are basically four types of instructions:  Sequence Control Instructions.  Decision and Case Control Instructions.  Loop Control Instructions.

42 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:42 Sequence Control Instructions This instruction ensures that the instructions are executed in the same order in which appear in the program. Decision and Case Control Instruction This instruction allows the computer to take a decision as to which instructions are to be executed next.

43 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:43 LOOP CONTROL INSTRUCTIONS This instruction allows the computer to execute a group of statements repeatedly. A / B * C A / B * C 1 st Option (A / B) * C 2 nd Option A / (B * C) Ambiguity Results

44 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:44 HIERARCHY OF OPERATORS The priority in which the operators in an arithmetic statement are performed is called hierarchy of operators. PriorityOperatorsDescription 1 st *, /, % Multiplication, Division, Modular Division 2 nd +, - Addition, Subtraction 3 rd =Assignment

45 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:45 EXAMPLE i = 2*3/4+4/4+8-2+5/8 i = 6/4+4/4+8-2+5/8 Operation * i = 1+4/4+8-2+5/8 Operation / i = 1+1+8-2+5/8 Operation / i = 1+1+8-2+0 Operation / i = 2+8-2+0 Operation + i = 10-2+0 Operation + i = 8+0 Operation – i = 8 Operation +

46 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:46 PROGRAM  Any C program is nothing but a combination of functions.  C is also called as procedure-oriented language.  Main() is one of such function.  Empty parenthesis after the function are necessary.

47 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:47 STRUCTURE OF A C PROGRAM # include # include void main() { float no1=1.5, no2=3.6, sum; float no1=1.5, no2=3.6, sum; sum=no1 + no2; sum=no1 + no2; }

48 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:48  Stdio = Standard input output  h = header file  #include means include stdio.h file to a program  Conio = Console input output  #include means include conio.h file to a program.

49 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:49  Void means does not return any value.  Operating system calls main().  Void main() means main() is not going to return any value to operating system.  The set of statements belonging to a function should be enclosed within a pair of braces.

50 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:50  Variable Declaration should be done immediately after { braces.  Any C statements ends with a semicolon.  Alt+F9 to compile the program means to check for errors.  If it is error free then Ctrl+F9 to run (execute) the program.

51 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:51  To see the output you have to do Alt+F5.  If we want to print something on to the screen the function available is printf().  The syntax is: Printf(“format string”, list of variables);

52 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:52  Format string could be: %d for printing integer value %f for printing float value %c for printing character value  In the above example if we want to print the value of a variable sum then printf(“%f”,sum);

53 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:53  If we want to accept data from the keyboard then use the function called scanf().  The syntax is: scanf(“format string”, &list of variables);  Format string is same as printf.  In the above example the value of no1 and no2 should be entered from the keyboard as follows: scanf(“%f%f”,&no1,&no2)

54 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:54  If we want to take the cursor to a next line then use \n.  If you want to put some comment in a program which should not be executed while running the program that can be done through /*……………………………………….*/  Example /*ADDITION OF TWO NUMBERS*/

55 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:55  If you want to clear the screen everytime when you run the program then use clrscr() function.  After running the program you want to hold the screen for displaying the output then use getch() function.  Getch() means get a character from the keyboard.

56 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:56 MODIFIED PROGRAM #include #include Void main() { float no1, no2, sum;/*variable declaration*/ float no1, no2, sum;/*variable declaration*/ clrscr();/*To clear the screen*/ clrscr();/*To clear the screen*/ scanf(“%f%f”,&no1, &no2);/*Input values*/ scanf(“%f%f”,&no1, &no2);/*Input values*/ sum = no1 + no2; sum = no1 + no2; printf(“%f”,sum); /*printing the value of sum*/ printf(“%f”,sum); /*printing the value of sum*/ getch(); getch(); }

57 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:57 #include #include Void main() { float no1, no2, sum;/*variable declaration*/ float no1, no2, sum;/*variable declaration*/ clrscr();/*To clear the screen*/ clrscr();/*To clear the screen*/ printf(“Enter two numbers:”); printf(“Enter two numbers:”); scanf(“%f%f”,&no1, &no2);/*Input values*/ scanf(“%f%f”,&no1, &no2);/*Input values*/ sum = no1 + no2; sum = no1 + no2; printf(“The addition of two numbers is %f”, sum); printf(“The addition of two numbers is %f”, sum); getch(); getch(); }

58 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:58 ASSIGNMENTS  Write a program to calculate the simple interest.  Write a program to find the area of a circle.  Write a program to accept a five digit number and calculate its sum.

59 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:59 ASSIGNMENTS  Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate gross salary.  Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

60 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:60 ASSIGNMENTS  If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.

61 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:61 ASSIGNMENTS  The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area and perimeter of the rectangle, and the area and circumference of the circle.

62 Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:62 ASSIGNMENTS  If a five-digit number is input through the keyboard, write a program to reverse the number.  If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number.


Download ppt "Copyrights© 2008 BVU Amplify DITM Software Engineering & Case Tools Page:1 INTRODUCTION TO ‘C’ LANGUAGE Chapter 2."

Similar presentations


Ads by Google