1 / 78

Advanced C Programming

Advanced C Programming. Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani. Course Information. One Lab / Week Saturday – 5:00 -6:30 P.M Total Labs: 11 (Planned) Room No: 1101. Two Lectures / Week Monday – 5:30 -7:00 P.M Wednesday –5:30 – 7:00 P.M Total Lectures: 22 (Planned)

Download Presentation

Advanced C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced C Programming Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani

  2. Course Information • One Lab / Week • Saturday – 5:00 -6:30 P.M • Total Labs: 11 (Planned) • Room No: 1101 • Two Lectures / Week • Monday – 5:30 -7:00 P.M • Wednesday –5:30 – 7:00 P.M • Total Lectures: 22 (Planned) • Room No: 5103

  3. Evaluation Quiz-5 In Class[5M] Assignments-02Take home[10M] Online Test -02 In Lab [10 + 15 M] Final Exam 120minutes[30M] Any absence for the evaluation components must be pre-approved by the instructor. Attendance-- 75%

  4. Top-Down Approach Structured programming is often (but not always) associated with a "top-down" approach to design. Designers map out the large scale structure of a program in terms of smaller operations, implement and test the smaller operations, and then tie them together into a whole program.

  5. Top-down Design • How do you solve the sub-problem? • Divide-and-conquer again! • Steps • Divide the problem into sub-problems • Repeat step 1 for each sub-problem until the problem is small enough to have an atomic solution. 3. Combine the solutions.

  6. Task Subtask1 Subtask3 Subtask2 Subtask1.1 Subtask1.2

  7. Top-Down Design – Example 1 • Problem: Compute a solution to a quadratic equation. • Input: Real Numbers a,b,c • Output: x such that ax^2 + bx + c = 0 • Fact: • x = (-b + sqrt(b^2 – 4ac)) / (2a) is a solution.

  8. Top-Down Design – Example 1 Compute x s.t. ax^2+bx+c=0 Compute (-b + sqrt(b^2 – 4ac)) / (2a) Compute (-b + d) / (2a) Compute d = sqrt(b^2 – 4ac)

  9. Top-Down Design – Example 2 • Problem: Compute the sum of n natural numbers • Input: n • Output: sum(n) = 1 + 2 + ……+n-1 + n

  10. Example2:Sum(n)=1+2+ ….(n-1)+n + Sum(n-1) n + Sum(n-2) n-1 ……………….

  11. Top-Down Design – Example 3 Compute xy where y is a power of 2. Input: x and y Output: xy

  12. Example 3 – xy xy * xy/2 xy/2 * * xy/4 xy/4 xy/4 … … Consider xy, where y=2k

  13. Top-Down Model In the Top-Down Model an overview of the system is formulated, without going into detail for any part of it. Each part of the system is then refined by designing it in more detail. Each new part may then be refined again, defining it in yet more detail until the entire specification is detailed enough to begin development. Top down approaches emphasize planning, and a complete understanding of the system.

  14. Programming Languages Machine languages Strings of numbers giving machine specific instructions Example: +1300042774 +1400593419 +1200274027

  15. Programming Languages Assembly languages English-like abbreviations representing elementary computer operations (translated via assemblers) Example: LOAD PRICE ADD TAX STORE COST

  16. Programming Languages High-level languages Similar to everyday English and use mathematical notations (translated via compilers) Example: Cost = Price + Tax

  17. Syntax and Semantics • The syntax of a programming language: • Set of rules that specify allowable statements in the language. • Specification for the function of each word in statements in the language. • Similar to a grammar for a natural language. • The semantics of a programming language: • Rules for interpreting the computational operations specified by statements in a programming language.

  18. Program Development

  19. Introduction to C General purpose programming language. Closely related to UNIX Often referred as a system programming language as useful for writing compilers and operating systems.

  20. History of C 1960 – block-structured ALGOL emerged 1967 – Martin Richards developed BCPL (Basic Combined Programming Language) 1970 – Ken Thompson developed B while working on DEC PDP-7, B originated from BCPL BCPL and B are both typeless languages 1972 – Dennis Ritchie and Ken Thompson developed C (with data types) – traditional C 1989-1990 – ANSI and ISO standard for C approved.

  21. Features of C • Portability • Implemented on many diverse systems • Implemented on wide variety of hardware • Defined libraries are themselves portable • Applications are portable • Programmes are portable • Powerful • Completeness – business to scientific applications • Flexibility – preprocessor, conditional compilation, header files

  22. Features of C • Structured Programming – readability and maintainability • Both a high level and low level language • High level – symbolic references, user written functions, powerful operator set, derived data types • Low level – allow reference to storage directly, bits and bit patterns manipulation

  23. Basic constructs of programming • Typically we have been given a task (problem) to solve, which we break down into several subtasks. • These modules or subtasks are to be converted into computer understandable format and this exercise is known as programming. • Based on the interdependency of these subtasks, the programming statements are categorized into three set of statements. • Sequential statements • Conditional statements • Iterative statements

  24. Basic constructs of programming 1. Sequential Statements: Sequential statements enable you to perform subtask1 to completion, then, Subtask2 etc.., in a sequence. The sequence never goes back to previous statements. 2. Conditional statements: If condition is true, do Subtask 1; else, do Subtask 2. It is important to realize here that conditional statements never permit to perform both the subtasks 3. Iterative statements: Iterative statements are the statements to perform particular subtask over and over, number of times, based on some condition as long as the test condition is true)

  25. How C programs work? • First of all you have to write a program using some text editor (in our case “vi”) and save it as somefile with a “.c” extension (eg: file1.c) • Then you have to compile it using a compiler (in our case it is gcc or cc). So you have to type in at the command prompt: gcc file1.c • This command would generate an executable file (a.out) in the pwd where you issued gcc, which will be the executable code in binary. ./a.out

  26. Comments • Just like “;” was used for single line comments in LC-2 assembly here “//” is used for single line comment in C • Unlike in LC2 assembly, C supports multi-line comments like the following Line 1 /* Line 2 Line 3 Line 4 */ Line 5 • Multi-line comments cannot be nested. Why?

  27. Working with vi • Text editing involves creation and storage of text inside files. • vi - a contribution of University of California, Berkeley, was developed by Bill Joy, then a graduate student, is available with all Unix systems • vi is a full screen text editor – it displays text on a full screen and allows users to navigate the text using the keyboard

  28. Vi Editors • Create a file • Edit a file • Save a file • Display the line numbers

  29. Creating file using vi • In shell prompt, vi <file_name> Note: • If <file_name> is already exist in current directory ,then it is opened by the vi editor.

  30. Opening Modes • 2 Modes • command mode (also called edit mode) • insert mode • Command Mode • allow you to alter text -- to fix typing errors, add or subtract lines, etc. • are not usually printed on the screen • Case sensitive

  31. Insert Mode • To add text to your file, use a vi command such as i. What you type in insert mode: • becomes part of the file you are editing • requires you to press the RETURN key to begin a new line of text. • To return to command mode, press the ESCAPE key.

  32. Few Useful commands • :w ,write to the file already named • :wq , write file and quit (:x) • :q , quit editing • :q! , quit without saving file • dd , delete line • :set number, sets up line numbers • :/text ,  will search for occurance of 'text' (hitting n will find next occurence)

  33. Compile & Build (A brief overview)

  34. Detailed Compilation Process

  35. Variable Declaration basics • Integer (int) • Character (char) • Real Number (float) • High Precision Real Numbers (double)

  36. Declaration of a Variable • The declaration of a variable will assign name to the variable and define the type of data that will be held in the location. marks Variable : Address or Location Value 10

  37. Operators • Arithmetic: +, -, *, /, % (binary) -, + (Unary) • Relational: <, <=, >, >=, ==,! = • Logical: &&, || , ! • Bit wise: &, |, ^, <<, >>, ~(complement) • Assignment: =, *=, +=, -=, /=, %=, &=, ^=,! =, <<=, >>= •  Brackets:()

  38. Arithmetic Operators int x,y,z; // Declaration x= 13; // initialization y= 5; z= x + y; z= x - y; z= x * y; z= x / y; z= x % y; // z=18 // z=8 // z=65 // z=2 // z = 3

  39. Arithmetic Operators: Operator precedence int x,y,z; x=3; y=5; z= x * 2 + y / 3; z= x – 2 * y; //z=7 //z=-7

  40. Structure of C Program

  41. General Format of a ‘C’ Program #include<libraries.h> main () { Declarations; Statements; } function1() { Declarations; Statements; } function2() { Declarations; Statements } -Program execution starts at main () -Every ‘C’ program must have one and only one main function -Semicolon acts as a statement separator

  42. Functions • ‘C’ is a modular language. ‘C’ programs consist of one or more functions. • Functions do a particular task (procedure or subroutine). • There are two types of functions: built-in and user defined. printf(“Hello World”); // built – in add(a,b); // user defined

  43. Function structure • Every function will have the following • A unique (for each program) function name • A set of inputs (input parameters that the function takes as input) • A single return type (data type of the value that the function should output) A Add4 A + B + C + D B C D

  44. How to write and use a function • Functions in C can be written and used only if the following parts exist • Function Declaration (just like variables, functions have to be declared before they are used) • Function definition (the actual code for the function) • Function call (invoking the function from some other function)

  45. Function problem • Now for explaining the way in which I build and use a function we will use the following example. • Write a C program to calculate the sum of three integers and print the output

  46. Function declaration or function signature • What should be the name of this function? • sum_of_three • What are the inputs? • A, B, C (three integers) • What are the data types of these inputs? • A is an integer (int A) • B is an integer (int B) • C is an integer (int C) • What is the output’s data type? • The output is sum of three integers. So the output must also be an integer if the three inputs are integers. (int) • So my function declaration would be the following int sum_of_three(int A, int B, int C);

  47. Function definition • Here you write the actual code: int sum_of_three(int A, int B, int C) { int result=0; result=A + B + C; return result; }

  48. Function calling or invoking • So we have declared and defined a function (sum_of_three). Now we have to call it, to actually use the function. • So let me assume I need to add three numbers 5, 102, 83 for instance and I want to store the sum of these three in a variable called y (y must be an integer: int y;) y=sum_of_three(5,102,83); • This above line must appear only within some function block ({ }).

  49. Functions • Functions could be of two types: • Built-in functions • Declared in header files eg: stdio.h, string.h, math.h • Defined in standard libraries along with the compiler • Called or invoked from your code • Example: printf(), scanf(), atoi() etc.... • User defined functions • These functions are those that you declare, define and use in your programs

More Related