c basics course material_version1.3_updated

147
1

Upload: abdelrahman-saad

Post on 21-Jul-2016

236 views

Category:

Documents


2 download

TRANSCRIPT

1

2

3

4

1. Tell the students why we need to know about programming ( machine language ) 2. Show them a simple compilation flow from c file to hex file ( IDE integrated

development environment)preprocessor (txt.c) – compiler (txt.o , .asm) – linker ( txt.exe , .hex)

1. Show them the Structure of the C file from #include to functions bodies if any

5

6

7

8

In windows:int 4 bytesShort 2 byteschar 1 bytefloat 4 byteslong 4 bytesdouble 8 bytes

9

10

Naming Convention: You have to stick to a clear naming convention in order to make your code readable, for example:Char u8NumOfEmployessLong u16Salaryint _myAge

11

12

13

14

15

16

17

18

For Example If (x==y) depends on if x really equals to y, but in the other hand if (x=y) it will depend on the value of y if it is equal to 0 so the evaluation is false else it is true.int x = 1; int y = 0; if (x=y) False, int x = 1; int y = 10; if (x=y) True.

19

Short circuit: The evaluation of an expression is discontinued if the value of a conditional expression can be determined early. Be careful of any side effects in the code.Examples:(3==3) || ((c=getchar())==’y’). The second expression is not evaluated. (0) && ((x=x+1)>0) . The second expression is not evaluated.

20

21

Notes: - AND is true only if both operands are true. - OR is true if any operand is true. - XOR is true if only one of the operand is true. - Shift Left by x equal multiplication by 2 power to x- Shift Right by x equal divide by 2 power to x

22

23

24

As a rule, the compiler promotes each term in an binary expression to the highest precision operand.

25

26

27

28

29

30

31

32

33

34

Show the student a simple program that takes a number from the Keyboard and then display it back on the screen (Echo)

35

Show the student a simple program that takes a number from the Keyboard and then display it back on the screen (Echo)

36

Show the student a simple program that takes a number from the Keyboard and then display it back on the screen (Echo)

37

A common pattern in C (and in most programming) languages is the following: if ( cond ) x=<expra >; else x=<exprb >;

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

Give the Students a program that has al logical error and tell them to debug and know the error source :

#include <stdio.h>#include <stdlib.h>

int main(){

int value,i;

printf("value before for loop is %d \n",value);for(i=0;i<10;i++);{

value++;}

printf("value before for loop is %d \n",value);

return 0;}

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

Give the student a simple initialization example

83

84

85

86

Give the student a simple initialization example

87

88

89

90

91

92

93

94

U can repeat any of the prev examples using functions

95

96

97

98

99

What is the scope of each variable in this example?

100

The compiler always reference the variable from the private scope first then the outer scopeSo first it will print 10, and then 25 line is printed.

101

102

Use multiple files in the project

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

St_employee : St stands for structure type (to increase readability)

134

S_employee : S stands for structure type (to increase readability)

135

S_employee : S stands for structure type (to increase readability)

136

137

138

Exercise on all typedefs

139

140

The preprocessor operation : is a stage before compilation to handle the preprocess keywords like #define, #include, #ifdef, and #ifndef.Simply it does some text replacement, addition, deletion on the code depending of the used keywords before the compilation.

Other keywords will be explained later with the convenient context.

141

142

143

144

145

146

147