Basic Programming Skills/ Foundations of Computer Programming DCAP102/DCAP401 Editor Dr
Total Page:16
File Type:pdf, Size:1020Kb
Basic Programming Skills/ Foundations of Computer Programming DCAP102/DCAP401 Editor Dr. Anil Sharma www.lpude.in DIRECTORATE OF DISTANCE EDUCATION BASIC PROGRAMMING SKILLS/ FOUNDATIONS OF COMPUTER PROGRAMMING Edited By Dr. Anil Sharma ISBN: 978-93-87034-75-4 Printed by EXCEL BOOKS PRIVATE LIMITED Regd. Office: E-77, South Ext. Part-I, Delhi-110049 Corporate Office: 1E/14, Jhandewalan Extension, New Delhi-110055 +91-8800697053, +91-011-47520129 [email protected]/[email protected] [email protected] www.excelbooks.com for Lovely Professional University Phagwara CONTENTS Unit 1: Foundation of Programming Languages 1 Manmohan Sharma, Lovely Professional University Unit 2: Introduction to C Language 19 Kumar Vishal, Lovely Professional University Unit 3: Basics - The C Declaration 36 Anil Sharma, Lovely Professional University Unit 4: Operators 48 Yadwinder Singh, Lovely Professional University Unit 5: Managing Input and Output in C 61 Anuj Sharma, Lovely Professional University Unit 6: Decision-making and Branching 91 Balraj Kumar, Lovely Professional University Unit 7: Decision-making and Looping 126 Mandeep Kaur, Lovely Professional University Unit 8: Arrays 155 Kanika Sharma, Lovely Professional University Unit 9: Strings 168 Sarabjit Kumar, Lovely Professional University Unit 10: Pointers 187 Anil Sharma, Lovely Professional University Unit 11: Functions 209 Anil Sharma, Lovely Professional University Unit 12: Union and Structure 237 Sarabjit Kumar, Lovely Professional University Unit 13: File Handling in C 266 Anil Sharma, Lovely Professional University Unit 14: Additional in C 282 Avinash Bhagat, Lovely Professional University SYLLABUS Basic Programming Skills/Foundations of Computer Programming Objectives: It imparts programming skills to students. Students will be able to: Understand the structure of a C/C++ language program including the use of variable definitions, data types, functions, scope and operators. Be able to develop, code, and test well structured programs using: if-then logic, while, do, and for loops, functions, arrays, strings and string functions Sr. No. Description 1. Introduction: ANSI C standard, Overview of Compiler and Interpreters, Structure of C Program, Programming rules, Execution 2. Basics – The C Declarations: C Character Set, keywords, : Identifiers, data types, operators, constants and variables Operators & Expressions 3. Input/ Output in C: Formatting input & output functions. 4. Decision-making Statements: if, else if Control Statements: For, do while, while. Control transfer statements - break, continue. 5. Arrays and Strings: Defining arrays; I/O of arrays, I/O of string data; built-in library functions to manipulate strings, array of strings 6. Pointer: Introductions, Features, Declaration, Pointers and Arrays, pointers to pointers ,Pointers and strings, Void Pointers 7. Functions: Defining and accessing a functions, passing arguments – call by value, function prototypes, recursive functions Storage Classes: Storage classes and their usage 8. Structures & Unions: Defining and processing structures, array of structures, nested structures, Unions & difference from Structures 9. Files: Opening, reading, writing & Closing file 10. Additional in C: Dynamic memory allocation, Memory models, Linked List Manmohan Sharma, Lovely Professional University Unit 1: Foundation of Programming Languages Unit 1: Foundation of Programming Languages Notes CONTENTS Objectives Introduction 1.1 Programming Language 1.2 Assembly Language 1.3 Assembly Program Execution 1.4 Assembler 1.5 Assembly Program and its Components 1.6 Machine Level Language 1.7 Higher Level Languages 1.8 Compiling High Level Language 1.9 Some High Level Languages 1.10 Summary 1.11 Keywords 1.12 Self Assessment 1.13 Review Questions 1.14 Further Readings Objectives After studying this unit, you will be able to: Explain programming language Describe assembly language Describe how to execute assemble program Explain higher level language Describe some high level language Introduction Computer is an electronic device which works on the instructions provided by the user. As the computer does not understand natural language, it is required to provide the instructions in some computer understandable language. Such a computer understandable language is known as Programming language. A computer programming language consists of a set of symbols and characters, words, and grammar rules that permit people to construct instructions in the format that can be interpreted by the computer system. LOVELY PROFESSIONAL UNIVERSITY 1 Basic Programming Skills/Foundations of Computer Programming Notes Computer Programming is the art of making a computer do what you want it to do. Computer programming is a fi eld that has to do with the analytical creation of source code that can be used to confi gure computer systems. Computer programmers may choose to function in a broad range of programming functions, or specialize in some aspect of development, support, or maintenance of computers for the home or workplace. Programmers provide the basis for the creation and ongoing function of the systems that many people rely upon for all sorts of information exchange, both business related and for entertainment purposes. 1.1 Programming Language Different programming languages support different styles of programming. The choice of language used is subject to many considerations, such as company policy, suitability to task, availability of third-party packages, or individual preference. Ideally, the programming language best suited for the task at hand will be selected. Trade-offs from this ideal involve fi nding enough programmers who know the language to build a team, the availability of compilers for that language, and the effi ciency with which programs written in a given language execute. The basic instructions of programming language are: 1. Input: Get data from the keyboard, a fi le, or some other device. 2. Output: Display data on the screen or send data to a fi le or other device. 3. Math: Perform basic mathematical operations like addition and multiplication. 4. Conditional execution: Check for certain conditions and execute the appropriate sequence of statements. 5. Repetition: Perform some action repeatedly, usually with some variation. 1.2 Assembly Language Assembly languages are also known as second generation languages. These languages substitute alphabetic symbols for the binary codes of machine language. In assembly language, symbols are used in place of absolute addresses to represent memory locations. Mnemonics are used for operation code, i.e., single letters or short abbreviations that help the programmers to understand what the code represents. e.g.: MOV AX, DX. Here mnemonic MOV represents ‘transfer’ operation and AX, DX are used to represent the registers. One of the fi rst steps in improving the program preparation process was to substitute letter symbols mnemonics for the numeric operation codes of machine language. A mnemonic is any kind of mental trick we use to help us remember. Mnemonics come in various shapes and sizes, all of them useful in their own way. Example: A computer may be designed to interpret the machine code of 1111 (binary) or 15 (decimal) as the operation ‘subtract’, but it is easier for human being to remember is as SUB. 2 LOVELY PROFESSIONAL UNIVERSITY Unit 1: Foundation of Programming Languages Use of Symbols Instead of Numeric of OpCodes Notes All computers have the power of handling letters as well as numbers. Hence, a computer can be taught to recognize certain combination of letter or numbers. It can be taught to substitute the number 14 every time it sees the symbol ADD, substitute the number 15 every time it sees the symbol SUB, and so forth. In this way, the computer can be trained to translate a program written with symbols instead of numbers into the computer’s own machine language. Then we can write program for the computer using symbols instead of numbers, and have the computer do its own translating. This makes it easier for the programmer, because he can use letters, symbols, and mnemonics instead of numbers for writing his programs. Example: The preceding program that was written in machine language for adding two numbers and printing out the result could be written in the following way: CLA A ADD B STA C TYP C HLT Which would mean “take A, add B, store the result in C, type C, and halt.” The computer by means of a translating program, would translate each line of this program into the corresponding machine language program. Advantages of Assembly Language The main advantages of assembly language are: 1. Assembly language is easier to use than machine language. 2. An assembler is useful for detecting programming errors. 3. Programmers do not have to know the absolute addresses of data items. 4. Assembly languages encourage modular programming. Disadvantages of Assembly Language The main disadvantages of assembly language are: 1. Assembly language programs are not directly executable. 2. Assembly languages are machine dependent and, therefore, not portable from one machine to another. 3. Programming in assembly language requires a higher level of programming skill. 1.3 Assembly Program Execution An assembly program is written according to a strict set of rules. An editor or word processor is used for keying an assembly program into the computer as a fi le, and then the assembler is used to translate the program into machine code. LOVELY PROFESSIONAL UNIVERSITY 3 Basic Programming