Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming.

Similar presentations


Presentation on theme: "C Programming."— Presentation transcript:

1 C Programming

2 A S D F K L ;

3 Computer system Monitor CPU Key Board mouse

4 Parts of a computer Processor, memory, hard disk, input, output. In memory both the program lines and variables are kept Action is taking place in processor and results are stored there in the memory. Even the output is given in the output window and flipped back to the edit window

5 Computer system Monitor CPU Key Board mouse

6 Computer system memory Monitor Hard disk processor CPU Key Board mouse

7 Steps in executing a code
Write the code (source code) Compile the source code If errors go back to edit and correct and repeat compiling If no errors, the compiler has generated the machine (object code) Run the (object) code and so get the results

8 void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c);
} 4 a 7 b junk c

9 void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c);
} 4 a 7 b 11 c

10 void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c);
} 4 a 7 b 11 c Sum is

11 void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c);
} 4 a 7 b 11 c Sum is 11

12 printf(“ %d + %d is %d “,a,b,c); }
void main() { int a = 4,b = 7, c; c = a + b; printf(“ %d + %d is %d “,a,b,c); } 4 47 a 7 11 b 11 junk c 4 + 7 = 11

13 What is programming? Making the computer work as we want it to work Getting things done by the computer We write the program in the memory and make the processor work it out

14 What is programming? Programming is making the computer work as we want it to. ie. we will have to talk to a machine. Some syntax is there. Only a few syntaxes to be byehearted. A series of instructions to the processor, so that it can do the entire work, without any difficulty. Instructions in a tape recorder or MP3 player to make milk tea!

15 How do we make it work? We keep a program in memory and ask the processor to execute line by line It is like a boy following the instructions given to it, systematically and quickly, but without any common sense or mind reading capability. Fast unlike us, obedient unlike us and having no common sense like us?!

16 Vehicle v/s program If a new vehicle, draw, expert opinion, (----- )finally build and run. But this expert (compiler) will build it! So if there is a modification, show to expert/(compile) and only then run

17 The compiler is like an expert
Compiler is the expert who/which tells whether the syntax is correct! If not, we are to correct and again submit. Finally build and test run. Compiler will compile too! Compiler is also without common sense at times. Logical errors! Compiler will not correct logical errors. So to know what is happening, print the results, in between or check the values of variables.

18 In Input and output mechanism
If we are to see the result, project them onto the screen (printf) %d is a place holder in that order But the result screen will flip back to edit and so get it back by alt f5 order to get values from the key board, scanf

19 Variables and memory address
All the variables to be used must be declared (and so given locations). The dissection of a frog! The memory address is the uniquely identifying method. In a college, register number, in a town, the address,

20 The operations possible
Different operators, + - * / but % is also there! Integer on integer will give only an integer.

21 Some suggestions Meaningful variable names and function names Do proper indentation Results printed should be made meaningful Do hand tracing Draw flow charts! Feel free to ask doubts, loud clear and smart Free to ask. Loud and smart Please learn blind typing at the earliest Note that there are two types of slashes in KB Use the home and end keys wherever required Make your own changes and see whether the expected results are obtained

22 Neither copy nor mug up Do a hand tracing of the programs for understanding on the one side and to find out the mistakes on the other side Spend some time to plan the program using algorithm or flow chart Be sure that it is logic and no magic. Avoid printing results inside the loops Hard code first and then go for soft coding Get the addresses from the computer and draw a memory map

23 There are different slashes, \n, \t.... // /* and */
The operations possible Different operators, + - * / but % is also there! Integer on integer will give only an integer. The operator precedence issues. 5+2 * 3 = 11 v/s (5+2) * 3 = 21 Right to left and left to right 2/4*3 v/s 2*3/4 X+1 % 5 v/s (x+1) % 5;

24 See Programming in C #include<stdio.h>// header file inclusion void main() { printf(“Let Us C”); }

25 See Programming in C #include<stdio.h> void main()//a must for any C program to work { printf(“Let Us C”); }

26 See Programming in C #include<stdio.h> void main()//see the function braces { printf(“Let Us C”); }

27 See Programming in C #include<stdio.h> void main()//see the function braces // there is a space between void and main // main can not be capital! ie. C is case sensitive { printf(“Let Us C”); }

28 See Programming in C #include<stdio.h> void main()//function header { printf(“Let Us C”); }

29 #include<stdio.h> void main()//function header {
See Programming in C #include<stdio.h> void main()//function header { printf(“Let Us C”); } If there is a head, there shall be a body after/below the head

