1.33k likes | 1.43k Views
C Programming. A. S. D. F. K. L. ;. Computer system. Monitor. CPU. Key Board. mouse. Parts of a computer Processor, memory, hard disk, input, output. In memory both the program lines and variables are kept
E N D
A S D F K L ;
Computer system Monitor CPU Key Board mouse
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
Computer system Monitor CPU Key Board mouse
Computer system memory Monitor Hard disk processor CPU Key Board mouse
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
void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c); } 4 a 7 b junk c
void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c); } 4 a 7 b 11 c
void main() { int a = 4,b =7 , c; c = a + b; printf(“ sum is %d” ,c); } 4 a 7 b 11 c Sum is
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(“ %d + %d is %d “,a,b,c); } 47 0 4 a 7 0 11 b 11 junk c 4 + 7 = 11
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
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!
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?!
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
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.
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
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,
The operations possible Different operators, + - * / but % is also there! Integer on integer will give only an integer.
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
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
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;
See Programming in C #include<stdio.h>// header file inclusion void main() { printf(“Let Us C”); }
See Programming in C #include<stdio.h> void main()//a must for any C program to work { printf(“Let Us C”); }
See Programming in C #include<stdio.h> void main()//see the function braces { printf(“Let Us C”); }
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”); }
See Programming in C #include<stdio.h> void main()//function header { printf(“Let Us C”); }
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
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
See Programming in C #include<stdio.h> void main()//function header { printf(“Let Us C”);// end of an instruction }
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
#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
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
See Programming in C #include<stdio.h> void main() { printf(“Let Us C”); }
See Programming in C Let Us C #include<stdio.h> void main() { printf(“Let Us C”); }
See Programming in C #include<stdio.h> void main() { printf(“Let Us \nC”); }
See Programming in C Let Us C #include<stdio.h> void main() { printf(“Let Us \nC”); }
See Programming in C #include<stdio.h> void main() { printf(“Le\tt Us C”); }
See Programming in C Le t Us C #include<stdio.h> void main() { printf(“Le\tt Us C”); }
See Programming in C #include<stdio.h> void main() { printf(“Let Us C\bK”); }
See Programming in C Let Us K #include<stdio.h> void main() { printf(“Let Us C\bK”); }
See Programming in C #include<stdio.h> void main() { printf(“Let Us C\rG”); }
See Programming in C Get Us C #include<stdio.h> void main() { printf(“Let Us C\rG”); }
See Programming in C #include<stdio.h> void main() { printf(“Let Us C\a”); }
See Programming in C Let Us C #include<stdio.h> void main() { printf(“Let Us C\a”); }
See Programming in C: Use of Variables #include<stdio.h> void main() { a = 5; printf(“a = a”); }
See Programming in C: Use of Variables #include<stdio.h> void main() { a = 5; printf(“a = a”); } Undefined symbol ‘a’ in main
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!