Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
×
8 of 64

# CSEG1001Unit 2 C Programming Fundamentals

2

Share

University of Petroleum and Energy Studies

See all

See all

### CSEG1001Unit 2 C Programming Fundamentals

1. 1. CSEG1001 Computer Programming
2. 2. CSEG 1001 Computer Programming Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
3. 3. Road Map • Generations of Computers and Languages • Organization of Computers-Online Lecture • Number Systems Conversion • Logical Analysis and Thinking Introduction to Computers • Structure of C Program & Compilation and Linking Process • Variables and Datatypes • Managing Input and Output statements and Operators • Decision and Looping Statements C Programming Basics • Creation and Usages • 1D and 2 D arrys • String Functions • Matrix operations Arrays and Strings • Declaration and Definitions of Functions • Passing Arguments • Recursion • Pointers & Pointer Arithmetic Functions and Pointers • Need of Structure and Unions • Declaration and Definition • Storage classes • Preprocessor Directives Structures and Unions CSEG1001 Computer Programming
4. 4. LECTURE #8 STRUCTURE OF C PROGRAM ..COMPILATIONAND LINKING Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
5. 5. Steps in C compilation • Compiling a C program is a multi-stage process • It contains four separate stages: • Traditional C compilers orchestrate this process by invoking other programs to handle each stage. CSEG1001 Computer Programming Preprocessing Compilation Assembly Linking
6. 6. CSEG1001 Computer Programming
7. 7. Compiling and Executing C Program CSEG1001 Computer Programming
8. 8. Files in a C program CSEG1001 Computer Programming
9. 9. Structure of C Program CSEG1001 Computer Programming
10. 10. Documentation • Comments • provide clarity to the C source code • allows others to better understand • helps in debugging the code. • Two types • Single Line // • Multiline /* any text */ CSEG1001 Computer Programming
11. 11. Hello World in C // This is my first Program /* Written for Engineering students of Petroleum University */ #include <stdio.h> void main() { printf(“Hello”); } 11 Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
12. 12. Preprocessing • First stage of compilation • Lines starting with a # character are interpreted by the preprocessor as preprocessor commands. • This language is used to reduce repetition in source code • Print the result of the preprocessing stage, pass the -E option to cc: #include <stdio.h> #include <stdlib.h> #include <string.h> CSEG1001 Computer Programming
13. 13. Function / Block • Definition: Function/Procedure/Sub-routine • Set of Instructions that are executed to achieve a particular task • Code reusability • Types • Built-in Functions (System Defined) • E.g. printf(), scanf() • User-defined Functions 13 Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
14. 14. Main() function  main() is always the first function called in a program execution. void main( void ) or int main() { … {….  void indicates that the function takes no arguments  int indicates that the function returns an integer value CSEG1001 Computer Programming
15. 15. LECTURE #9 VARIABLESAND DATATYPES Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
16. 16. Data & Variable CSEG1001 Computer Programming
17. 17. DataType in C language CSEG1001 Computer Programming
18. 18. 18
19. 19. Variables • Naming a Variable • Must be a valid identifier. • Must not be a keyword • Names are case sensitive. • Variables are identified by only first 32 characters. • length • lowerLimit lower_limit • incomeTax income_tax
20. 20. Variable Declaration / Definition • Declaring/Definition a Variable • Declaration announces the data type of a variable and allocates appropriate memory location. • Each variable used must be declared. • A form of a declaration statement is data-type var1, var2,…; Examples int sum = 0; char t1 = ‘a’; float epsilon = 1.44;
21. 21. Variable Definition and Initialization If not initialized it will take a garbage value. CSEG1001 Computer Programming
22. 22. 22 Global and Local Variables • Global Variables • These variables are declared outside all functions. • Life time of a global variable is the entire execution period of the program. • Can be accessed by any function defined below the declaration, in a file. • Local Variables • These variables are declared inside some functions. • Life time of a local variable is the entire execution period of the function in which it is defined. • Cannot be accessed by any other function. • In general variables declared inside a block are accessible only in that block.
23. 23. Example 23
24. 24. Constants • The value will not change wherever declared • Declared using const keyword • Eg. const float pi=3.14; CSEG1001 Computer Programming
25. 25. LECTURE #10 INPUTAND OUTPUT STATEMENTS Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
26. 26. OUTPUT STATEMENT - printf() • printf() function • Output to Standard Output • Prototype definition available in stdio.h 26 Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
27. 27. • Case 1: printing only text Flowchart code Output printf(“Hello”); Hello • Case 2: printing only value of a variable printf(“%d” , area); 120 • Case 3: printing text and a value printf(“The value of area is %d”,area); The value of area is 120 Print Hello Print area OUTPUT STATEMENT - printf()
28. 28. INPUT STATEMENT – scanf() • Case 1: getting 1 input value scanf( “%d”, &r); • Case 2: getting 2 input value scanf( “%d %d ”, &l, &b); • Case 3: getting 3 input value scanf( “%d %d %d ”, &a, &b ,&c);
29. 29. • scanf() function • Input from Standard Input • Prototype definition available in stdio.h • Format Specifier(%d) • Address-of Operator (&) • Provides the memory address of the input variable were the input value is to be stored 29 Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
30. 30. Format Specifiers in C • Used in association with printf() and scanf() CSEG1001 Computer Programming
31. 31. ONLINE LECTURE #11 OPERATORS IN C Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
32. 32. Online lectures CSEG1001 Computer Programming
33. 33. CSEG1001 Computer Programming
34. 34. CSEG1001 Computer Programming
35. 35. Ternary Operator - Conditional Check Operator CSEG1001 Computer Programming
36. 36. Example CSEG1001 Computer Programming
37. 37. LECTURE #12 DECISION STRUCTURES IN C Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
38. 38. List of Decision Making Structures in C •If structure •If Else structure •Nested If structure •Switch Structures •Conditional Operator/ Terniary operator CSEG1001 Computer Programming
39. 39. Converting a if block to C program CSEG1001 Computer Programming if(condition) { //true statements }
40. 40. Check if entered number is positive CSEG1001 Computer Programming
41. 41. Converting a if-else block to C program CSEG1001 Computer Programming if(condition) { //true statements } else { //false statements }
42. 42. Example: If Else Statement CSEG1001 Computer Programming
43. 43. Nested IF CSEG1001 Computer Programming if(condition 1) { //true statements } else if(condition 2) { //statements } else if(condition 3) { //statements } else { //statements }
44. 44. Example CSEG1001 Computer Programming
45. 45. Switch case Vs Nested IF CSEG1001 Computer Programming
46. 46. Convert Switch to C program CSEG1001 Computer Programming
47. 47. Knowledge Checks #include <stdio.h> void solveMeFirst( ) { int num1,num2; scanf("%d %d",&num1,&num2); int sum; sum = num1+num2; printf("%d",sum); } void main() { ________________Fill me } CSEG1001 Computer Programming
48. 48. Knowledge Checks CSEG1001 Computer Programming
49. 49. EXPERIMENT NO – 4 Control Statements in C Language List of lab works: 1. Write a program to accept 3 numbers and find the greatest of them, using if…….else statements. 2. Write a program to find the biggest of 3 numbers using conditional operator/ternary operator? 3. Write a program to check whether the roots of a quadratic equation are real or imaginary? 4. Program to find the average of students marks, if average<50 then result is ‘FAIL’ otherwise print the grade as pass /first class/distinction. 5. A book and stationary store decides to give its customers 10% discount on a purchase greater than 10,000/-. The program should accept the quantity purchased the price of the items and then calculate the amount payable. Further based on the total amount, appropriate discount should be given and final payable amount should be displayed. 6. Write a program to accept a number and display “Sunday/Monday/Tuesday…..” Based on the number. (hint: if 1 is input then “Sunday”, if 2 is input then “Monday”…..) using switch case. 7. Read the minutes from the keyboard and find out the no. of hours, mins , days ? (Ex 1210 mins are displayed as 0 days,20 hrs,10 min) CSEG1001 Computer Programming
50. 50. LECTURE #13 LOOP STRUCTURES IN C Instructor Dhiviya Rose J . Asst. Prof. Senior Scale School of Computer Science and Engineering | UPES CSEG1001 Computer Programming
51. 51. Introduction - Loop Statements • Helps in executing a statement in flowchart repeatedly • Loop statements – block of statement executed more than one time • How many times the loop statements are executed is managed by a counter variable • Counter Variable Initialization • Eg. C=0 • Counter Variable Increment/Decrement • Eg. C++, c=c+1,c=c+3 • Counter Variable Condition Check • Eg. C<3 CSEG1001 Computer Programming
52. 52. List of Loop Structures in C • While • Do….While • For CSEG1001 Computer Programming
53. 53. While • Entry condition check loop CSEG1001 Computer Programming
54. 54. CSEG1001 Computer Programming
55. 55. Example – While Program CSEG1001 Computer Programming
56. 56. Do … While Loop • Exit check Loop • At least executes the statement once if the condition is false CSEG1001 Computer Programming
57. 57. At least executes once - if condition fails CSEG1001 Computer Programming
58. 58. CSEG1001 Computer Programming
59. 59. For CSEG1001 Computer Programming
60. 60. Example CSEG1001 Computer Programming
61. 61. CSEG1001 Computer Programming
62. 62. Continue statement CSEG1001 Computer Programming
63. 63. EXPERIMENT NO – 5 Loop Statements in C Language List of lab works: 1. Write a program to print half pyramid using *. 2. Write a C program to print all natural numbers from 1-n using while loop. 3. Write a C program to find the sum of all even numbers between 1 to n using do-while loop. 4. Write a C program to print multiplication table of any number using for loop. CSEG1001 Computer Programming
64. 64. CSEG1001 Computer Programming