30 #include<stdio.h> void main()//function header {// body begins
See Programming in C #include<stdio.h> void main()//function header {// body begins printf(“Let Us C”); }// body ends If there is a head, there shall be a body after/below the head

31 See Programming in C #include<stdio.h> void main()//function header { printf(“Let Us C”);// end of an instruction }

32

33 End of an instruction is signaled by ;
Every program should have this function and only one main function. No capital M please DISSECTION of a basic C program Function braces, simple braces, ellipse. This is to hold or receive parameters or arguments. Presently it is holding nothing Function body begins main() { printf(“Welcome to see the sea of C”); } Function body ends printf function takes/holds necessarily one argument which is a string. Starting and ending by ” ” End of an instruction is signaled by ; Only one instruction or statement

34 ; #include<stdio.h> printf should have a prototype ? main() {
printf(“\n \tWelcome to see the sea of C”) } ; } We can add escape sequences and format specifiers in the string Statement missing Compound Statement missing

35 Welcome to see the sea of C
See that C is Ecstatic #include<stdio.h> main() { printf(“\n \tWelcome to see the sea of C”); printf(“\n See that C is Ecstatic” ); } Un-terminated string or character constant

36 See Programming in C #include<stdio.h> void main() { printf(“Let Us C”); }

37 Let Us C See Programming in C #include<stdio.h> void main() {
printf(“Let Us C”); }

38 See Programming in C #include<stdio.h> void main() { printf(“Let Us \nC”); }

39 Let Us C See Programming in C #include<stdio.h> void main() {
printf(“Let Us \nC”); }

40 See Programming in C #include<stdio.h> void main() { printf(“Le\tt Us C”); }

41 Le t Us C See Programming in C #include<stdio.h> void main() {
printf(“Le\tt Us C”); }

42 See Programming in C #include<stdio.h> void main() { printf(“Let Us C\bK”); }

43 Let Us K See Programming in C #include<stdio.h> void main() {
printf(“Let Us C\bK”); }

44 See Programming in C #include<stdio.h> void main() { printf(“Let Us C\rG”); }

45 Get Us C See Programming in C #include<stdio.h> void main() {
printf(“Let Us C\rG”); }

46 See Programming in C #include<stdio.h> void main() { printf(“Let Us C\a”); }

47 Let Us C See Programming in C #include<stdio.h> void main() {
printf(“Let Us C\a”); }

48 See Programming in C: Use of Variables
#include<stdio.h> void main() { a = 5; printf(“a = a”); }

49 See Programming in C: Use of Variables
#include<stdio.h> void main() { a = 5; printf(“a = a”); } Undefined symbol ‘a’ in main

50 See Programming in C: Use of Variables
#include<stdio.h> void main() { int a; printf(“a = a”); } //integer: used for counting Whatever you use should be brought in beforehand! The dissection of a frog, or brisk physics practical and instruments are damaged!

51 a = a See Programming in C: Use of Variables #include<stdio.h>
void main() { int a; printf(“a = a”); }

52 The value of a is 6785 Data type followed by variable name Declaration of a variable #include<stdio.h> main() { int a; printf(“\n The value of a is %d “,a); } format specifier or place holder and its corresponding expression Undefined symbol a Undeclared first use ‘a’

53 See Programming in C: Use of Variables
#include<stdio.h> void main() { int a; a = 5; // initialisatioin printf(“a = %d”,a); } //variable declaration

54 a = 5 See Programming in C: Use of Variables #include<stdio.h>
void main() { int a; a = 5; printf(“a = %d”, a); }

55 The value of a is 6 #include<stdio.h> Declaration & Initialization of a variable main() { int a = 6; printf(“\n The value of a is %d “,a); }

56 5 + 3 = 8 See Programming in C: Use of Variables
#include<stdio.h> void main() { int a=5,b=3,c; c = a + b; printf(“%d + %d = %d”,a,b,c); }

57 5 + 3 = 8 See Programming in C: Use of Variables
#include<stdio.h> void main() { int a=5,b=3,c; c = a + b; printf(“%d + %d = %d”,a,b,c); } Place holders in printf statement and the variables listed after the string sit in the place holders in the same order

58 The product 6 * 5 = 30 #include<stdio.h> main() { int a,b; a = 6; b= 5; printf(“\n The product %d * %d = %d “,a, b , a*b); } format specifier or place holder

