Principles Of ProgrammingusingC Subject Code : 22POP13 CIEMarks: 50 SEEMarks: 50 Lecture Hours : 40 Credits : 03 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga 06HoursIntroductionto C Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • History: In 1822, Charles Babbage begandevelopingwhatfirst mechanical computer. • And then in 1833 he actually designed an Analytical Enginewhich was a general-purpose computer. • It contained an ALU, some basic flowchart principlesandtheconcept of integrated memory. Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • 1st Generation (1940 to 1955): Vacuumtubes usedforthecircuitry . Example: ENIAC, UNIVAC-1, EDVAC. • 2nd Generation (1957-1963): Advanced vacuumtubesortransistors used for the circuitry . Example: IBM 1620, IBM7094, CDC1604, CDC3600 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • 3rd Generation (1964-1971): Integrated circuitsusedforthecircuitry . Example: IBM-360 series, Honeywell-6000series, and the IBM-370/168. • 4th Generation (1971-1980): Microprocessors usedExample: STAR 1000, PDP 11, CRAY-1, CRAY-X-MP,andApple II Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • 5th Generation (1980 onwards): Ultra LargeScaleIntegrated circuits used for the circuitry . Example: IBM, Pentium, Desktop, Laptop, Notebook, Ultrabook, and so on. Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • A computer as an electronic device • Accept data as input • Perform the required mathematical and logical operations• Output the result Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers • A computer is a combination of hardware andsoftware• Hardware are the physical components of a computerlikeprocessor, memory, devices, monitor, keyboardetc. • Software is the set of programs or instructions that arerequired by the hardware to performvarious operationsasperthe requirement of users Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Basic Computer Organization • Computer basically performs five major operations1. Accepting data or instructions (input) 2. Storing data 3. Processing data 4. Displaying results (output) 5. Controlling and coordinating all operations inside a computer Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer 1. Memory 2. Disks 3. Processor 4. Peripheral Devices/Input and Output Devices Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Memory • Memory is an internal storage , used to store dataandprograms either temporarily or permanently• Dived as Primary memory and Secondary memory• Primary memory (Main Memory) holds instructionsanddata when a program is executing • Volatile in nature, high speed and Costly (RAMandROM) Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Disks • Secondary memory (Auxiliary) holds data andprogramsthat are not currently in use and provides long-termstorage• Non-volatile, low speed and cheaper (HardDisks) Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Disks Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Processor • Processor consists two main parts ALU&control unit(CU)• Also Registers, Execution unit &bus interfaceunit (BIU) Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Processor Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Pheripheral / IODevices • To interact with computer pheripheral devices required• Input Devices are used to feed data and instructionsintothecomputer Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Input Devices Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Input Devices Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Output Devices Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to computers Components of Computer: Output Devices Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigms• Paradigm is a fundamental style of programmingthat defineshow the sructure and basic elements of a Computerprogramwill be built. • The style of writing programs and the set of capabilitiesandlimitations that a particular programming languagehasdepends on the paradigm it supports. Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications 1. Monolithic programming - Emphasize on findingasolution2. Procedural programming - Lays stress on algorithms3. Structured programming - Focuses on modules 4. Object-oriented programming - Emphasiszes onClassesandObjects Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications 5. Logic-oriented programming - Focuses on goals, usualyexpressed inpredicatecalculas 6. Rule-oriented programming - Makes use of if-then-elserulesfor computation 7. Constraint oriented programming- Utilizes invariant relationships tosolveproblem Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Monolithic Programming ADB 10 Global Data BDB 20 SUM DB? MOV AX, A ADD AX, B Sequential Code MOV SUM, AX JMP STOP ….………… STOP: EXIT Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Monolithic Programming • Only one programming module • Cosnsists Global data and sequential code • Global data can be accessed and modified byanypartoftheprogram • All instructions are executed in specified sequence Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Monolithic Programming • Example: Assembly Language and BASICLimitations: - Serious threat to its Data Integrity- Do not support concept of subroutine - Program size is very big and difficult todebug Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Procedural Programming Global Data Subroutine Program Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Procedural Programming • Program divided into subroutines that access global data• Example: FORTRAN and COBOL Advantages: 1. Easy to write compare to monolithic program Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Procedural Programming Dis-advantages: 1. No concept of reusability 2. Requires more time and effort to write programs3. Programs are difficult to maintain 4. No data integrity Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Structured Programming Global Data Program Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Structured Programming - Modular programming, programbroken intomodules- Suggested by Corrado Bohm and Guiseppe Jacopini in1966- To down approach Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Structured Programming Advantages - Easy to understand - Code can be reused - Less time to code - Easy to debug - Easy to change - Emphasis on code instead on data Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Structured Programming Dis-Advantages - Not data-centered - Global data may inadvertently modified - Main focus is on functions Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Object Oriented Programming Object 1 Object 1 Object 3 Object 4 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Object Oriented Programming- Treats data is the critical element - Restricts free movement of data in system- Task-based and data-based - Task and data grouped together in entity knownobject Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs: ProgrammingParadigmsClassifications : Object Oriented Programming: Features- Programs are data centered Programs are dived in terms of Objects - Functions that operate on data - Data is hidden - New data and functions can be added as and whenrequired- Bottom up approach Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Designing Efficient Programs : SDLC AnalysisDesign Implementation Requirement S/W Deployment, Testing Training and support Maintenance Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Algorithms - Step by step procedure to solve the problem- Provides blue print to write program - Effective procedure for solving the probleminafinitesteps-> Must be presise -> Be unambiguous -> Must not be infinite statements -> Results must be obtained Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Flowcharts - Graphical / pictorial representation of problemsolvingstepsNotations (Oval / Rounded Rectangle) Start / Stop (Parallelogram) Input / Output (Rectangle) Processing / Computation steps Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Flowcharts - Graphical / pictorial representation of problemsolvingstepsNotations (Diamond Shape) Decision statements (Small circle) Connector (Arrows) Control Flow direction Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Flowcharts Advantages - Used for program documentation - Helpful to solve complex problems - Error free programs can be implement - Can be used to debug the program Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Flowcharts Dis-advantages - Time consuming and need more human resource- Flowchart of complex program become clumsy- Little modification in coding requires to redrawentireflowchart - No well-defined standards Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Pseudocodes - Compact and informal high-level descriptionof analgorithmUses structural conventions of programminglanguage- Focuses on logic of the program - Syntax neutral - Enhance human understandability of the solution Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Program Design Tools: Pseudocodes Example: 1. Read the radius of circle 2. Calculate are of the circle 3. Print the area of the circle 4. End Variables: redius of the circle, area Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Types of Errors Types of Errors Run-time Errors Compile-time Errors Linker Errors Logical Errors Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Types of Errors : Run-time Errors - Occurs during the execution of the programOccurs when some illegal operations like 1. Dividing a number by zero 2. Opening a file that already exists 3. Lack of free memory space 4. Finding square or logarithm of negative numbersRun-time errors may terminate programexecution Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Types of Errors : Compile-time Errors - Occurs during the compilation, classified as 1. Syntax Errors: When rules of the programminglanguageviolated 2. Semantic Errors: May comply with rules of programminglanguage but are not meaningful tothe compiler Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Types of Errors : Compile-time Errors - Occurs during the compilation, classified as 3. Logical Errors: Results unexpected and undesirableoutputWill not be detected by compiler May occur due to incorrect statements 4.Linker Errors: Occurs when linker is not abletofindthefunction definition for a givenprototype Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Testing and Debugging : Testing - An activity to verify correct behaviour of the programImplementation Stage 1. Unit Test: Testing single unit / module 2.Integration Test: Combined units and interfaces aretested3.System Test: Checks the entire system Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Testing and Debugging : Debugging - An activity that includes execution testing andcodecorrection- Main aim is locate the errors and fix themDiffrent Approaches are 1. Brute-Force Method: CPU registers and Memorylocations2.Backtracking Method: Locate first and tracingbackword3.Cause Elimination: List of all possible causes of anerrorisdeveloped Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Introduction - Developed early by 1970 by Dennis Ritchie at Bell Laboratory- Used by UNIX OS - Named ‘C’ because many featuers are derivedfromB Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Taxonomy ALGOL BPCL B Traditonal C K & R C ANSI C C99 C95 ANSI / ISO Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Background - Derived from ALGOL (first lanuage with blockstructure)- In 1967 Martin Richards developed BPCL(Typeless)- In 1970 Ken Thompson developed B- In 1972 , Dennis Ritchi developed C, supports datatypes- C was documented and popularized in book ‘TheCProgramming Language by Brian W. KernighanandDennisRitchie in 1978 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Background - In 1983, ANSI started working on standard for Candapprovedin 1989 known as ANSI C - In 1990, ISO adopted ANSI standard, became C89- In 1995 minor changes made on C89, so versionisC95- Later significant changes made on C95 and modifiedversionC99 came exist on 1999 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : C99 Features - Extension to character types to support non-EnglishcharactersBoolean Data Types - Extension to Integer type - Inclusion of type definitions in for statement - Inclusion of imaginary and complex types - Addition of // comment statements Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Characteristics of C- Structured programming - Rich set of built in functions - 32 Keywords - Efficient - Low level programming - Supports pointers Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Uses of C - Used for dystem programming - Used to develop compilers, libraries and interpretersforotherlanguage -Used as an intermediate language for implementationofotherlanguage - End user applications Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Structure of a CProgramDocument Section Link Section #include<stdio.h> Global Decleration int main() { int main() { Local Decleration Statement/s } printf(“Hello C”); } User defined functions Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Files used in CFiles in a C Program Source File Header File Object File Executable File Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Files used in C- Header files Ex: stdio.h stdlib.h string.h math.h- Source code files Ex: circle.c sample.c - Object files Ex: circle.o sample.o - Binary executable file Ex: circle.exe sample.exe Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Compiling and ExecutingCProgramSource File Compiler Object Files Linker Executable Files Library Files Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Comments - Single line comment statement //This is single line comment statement - Multi line comment statement /* This is multi line comment statement can use for multi lines */ Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : C Tokens - Tokens are basic building blocks in Clanguage- Smallest individual unit in C - Program is constructed using a combination of tokens- There are six types of tokens Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : C Tokens Tokens in C Keywords Variables Constants Strings Special Characters Operators Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Identifiers - An identifier is “name given to programelements”Which help us to identify elements of the program- Such as Variables, Keywords, Funtions etc Example: radius area p length int float void main printf scanf Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Identifiers Rules for forming identifier names 1. Names may formed with combinations of letters, digitsandunderscore Example: total sum2 sum_of 2. Names must begin with letter or underscore Valid: marks rollno _total Invalid: 2marks #rollno @usn Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Identifiers Rules for forming identifier names 3. Keywords cannot be used as identifier name Invalid: int float 4. Identifier names are ase sensitive sum Sum SUM all are different Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Identifiers Rules for forming identifier names 5. First 31 characters are significant summation_of_two_numbers salary_of_employee_for_december_month 6. Two successive underscores are not allowedInvalid: balance__of Valid: balance_of Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Keywords in C- Keywords are predefined words - Keywords meaning are fixed - Keywords are reserved, cannot be used as identifiers- Keywords must be written in lower case / small letter Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Keywords in Cauto break case char const continuedefault do double else enumexternfloat for goto if int longregister return short signed sizeof staticstruct switch typedef union unsignedvoidvolatile while Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Character set in C- Any letter from English Alphabet, digit or special symbol- Combined to form C token 1. English Alphabet: a - z and A - Z 2. Digits: 0 - 9 3. Special Characters: ~ # %^ &*{}<> = _ + - $/ () \ ; : [ ] ‘ “?. !|,@ Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Character set in C4. White space characters: White Space Characters\b Mea Blank Spac \t Horizontal T \v Vertical retu \r \f \n New Line Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Character set in C5. Escape sequence P Escape sequence\a Audible sign \b Backspace \t Tab \n New Line \v Vertical Tab \f N Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Basic Data Types in C Data Type Keywo Use Character char Integer int Floating Point float Double double Valuless void 1 in bytes Value -128 to 127 2 Use To storechara -32 32767 To storein numbers 3.4 3.4E+38 To storeflo point numbers 8 1.7E 1.7E+308 To sto floatingpoint numbers 0 No v 4 - Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Basic Data Types in C Data Typechar Size 1 unsigned -128 to 127 char 1 int 2 unsigned in bytes Value range 0 to 255 -32768 to 32767 int 2 0 to 65535 short int 2 -32768 to 32767 unsigned short int 2 0 to 65535 lon unsigned -2147483648 to 2147483647 long in 0 to 4294967295 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Basic Data Types in C Data Typefloat Size 4 double 3.4E-38 to 3.4E+38 8 long in bytes Value range 1.7E-308 to 1.7E+308 10 3.4E-4932 to 1.1E+4932 Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Variables - Name given to memory location - Using the variable, refers to adress of the memorylocationLogical Physical Memory Address Address Location . . . 1000 radius 1002 area . . . Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Variables Two kinds: 1. Numeric Variables: - Used to store integer or floatingpointvalues / numbers (321.5) - For integer variables, default values are signed 2. Character Variables: - Used to store characters- Values are placed insingleqoutes Example : ‘a’ ‘A’ ‘5’ ‘@’ Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Variables Declaring Variables: - Each variable must be declared before used intheprogram-To declare the variable, appropriate data type must beused- Data types indicates to compiler, kind of values will bestored in it - Variable must not be declared with void type Arun Kumar P Dept. of ISE, JNNCE, Shivamogga Introduction to C Introduction to C : Variables Places to Declare Variables: - Variables can be declared at 3 places 1. At global scope (Outside of all functions) 2. At loacl scope (Inside of the function) 3. At function definition (Formal parameters) Arun Kumar P Dept. of ISE, JNNCE, Shivamogga