EEE 0115 Chapter 2 Introduction to C Programming

  • Slides: 20
Download presentation
EEE 0115 Chapter 2: Introduction to C Programming 1 C How to Program Deitel

EEE 0115 Chapter 2: Introduction to C Programming 1 C How to Program Deitel & Deitel

2 First C Program: Printing a Line of Text Second C Program: Adding Two

2 First C Program: Printing a Line of Text Second C Program: Adding Two Integers Memory Concepts Arithmetic in C Decision Making: Equality and Relational Operators

3 First C Program: Printing a Line of Text

3 First C Program: Printing a Line of Text

4 First C Program: Printing a Line of Text The textual information given between

4 First C Program: Printing a Line of Text The textual information given between /* and */ is called as comments. Comments are not executable statements. They are used to inform users of the program. #include <stdio. h> #include is a preprocessor directive and used to load a specific file. In this example, the file is stdio. h and it is used for standard input/output operations.

5 First C Program: Printing a Line of Text Each C program must have

5 First C Program: Printing a Line of Text Each C program must have main() function. int main() – int shows that main function returns integer value. Like in all functions, { and } braces are used to specify function body in main function.

6 First C Program: Printing a Line of Text printf("Welcome to C!n"); Printf is

6 First C Program: Printing a Line of Text printf("Welcome to C!n"); Printf is used to print a string of characters given in quotes. All statements like printf must end with semicolon (; ) is used to specify an escape character. In this example n is the newline character. n Newline t Horizontal tab a Alert \ Backslash " Double quote

7 First C Program: Printing a Line of Text return 0; It shows that

7 First C Program: Printing a Line of Text return 0; It shows that C program terminated succesfully without any problem. } right brace indicates main function ends.

8 First C Program: Printing a Line of Text

8 First C Program: Printing a Line of Text

9 Second C Program: Adding Two Integers

9 Second C Program: Adding Two Integers

10 Second C Program: Adding Two Integers integer 1, integer 2, sum; is used

10 Second C Program: Adding Two Integers integer 1, integer 2, sum; is used to define variables. Variable names include letters and digits. They are case sensitive. Variable declarations are placed before executable statements. int variables hold integers.

11 Second C Program: Adding Two Integers scanf("%d", &integer 1 ); is used to

11 Second C Program: Adding Two Integers scanf("%d", &integer 1 ); is used to get a value from user. %d shows that the input will be an integer &integer 1 shows the location in which the input value will be stored. = assignment operator is used to assign a value to a variable. printf("Sum is %dn", sum ); is used to print a string of characters and a value of a variable. %d shows that a decimal integer will be printed. sum is the variable name to be printed on the screen. Calculations can be performed inside printf statements.

12 Memory Concepts The variable names actually correspond to locations in the computer’s memory.

12 Memory Concepts The variable names actually correspond to locations in the computer’s memory. Each variable has a name, type, size and a value. Reading variables does not modify their values. When you place a new value to a variable, it overwrites the old value.

13 Arithmetic +, - addition and subtraction *, / multiplication and division İnteger division

13 Arithmetic +, - addition and subtraction *, / multiplication and division İnteger division produce integer result. % operator finds the remainder 9%4 returns 1 Some operators have precedence over other operators Multiplication and division have higher precedence than addition and subtraction You may use parenthesis.

14 Arithmetic

14 Arithmetic

15 Decision Making If the condition given in a if control statement is true,

15 Decision Making If the condition given in a if control statement is true, the body of if statement is executed. If the condition given in a if control statement is false, the body of if statement is not executed. 0 is false, non-zero values is true.

16 Decision Making

16 Decision Making

17 Decision Making

17 Decision Making

18 Decision Making

18 Decision Making

19 Decision Making

19 Decision Making

20 Operators Associativitiy

20 Operators Associativitiy