59 The prod of 6 and 5 is 30 #include<stdio.h> main() { int a=6,b=5; printf(“\n The prod of %d and %d is %d = %d”, a, b , a*b); } Variable declaration and initialization can be done together for multiple vars

60 The prod of 6 and 5 is 30 #include<stdio.h> main() { int a=6,b=5; printf(“\n The prod of %d and %d is %d = %d”, a, b , a*b); } Variable declaration and initialization can be done together for multiple vars But what is the big deal if we know the values in advance? Hard coded values! Why not soft code? Accept from KB during the run time?

61 See Programming in C: accepting of Variables
#include<stdio.h> void main() { int a,b; printf(“enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); }

62 See Programming in C: accepting of Variables
#include<stdio.h> void main() { int a,b; printf(“enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); } 627 a 7218 b In memory

63 See Programming in C: accepting of Variables
Enter value of a #include<stdio.h> void main() { int a,b; printf(“Enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); } 627 a 7218 b In memory

64 See Programming in C: accepting of Variables Enter value of a4
#include<stdio.h> void main() { int a,b; printf(“enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); } 4 a 7218 b 4 In memory

65 See Programming in C: accepting of Variables
Enter value of a4 #include<stdio.h> void main() { int a,b; printf(“enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); } 4 a 8 b 4 In memory

66 See Programming in C: accepting of Variables
Enter value of a4 4 x 2 = 8 #include<stdio.h> void main() { int a,b; printf(“enter value of a”); scanf(“%d”,&a); b = 2 * a; printf(“%d x 2 = %d”,a,b); } 4 a 8 b 4 In memory

67 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 856 d e 0

68 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 46 d e 0

69 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 46 d 460 e 0

70 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 46 d 460 e 2

71 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 46 d 460 e 2

72 What is this operation? void main() { did a mod operation:
int a=462,b=10,c,d,e; c = a/b; d = c * b; e = a – d; printf(“ e = %d”,e); } did a mod operation: a % 10 operation or the last digit of a a b 10 c 46 d 460 e 2

73 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b;
So what is 659 % 10 13 % 10 7 % 10 7 % 4 8 % 4 5 % 4 4 % 4 3 % 4 1 % 4 What is this operation? void main() { int a=462,b=10,c,d,e; c = a/b; d = c * b; e = a – d; printf(“ e = %d”,e); } a b 10 c 46 d 460 e 2 did an a % 10 operation or the last digit of a

74 #include<stdio.h>
The square of 6 is 36 main() { int a; printf(“Enter a num ”); scanf(“%d”,&a); printf(“\n The square of %d is %d “,a, a*a); } We want the values on the run, during execution, ie from the keyboard ! Lest one be blinking, better give a printf statement before scanf.

75 #include<stdio.h>
The square of 6 is 36 main() { int a; printf(“Enter a num ”); scanf(“%d”,&a); printf(“\n The square of %d is %d “,a, a*a); } We want the values on the run, during execution, ie from the keyboard ! Lest one be blinking, better give a printf statement before scanf.

76 #include<stdio.h>
The square of 6 is 36 main() { int a; printf(“Enter the value of to find square ”); scanf(“%d”,&a); printf(“\n The square of %d is %d “,a, a*a); } We want the values on the run, during execution, ie from the keyboard ! Lest one be blinking, better give a printf statement before scanf.

77 In the memory Enter the value of a to find square 6 The square of 6 is 36 1 5681 a 6 36 9625 sq #include<stdio.h> 2 main() { int a,sq; printf(“\n Enter the value of a to find square ”); scanf(“%d”,&a); sq= a * a; printf(“\n The square of %d is %d “,a, sq); } 6

78 Common error messages Statement missing check the semicolon in the previous line Compound statement missing check whether the closing curly brace is missing Declaration not allowed check whether the opening curly brace is missing Un-terminated string or character constant check whether the string which is started is ended or not: any closing double quote missing Undefined symbol ‘x’ check whether the variable is declared or not Undefined symbol ‘xyz’,’pqr’,’apwzyd’… Check whether a string starting is missing or not

79 L value expected check whether some constant is being modified
Connot covert int * to int check whether an integer pointer value is being assigned to an integer variable Too few parameters in the function call check the shortage of parameters in the calling of a function, whereas the function definition or prototype has more parameters Too many parameters in the function call check the additional parameters in the calling of a function, whereas the function definition or prototype has lesser parameters

80 The sum is void main() { int num = 47,sum = 0, ld; while (num > 0)
ld = num % 10; sum = sum + ld; num = num / 10; } printf(“The sum is “+sum); 4 47 num 7 11 11 sum junk 4 7 ld

81 22 23 24 20 21 576 396 63 41 86 20 I j Void main(){ int I, j, sum= 0; for(i=20;i<= 23;i++){ sum = sum + i; printf(“\n the sum is %d and i is %d “,sum,i); } sum The sum is 20 and I is 20 The sum is 41 and I is 21 The sum is 63 and I is 22 The sum is 86 and I is 23

82 In the memory The square of 6 is 36 Enter the value of a to find square 25 9 6 1 81 625 5681 a 9625 #include<stdio.h> 2 81 625 36 sq main() { int a,sq,count; for(count=1,count<=3;count++) printf(“\n Enter the value to square ”); scanf(“%d”,&a); sq= a * a; printf(“\n The square of %d is %d “,a, sq); } 3 5681 4 3 1 2 count 9 6 25

83 In the memory The square of 6 is 36 Enter the value of a to find square 9 6 1 81 25 is 625 5681 a #include<stdio.h> main() { int a,sq,count; for(count=1,icount<=3;count++) printf(“\n Enter the value to square ”); scanf(“%d”,&a); sq= a * a; printf(“\n The square of %d is %d “,a, sq); } 9625 2 625 36 81 sq 3 5681 3 2 1 4 count 9 6 25

84 Problems to be done Swapping of two variables Swapping/rotation among n variables If statement syntax. Only one side check pass/even absolute value If else statement syntax. both sides pass or fail/odd or even biggest among two vars sort 2 variables senior/junior last digit is odd/even last but one digit odd/even divisible by 7 or not Nested if else 3 var biggest 3 var sort grade s, a, b,c… incentive upto and then on… special pass criterion roots of a quadratic equation sides of a triangle

85 For loop With serial numbers The dynamics of for loop Printing all the numbers from 1 to 50 Summing up all numbers from 1 to 50 Accepting variables n times and processing them 21 matchstick game Printing a single star Printing a line of stars Printing the digits of a number Summing up the digits of a number Convert a number to binary Check whether a number is palindrome

86 Check whether a number is sliding down or climbing up
Check whether a given number is prime or not Check whether a number is Armstrong Perfect number Single digit sum number or not Cup number Grove number Platau number Mountain number Interchangeability of loops a statement 10 times

87 Nested loops Printing patterns Dynamic screen or dance on the screen Generation of numbers Prime, Armstrong, perfect, ninean, eighteen... Cup, mountain, palindrome Slide numbers, climb up numbers Oddeven sum equal Alter sum equal Triple sum equal Generation of deadly sevens. Either divisible by 7 or at least one digit is 7

88 Arrays Declare Initialize Print all Sum all Modify all Select on basis Print function Sort Bubble Selection Insertion

89 Palarray Print in reverse order Reverse array and print Find out the sum of digits in the array Arrays and addresses Pointer concept. Using a pointer, access another var, change it, accept to it Pointer arithmetic Double pointer issues. Proper pictorial representations Printing an array, given any address of the nth element in array

90 Functions Using built in functions with no arguments clrscr(); Using built in functions with arguments. Printf(“ ab”); Using built in functions with varying arguments printf(“%d %d “,a,b) User defined functions, without arguments User defined functions with arguments User defined functions return vals User defined functions with pass by reference arguments

91 Two d arrays Declaration and initialisation Printing td arrays Function to print/use td arrays 16 puzzle Addresses in two D arrays Sending any row’s address to a function Diagonal elements Major/ minor Upper major/upper minor

92 Transpose Addition multiplication Border elements Inner elements Steering rotation Inner steering rotation Check symmetric Creation of magic square Sum of rows/cols in a wrapper row/col Check quadian matrix if every subsquare matrix sum are equal

93 1 9 11 3 8 16 14 6 5 13 15 7 4 12 10 2

94 Reflections of td arrays
Rotations of td arrays The cofactor of a matrix Storage classes in C

95 Strings Definition Declaration Initilisatioin Searching Sorting Two arrays and strings Malloc and pointer array of storing strings

96 Operations Substring Length No. of vowels No of words Concatenation Palindrome Reversing a string Change case of string Upper/lower/toggle Sorting of strings

97 Structure Declaration Initialisation Copying Printing values Accepting values By variable By pointer Array of structure Array in a structure Structure within a structure

98 Unions Memory allocation Pre-processor directives

99

100

101

102

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


Download ppt "C Programming."

Similar presentations


Ads by Google