Successfully reported this slideshow.

Tech tut


Published on

Published in: Education, Technology, Spiritual
  • Be the first to comment

  • Be the first to like this

Tech tut

  1. 1. C (Not SHE) ki PATHSHALA (SUSWAGATAM)Sabhi ko mera yane Ketan urf Nano ka Pyar Bhara Namshkar Ap aye hum khush ho gaye
  2. 2. In Points pe Gaur Kijiye• We r here for Discussing C programming Fundas…so that u will rock ur campus placements• I will discuss some points with you. Yeh koi routine class nahi hai…I feel u will enjoy these Mera moto hai classes fun honi chahiye….
  3. 3. What u expect from me and answers• If you expect that u will be a champ after attending these sessions then u ve wrong impression…mai bhagwan nahi hun ki joh apki c chand dino me acchi kar dunga….• If u ask kya mere sessions boring( boys ki bhasha me c se start hone wala word) hoge toh my answers is at some points can be boring but overall they will be fun
  4. 4. What I expect from you and answers• U will keep ur Cell phones in Silent modes….• If u r seating near ur bf(for girls I guess :P),ff(for both boys and girls) and gf ( again for boys I guess :P) make sure u do not chit chat with them as it disturbs me and itz really annoying• You will get plenty of time for discussions, games and masti for making noise
  5. 5. • Pz do not be shy in asking me any difficulty…for personally asking ur difficulty write ur problem on any blank page…rt ur name and email id…or I will rt to you ur answer the next day…• OR SIMPLY RAISE UR HAND AND ASK….
  6. 6. Bhaiyo & Behno PZ NOTE IT…..• If you want be a real gud programmer in C do following things :-A] Always make a flowchart before writing the program..HUM SAB PRACTICALS ME PAHILE PROGRAM TAPTE HAI…THEN WE MAKE FLOWCHART…REALLY BAD PRACTISE….B]Make Memory Blocks on paper for any kind of variables or operations…apki adhi se jayda difficulties yehi solve ho jayegi…C LANG is all abt Memory Management
  7. 7. C]Practice makes the MAN as well as the WOMEN perfect…so do it as much as you can…D]AB what u need to crack campus questions is Develop understanding of basic things in C and develop the skill of debugging a programE]They try to test ur debugging skills by asking yourself simple questions on basic concepts…like for eg rules for integer declaration…
  8. 8. My way…..• My way is Simple…I will mostly try to cover THEORY concepts with the help of problems …which will help you to crack the questions• The questions have been compiled and tested and have been directly taken from campus placement tests of MNCs…which will basically show you how they ask…and What they ask…
  9. 9. • I need ur support so that u shud read material like any gud c book of kanitker sir alongside my sessions after going home…so that it will help you…• I can not teach you everything in detail as I do not have time…you will see that at the end of the course u will know what kind of questions are asked and what shud be ur attitude and approach towards solving them and what data is required to solve them
  10. 10. Topics which we will do…1]Types, Operators and Expressions2]Control Flow3]Functions and Program Structure4]Pointers and Arrays5]Structures6]Input and Output
  11. 11. 1.Types , Operators, ExpressionsVariables & Constants :-Def :- They are the basic data objects manipulated in program… Ab dekhte hai variables ke bare me info joh apko pata hona chahiye for Questions….
  12. 12. • You will be asked questions on variable naming rules….Typical rules on which questions are asked are :-
  13. 13. What will be output of the following c program? #include<stdio.h> int main(){ int _=5; int __=10; int ___; ___=_+__; printf("%i",___); return 0;}A]5 B]10 C]15 D]Compilation Error E]None of these
  14. 14. • ANS – C]15….. Reason :- Important rule for variable naming says that the first character in the name must be a letter…But “_” underscore is counted as a letter….Yeh Trick thi wahan pe…. So ap Dekh sakte hai…question was not difficult…bas yeh simple rule yad rakhiye…
  15. 15. What will be output of the following c program?#include<stdio.h>int main(){int goto=5;printf("%d",goto);return 0;}A]5 B]* C]** D]Compilation Error E]None of these
  16. 16. • ANS – D] Compilation Error Reason :- We have used the word “goto” as the name for the variable and assigned it the value 5…But “goto” is a keyword in C which already has a predefined function… Rule kehta hai…Ki Variable Name must not contain Keyword..Note this rule…Ispe Kafi bar sawal Ata hai… To tackle this…Just go thru the list of keywords in C..
  17. 17. What will be output of the following c program?#include<stdio.h>int main(){long int 1a=5l;printf("%ld",1a); return 0;}A]5 B]51 C]6 D]Compilation Error E]None of these
  18. 18. • ANS :- D]Compilation Error Reason :- Variable name must start with a Letter … But we have declared ‘1a’ and the first character in this name is a number…which is a wrong practice… Correct Rule is the first character in variable name must be a Letter….
  19. 19. Now , This is a very Special and Tricky question..What will be output of the following c program?#include<stdio.h>int main(){ int class=150; int public=25; int private=30; class = class/( private – public); printf("%d",class); return 0;}A]30 B] 2 C]Compilation Error D]None of these
  20. 20. • ANS :- A] 30… Reason :- Now Why 30????? You see Class , Public, Private….Yeh 3 joh hai yeh tin tighda kam bighda nahi hai…balki C++ ke keywords hai… So the Point to note is…Even if you can not use the “KEYWORDS OF C” as variable names but YOU CAN USE THE KEYWORDS OF C++ AS VARIABLE NAMES…PLEASE NOTE DOWN THIS POINT….
  21. 21. What will be output of the following c program?#include<stdio.h>int xyz=10;int main(){ int xyz=20; printf("%d",xyz); return 0;}A]10 B]20 C]30 D]Compilation Error E]None of these
  22. 22. • ANS :- B]20 Reason :- ???? You will observe that we gave the same name to both the variables…But Differences aisa hai ki…Ek Variable main ke ander hai…and ek main ke bahar…so Scope of that variable inside the main remains for that main…that is it can be manipulated inside the main…SO IMP RULE :- TWO VARIABLES CAN HAVE SAME NAME BUT IN DIFFRERENT SCOPES… NOW SEE THE VARIATION IN THE NEXT QUESTION…..
  23. 23. What will be output of the following c program? #include<stdio.h>int main(){ int xyz=20;{ int xyz=40; } printf("%d",xyz); return 0;}A]20 B]40 C]0 D] Compilation Error E]None of these
  24. 24. • ANS – A]20Reason :- Observe the braces…The scope of variable xyz =40 is limited to the braces in which it is declared..But the printf statement is written in the main and not in the braces…and the declaration xyz=20 is in main…so the printf will print as 20…. AGAIN IMP RULE IS…. TWO VARIABLES CAN HAVE SAME NAME BUT IN DIFFRERENT SCOPES…
  25. 25. What will be output of the following c program?#include<stdio.h>int main(){ int main = 80; printf("%d",main); return 0;}A] 80 B]0 C]Garbage Value D]Compilation Error E]None of these
  26. 26. • ANS :- A]80Reason :- Yahan pe note the point ki we can declare the variable name as main….C ko koi issues nahi hai is bare me….
  27. 27. What will be output of the following c program?#include<stdio.h>int main(){ int ABC=10; printf("%d",abc); return 0;}A]10 B]0 C]5 D]Compilation Error E]None of these
  28. 28. • ANS :- D] Compilation Error Reason :- Variable names are character sensitive and so abc and ABC are different…
  29. 29. Data TypesThere r few data types in C viz :-1] char :- a single byte, capable of holding one character in the local character set for eg. Char kp =‘A’;2] int :- an integer, typically reflecting the natural size of integers on the host machine for eg int kp =81;
  30. 30. 3]float :- single-precision floating point (For this please check IEEE 754 2008 format.) for eg float kp=0.23;4] double :- double-precision floating point (For this please check IEEE 754 2008 format.) for eg double kp =0.23;
  31. 31. Qualifiers• Integers can be applied two qualifiers viz short and long.For eg.short int sh;long int counter;The word int can be omitted in such declarations…• The qualifier signed and unsigned may be applied to char or any integer….
  32. 32. • Unsigned numbers are always positive or zero• Obey Law of Arithmetic modulo 2n• Where n is number of bits in the type• So if chars are 8 bits unsigned char variables have values between 0 and 255, while signed chars have values between -128 and 127• Whether plain chars are unsigned or signed is machine dependent
  33. 33. What abt the sizes?• For the sizes of Data Types each compiler is free to choose appropriate sizes for its own hardware• But the **Conditions Applied are :- A]Shorts and Ints are at least 16 bits B]Longs are at least 32 bits C]Long > Int > Short
  34. 34. • The type long double specifies extended- precision floating point.• float, double and long double could represent one, two or three distinct sizes• The standard headers <limits.h> and <float.h> contain symbolic constants for all of these sizes, along with other properties of the machine and compiler.
  35. 35. Constants• There are 5 types of Constants viz :-1] Integer Constant :- for eg 1234 is int constant 1234l or 1234L is Long Int Constant 1234ul or 1234 UL indicates unsigned Long 1234u or 1234 U indicates unsigned int2]Floating Point Constants :- They Contain a decimal point for eg 123.4 or 1e -2.Type is double unless suffixed… 123.4 is Double Constant 123.4f or 123.4F is floating Constant 123.4l or 123.4L is Long Double Constant
  36. 36. 3] Character Constant :- A character constant is an integer, written as one character within single quotes, such as ‘0’.The value of this is 48 according to the ASCII table and not numeric 0 as we have attached single quotes….4]String Constant :- A string constant, or string literal, is a sequence of zero or more characters surrounded by double quotes, as in "I am a string"
  37. 37. • 5]Enumeration Constant :- An enumeration is a list of constant integer values, as in enum boolean { NO, YES };Specialty of this enum is by default the value of no wud be 0 and yes wud be 1.Boolean the name of the enum. The user can also specify the value of the constants inside. If not given then only default values are given as 0 1 2 and so on…. For eg. enum escapes { BELL = a, BACKSPACE = b, TAB = t,NEWLINE = n, VTAB = v, RETURN = r };
  38. 38. What will be output when you will execute following c code?#include<stdio.h>void main(){ printf("%dt",sizeof(6.5)); printf("%dt",sizeof(90000)); printf("%d",sizeof(A));}There can not be a specific answer to this question…Why????
  39. 39. ANS :- As we saw the values of sizes depend on the compiler choosing appropriate sizes for the hardware…there can not be a specific answer to this question….
  40. 40. This is an information question to you…Please note the answer…Consider on following declaring of enum.(i) enum cricket {Gambhir,Smith,Sehwag}c;(ii) enum cricket {Gambhir,Smith,Sehwag};(iii) enum {Gambhir,Smith=-5,Sehwag}c;(iv) enum c {Gambhir,Smith,Sehwag};Which one of the above is Correct Declaration?ANS :- ALL ARE CORRECT….
  41. 41. Syntax of enum data type is:enum [<tag_name>]{ <enum_constanat_name> [=<integer_ value>], …} [<var_name>,…]Note:[] : Represents optional .<>: Represents any valid c identifier
  42. 42. • This question is combined of operator and type casting that happens in C :-What will be output when you will execute following c code?#include<stdio.h>void main(){ signed x; unsigned y; x = 10 +- 10u + 10u +- 10; y = x; if(x==y) printf("%d %d",x,y); else if(x!=y) printf("%u %u",x,y);}A] 0 0 B] 65536 -10 C] 0 65536 D]65536 0
  43. 43. ANS :- A] 0 0… WHY???Reason :-x = 10 +- 10u + 10u +- 10;10: It is signed integer constant.10u: It is unsigned integer constant.x :signed integer variable….Lower data type operand always automaticallytype casted into the operand of higher data typebefore performing the operation and result willbe higher data type
  44. 44. a +- ba +(THIS IS FOR EXPRESSION) –(this gets attached as a sign of no)ba + (-b)This is Calculated….
  45. 45. Signed is higher data type than unsigned int.So, Corresponding signed value of unsigned 10uIs +10And As we know operators enjoy higher precedence than binary operators. So our expression is:x = 10 + (-10u) + 10u + (-10); = 10 + -10 + 10 + (-10); =0y = x =0
  46. 46. What will be output when you will execute following c code?#include<stdio.h>const enum Alpha{ X, Y=5, Z}p=10;void main(){ enum Alpha a,b; a= X; b= Z; printf("%d",a+b-p);}A] -4 B]-5 C]11 D] 10 E]Error :- Can not modify object
  47. 47. ANS – A]-4Reason :- Default value of enum constant X is zero andZ=Y+1=5+1=6So, a + b – p = 0 + 6 -10 = -4
  48. 48. Operators• The important operators for questions are Increment and Decrement operators, Unary operators and Logical Operators…• Another thing that must be taken into account is precedence of operators…
  49. 49. Increment and Decrement Operator• The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1.• But the confusing and interesting part is they can be used prefix or post fix …• For exampleIf n is 5Then x = n++ will give x to be 5…as n is postfix and is calculated after the expression…
  50. 50. • But if n is 5And x = ++nThen x is 6…as now ++ is pre increment and n is incremented before expression is calculated…+ and – in unary form are the important operators in example like 10 + -(10u)Here precedence is given to -10u and –ve sign is the unary operator and + is binary operator…
  51. 51. • Logical operators are :-Logical and :- &&Logical or :- ||These should not be confused with bitwise operators & (bitwise and) and |(bitwise or)
  52. 52. Precedence of Operators…
  53. 53. What will be output of the following program? #include<stdio.h>int main(){ int i=1; i=2+2*i++; printf("%d",i); return 0;}
  54. 54. • ANS :- 5Reason :- i++ is a postfix operator…so the expression i = 2 + 2 *1 will be evaluated… i = 4 and then i++ will be evaluated so it will be i =5 and i will be printed as 5…
  55. 55. What will be output of the following program?#include<stdio.h>int main(){ int a=2,b=7,c=10; c=a==b; printf("%d",c); return 0;}
  56. 56. ANS :- 0 KYUN?????Go Back To the operator precedence table and you will see the expression a==b will be evaluated first….a = 2 and b = 7…so a is not equal to b and so the condition a==b is false and the value of the expression is 0 as the condition is false…now therefore c = 0…also observe c is declared as int and thus c= 10 will not matter and a new value will be assigned to c.... Now my questions is….if it was const c i.e. constant c in place of int c...
  57. 57. What will be output of the following program?#include<stdio.h>void main(){ int x; x=10,20,30; printf("%d",x); return 0;}
  58. 58. ANS :- 10Reason :- Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expressionx = 10, 20, 30First 10 will be assigned to x then comma operator will be evaluated.
  59. 59. What will be output of the following program?#include<stdio.h>int main(){ int a; a=015 + 0x71 +5; printf("%d",a); return 0;}
  60. 60. ANS :- 131….Kaise????Reason :- Here we introduce type casting…u will observe the variable a is int…so 015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13 0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113So, a = 13 + 113 + 5 = 131
  61. 61. Control FlowIF – Elseif (expression)statement1elseStatement2else part of an if-else is optional
  62. 62. Common Mistake :- Use Proper Braces….Nested Ifif (n > 0) if(n>0){ if(a>b)if (a > b) {z = a; z=a;} }else else{ {z = b; z=b;} }
  63. 63. Another Common Mistake is Use of Symbol = instead of == during comparison for egIf(a=b) instead of if(a==b)a=b assigns value of b to aWhere a==b properly compares the value of awith b
  64. 64. Else Ifif (expression)statementelse if (expression)statementelse if (expression)statementelse if (expression)statementelsestatement
  65. 65. expressions are evaluated in orderif an expression is true, the statement associatedwith it is executed, and this terminates thewhole chain.elsestatementcan be omitted, or it may be used for error checking to catch an ``impossible condition.
  66. 66. Switch :-The switch statement is a multi-way decision thattests whether an expression matches one ofa number of constant integer values, and branchesaccordingly.switch (expression) {case const-expr: statementscase const-expr: statementsdefault: statements}
  67. 67. All case expressions must be different. The caselabeled default is executed if none of the othercases are satisfied. A default is optional; if it isntthere and if none of the cases match, no action atall takes place. Cases and the default clause canoccur in any order.
  68. 68. Break Statement…switch (expression) {case const-expr: statements break;case const-expr: statements break;default: statements}• By break it will leave the switch…otherwise if there is no brake statement it will go on falling and executing next cases….• We can type return also instead of break…
  69. 69. What will be output when you will execute following ccode?#include<stdio.h>void main(){ int a=100; if(a>10) printf("M.S. Dhoni"); else if(a>20) printf("M.E.K Hussey"); else if(a>30) printf("A.B. de villiers");}A] M.S.Dhoni B] M.E.K. Hussey C] villiers
  70. 70. ANS :- A]M.S.DhoniYenna Rascala Mind it well ……Reason :-In case of if – if else – if else … Statement if first ifclause is true the compiler will never check rest ofthe if else clause and so on.Please Note Down…..
  71. 71. What will be output when you will executefollowing c code?#include<stdio.h>void main(){ int x=-1,y=-1; if(++x=++y) printf("R.T. Ponting"); else printf("C.H. Gayle");}A] R.T.Pointing B]C.H.Gayle C]Compilation Error
  72. 72. ANS :- C] Compilation Error …WHY ?????Consider following statement:++x=++yAs we know ++ is pre increment operator in the above statement. This operator increments the value of any integral variable by one and return that value. After performing pre increments above statement will be:0=0In C language it is illegal to assign a constant value to another constant.
  73. 73. What will be output when you will executefollowing c code?#include<stdio.h>void main(){ int m=5,n=10,q=20; if(q/n*m) printf("William Gates"); else printf(" Warren Buffet"); printf(" Carlos Slim Helu");}A]William Gates B]William Gates Carlos Slim Helu
  74. 74. ANS :- B] William Gates Carlos Slim HeluReason :-Consider the following expression:q/n*mIn this expression there are two operators. Theyare: /: Division operator *: Multiplication operatorPrecedence and associate of each operator is asfollow:
  75. 75. PRECEDENCE OPERATOR ASSOCIATE1 /,* Left to RightPrecedence of both operators is same. Hence associate will decidewhich operator will execute first. Since Associate is left to right. So/ operator will execute then * operator will execute.=q/n*m= 20 / 10 * 5=2*5=10As we know in c zero represents false and any non-zero numberrepresents true. Since 10 is non- zero number so if clause willexecute and print: William Gates
  76. 76. Since in else clause there is not any opening andclosing curly bracket. So compiler will treat onlyone statement as a else part. Hence laststatement i.e. printf(" Carlos Slim Helu");is notpart of if-else statement. So at the end compilerwill also print: Carlos Slim Helu So output ofabove code will be: William Gates Carlos SlimHelu
  77. 77. What will be output when you will execute following ccode?#include<stdio.h>void main(){ if("ABC") printf("Barack Obaman"); if(-1) printf("Hu Jintaon"); if(.92L) printf("Nicolas Sarkozyn"); if(0) printf("Ben Bernanken"); if(W) printf("Vladimir Putinn");}
  78. 78. ANS :- Barack Obama Hu Jintao Nicolas Sarkozy Vladimir PutinReason :- “ABC”: It is string constant and it will always return anon-zero memory address.0.92L: It is long double constant.‘W’: It is character constant and it has ASCII Value which is nonzero NumberAs we know in c language zero represents false and any non-zeronumber represents true. In this program condition of first,second, third and fifth if statements are true.
  79. 79. What will be output when you will executefollowing c code?#include<stdio.h>void main(){ int a=5,b=10; if(++a||++b) printf("%d %d",a,b); else printf("John Terry");}
  80. 80. ANS :- 6 10Reason :-Consider the following expression:++a || ++bIn this expression || is Logical OR operator. Twoimportant properties of this operator are:Property 1:(Expression1) || (Expression2)|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.
  81. 81. Property 2:To optimize the execution time there is rule,Expression2 will only evaluate if and only ifExpression1 return zero.In this program initial value of a is 5. So ++a will be 6.Since ++a is returning a non-zero so ++b will notexecute and if condition will be true and if clause willbe executed.
  82. 82. What will be output when you will execute following c code?#include<stdio.h>void main(){ int x=1; if(x--) printf("The Godfather"); --x; else printf("%d",x);}
  83. 83. ANS :- Compilation ERROR….Reason :- If you are not using { and } in if clause then you can write only one statement.
  84. 84. What will be output when you will executefollowing c code?#include<stdio.h>void main(){ int a=2; if(a--,--a,a) printf("The Dalai Lama"); else printf("Jim Rogers");}A]The Dalai Lama B]Jim Rogers C] Compile Error
  85. 85. ANS :- B] Jim RogersReason :-Consider the following expression:a-- , --a , aIn c comma is behaves as separator as well asoperator. In the above expression comma is behavingas operator. Comma operator enjoy lest precedence inprecedence table and its associatively is left to right. Sofirst of all left most comma operator will performoperation then right most comma will operator in theabove expression.
  86. 86. After performing a-- : a will be 2After performing --a : a will be 0a=0As we know in c zero represents false and any non-zero number represents true. Hence else part willexecute.
  87. 87. What will be output when you will execute followingc code?#include<stdio.h>void main(){ int check=2; switch(check){ case 1: printf("D.W.Steyn"); case 2: printf(" M.G.Johnson"); case 3: printf(" Mohammad Asif"); default: printf(" M.Muralidaran"); }}
  88. 88. ANS :- M.G.Johnson Mohammad Asif M.MuralidaranIf we will not use break keyword in each case theprogram control will come in each case after thecase witch satisfy the switch condition.
  89. 89. What will be output when you will execute following c code?#include<stdio.h>enum actor{ SeanPenn=5, AlPacino= -2, GaryOldman, EdNorton};void main(){ enum actor a=0; switch(a){ case SeanPenn: printf("Kevin Spacey"); break; case AlPacino: printf("Paul Giamatti"); break; case GaryOldman:printf("Donald Shuterland"); break; case EdNorton: printf("Johnny Depp"); }}
  90. 90. ANS :- Johny Depp …WHY????Reason :- Default value of enum constantGaryOldman = -2 +1 = -1And default value of enum constantEdNorton = -1 + 1 = 0Note: Case expression can be enum constant.
  91. 91. What will be output when you will execute following code?#include<stdio.h>void main(){ switch(6){ case 6.0f:printf("Salma Hayek"); break; case 6.0: printf(“Katrina Kaif"); break; case 6.0L:printf(“Gerard Butler"); break; default: printf(“Jonathan Rhys Meyers"); }}A]Salma Hayek B]Katrina Kaif C]Gerard Butler D] JonathanRhys Meyers E]Compilation Error
  92. 92. ANS :- E]Compilation ErrorReason :-Case expression must be integral constantexpression. If it is not integer then it isautomatically type casted into integer (int)6.0f = 6(int)6.0 = 6(int)6.0L = 6In c duplicate case is not possible.
  93. 93. What will be output when you will execute following ccode?#include<stdio.h>void main(){ switch(5/2*6+3.0){ case 3:printf("David Beckham"); break; case 15:printf("Ronaldinho"); break; case 0:printf("Lionel Messi"); break; default:printf("Ronaldo"); }}A]David Beckham B] Ronaldinho C] Lionel Messi D] RonaldoE]Compilation Error
  94. 94. ANS :- E]Compilation ErrorReason :- Consider on the expression:5/2*6+3.0=2*6+3.0=12 + 3.0=15.0In c switch expression must return an integervalue. It cannot be float, double or long double
  95. 95. What will be output when you will execute following c code?#include<stdio.h>void main(){ int a=5; a=a>=4; switch(2){ case 0:int a=8; case 1:int a=10; case 2:++a; case 3:printf("%d",a); }}A] 2 B] Compilation error C] 11
  96. 96. ANS :- B] Compilation ErrorReason :- We can not declare any variable in any case of switch case statement.Suppose Now I remove the int part behind int a = 10 and int a =8 ….Now what will be the o/p????
  97. 97. LoopsWhile and ForWhile Loop :-while (expression)StatementThe expression is evaluated. If it is non-zero,statement is executed and expression is reevaluated.This cycle continues until expression becomes zero,at which point execution resumes after statement.
  98. 98. For Loop :-The for statementfor (expr1; expr2; expr3)StatementGrammatically, the three components of a for loop are expressions.Most commonly, expr1 and expr3 are assignments or function calls andexpr2 is a relational expression. Any of the three parts can be omitted,although the semicolons must remain. If expr1 or expr3 is omitted, itis simply dropped from the expansion. If the test, expr2, is not present,it is taken as permanently true, sofor ( ; ; ) {...}is an ``infinite loop, presumably to be broken by other means, such asa break or return.
  99. 99. Do while LoopThe syntax of the do isdostatementwhile (expression);The statement is executed, then expression isevaluated. If it is true, statement is evaluatedagain, and so on. When the expression becomesfalse, the loop terminates.
  100. 100. Break and ContinueBreak:-The break statement provides an early exit fromfor, while, and do, just as from switch. A breakcauses the innermost enclosing loop or switch tobe exited immediately.
  101. 101. Continue :-The continue statement is related to break, butless often used; it causes the next iteration ofthe enclosing for, while, or do loop to begin.for (i = 0; i < n; i++)if (a[i] < 0) /* skip negative elements */continue;Basically used in nesting of loops
  102. 102. Go To and LabelsC provides the infinitely-abusable goto statement,and labels to branch to. Formally, the gotostatement is never necessary, and in practice it isalmost always easy to write code without it.
  103. 103. Nevertheless, there are a few situations where gotos may find a place.The most common is to abandon processing in some deeply nestedstructure, such as breaking out of two or more loops at once. The breakstatement cannot be used directly since it only exits from theinnermost loop. Thus:for ( ... )I Love You;for ( ... ) {...if (disaster)goto error;}...error:/* clean up the mess */
  104. 104. With a few exceptions like those cited here, codethat relies on goto statements is generallyharder to understand and to maintain than codewithout gotos.So it must be rarely used…The Consiquences canbe disastrous….
  105. 105. What will be output of following c code?#include<stdio.h>extern int x;int main(){ do{ do{ printf("%o",x); } while(!-2); } while(0); return 0;}int x=8;
  106. 106. ANS :- 10Reason :- Here variable x is extern type. So it will searchthe definition of variable x. which is present at the end ofthe code. So value of variable x =8There are two do-while loops in the above code. AS weknow do-while executes at least one time even thatcondition is false. So program control will reach at printfstatement at it will print octal number 10 which is equal todecimal number 8.Note: %o is used to print the number in octal format.In inner do- while loop while condition is ! -2 = 0In C zero means false. Hence program control will comeout of the inner do-while loop. In outer do-while loopwhile condition is 0. That is again false. So program controlwill also come out of the outer do-while loop.
  107. 107. What will be output of following c code?#include<stdio.h>int main(){ int i=2,j=2; while(i+1?--i:j++) printf("%d",i); return 0;}HINT :- Condition ? (if true executes this) : (if false executes this)? :- ternary operator
  108. 108. ANS :- 1Reason :- Consider the while loop condition: i + 1 ? -- i :++jIn first iteration: i + 1 = 3 (True)So ternary operator will return --i i.e. 1In c 1 means true so while condition is true. Hence printfstatement will print 1In second iteration:i+ 1 = 2 (True)So ternary operator will return -–i i.e. 0In c zero means false so while condition is false. Henceprogram control will come out of the while loop.
  109. 109. What will be output of following c code?#include<stdio.h>int main(){ int x=011,i; for(i=0;i<x;i+=3){ printf("Start "); continue; printf("End"); } return 0;}
  110. 110. ANS :- Start Start StartReason :- 011 is octal number. Its equivalentdecimal value is 9.So, x = 9First iteration:i=0i < x i.e. 0 < 9 i.e. if loop condition is true.Hence printf statement will print: Start
  111. 111. Due to continue keyword program control willcome at the beginning of the for loop and value ofvariable i will be:i += 3i=i+3=3Second iteration:i=3i < x i.e. 3 < 9 i.e. if loop condition is true.Hence printf statement will print: Start
  112. 112. Due to continue keyword program control willcome at the beginning of the for loop and value ofvariable i will be:i += 3i=i+3=6Third iteration:i=3i < x i.e. 6 < 9 i.e. if loop condition is true.Hence printf statement will print: Start
  113. 113. Due to continue keyword program control willcome at the beginning of the for loop and value ofvariable i will be:i += 3i=i+3=9fourth iteration:i=6i < x i.e. 9 < 9 i.e. if loop condition is false.Hence program control will come out of the for loop.
  114. 114. What will be output of following c code?#include<stdio.h>int main(){ static int i; for(++i;++i;++i) { printf("%d ",i); if(i==4) break; } return 0;}Hint :- Value of Static int in c by default is 0.
  115. 115. ANS :- 2 4Reason :- Default value of static int variable in c iszero. So, initial value of variable i = 0First iteration:For loop starts value: ++i i.e. i = 0 + 1 = 1For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loopcondition is true. Hence printf statement will print2
  116. 116. Loop incrimination: ++I i.e. i = 2 + 1 =3Second iteration:For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loopcondition is true. Hence printf statement will print4.Since is equal to for so if condition is also true.But due to break keyword program control willcome out of the for loop.
  117. 117. What will be output of following c code?#include<stdio.h>int main(){ int i=1; for(i=0;i=-1;i=1) { printf("%d ",i); if(i!=1) break; } return 0;}
  118. 118. ANS :- -1Reason :- Initial value of variable i is 1.First iteration:For loop initial value: i = 0For loop condition: i = -1 . Since -1 is non- zeronumber. So loop condition true. Hence printffunction will print value of variable i i.e. -1Since variable i is not equal to 1. So, if condition istrue. Due to break keyword program control willcome out of the for loop.
  119. 119. What will be output of following c code?#include<stdio.h>int main(){ for(;;) { printf("%d ",10); } return 0;}
  120. 120. ANS :- Infinite LoopReason :- In for loop each part is optional.
  121. 121. What will be the output ?include<stdio.h>int main(){ int i; for(i=0;i<=5;i++); printf("%d",i); return 0;}
  122. 122. ANS :- 6Reason :- Its possible for loop without any body
  123. 123. Finite or Infinite????#include<stdio.h>int main(){ char c=125; do printf("%d ",c); while(c++); return 0;}
  124. 124. ANS :- Finite timesReason :- If we will increment the char variable cit will increment as:126,127,-128,-127,126 . . . . , 3, 2, 1, 0When variable c = 0 then loop will terminate.
  125. 125. 3.Functions and Program Structures• What is function in C programming?• Definition Function is block or part of program. When any program is very long or same code is repeating many times then we try to cut the program in different parts (or blocks) so that whole program became more understandable, easier to debug (error checking) and size of code will be lesser.
  126. 126. Syntax of Function :-// Function Declaration<return_type> <function_name>(<parameters>,…);//Function Defination :-<return_type> <function_name>(<parameters>,…){Statement 1;Statement 2;return expression;}
  127. 127. Simple example of function structureint sum (int,int); //function declarationvoid main(){int p;p=sum(3,4); //function callprintf(“%d”,sum);}int sum( int a,int b) //function definition{int s; //function bodys=a+b;return s; //function returning a value }
  128. 128. There are Rules for Function naming…Please chkthem out…I leave this as exercise to you…Return type of function in c programming:-return is keyword of c. When the control reachesto the return keyword it immediately terminatesthe execution of that function and transfer thecontrol to the calling function.
  129. 129. void dev();void main(){ clrscr(); printf("onen"); dev(); printf("twon"); getch();}void dev(){ printf("threen"); return; printf("fourn");}
  130. 130. ANS :- one three twoAs after return nothing wud be executed
  131. 131. Primitive data type.Primitive data types are: char, int, float, double, voidExamples:a. function which is returning char data typeb. function which is returning int data typec. function which is returning float data typed. function which is returning double data typee. function which is returning void data type
  132. 132. Derived data type.Derived data types are: array, function, pointerExamples:a. Function which is returning arrayb. function which is returning functionc. function which is returning pointer
  133. 133. User defined data type.User defined data types are: structure, union, enumExamples:a. Function which is returning structureb. Function which is returning unionc. Function which is returning enum
  134. 134. Function can return only one value at time…Storage classes allowed with return type arestatic, extern, typedef i.e. we cannot use auto andregister storage class with the return type of anyfunction.For eg
  135. 135. auto int tcs(int);void main(){int a=5;a=tcs(a);printf("%d",a);}auto int tcs(int x){ return x++;}Output: Compilation error
  136. 136. In return type we can use modifier like short, long, signed, unsigned, extern,static, const, volatile etc.long unsigned static const ddlg(){ static const long unsigned a=0101; // This is octal number…. return a;}void main(){ long number; number=ddlg(); printf("%X",number); // %x – Hexadecimal equivalent}Output:41
  137. 137. Parameter or argument of functionParameter of function can be:1. Primitive data type.2. Derived data type.3. User defined data type.4. Ellipsis i.e. variable number of parameter.
  138. 138. float sachin(int x){ float r=(float) x; return r;}void main(){ float f;f=sachin(33);printf("%f",f);}Output : 33.000000
  139. 139. void main(){ float f;f=sachin(33); printf("%f",f); }float sachin(int x){ float r=(float)x; return r;}Output : Compilation error
  140. 140. float sachin(int);void main(){ float f;f=sachin(33); printf("%f",f);}float sachin(int x){ float r=(float)x; return r;}Output: 33.000000
  141. 141. typedef float kp (int,char);void main(){ float num,num1,num2; kp a,b; num1=a(5,a); num2=b(6,0); num=num1+num2; printf("%f",num);}
  142. 142. float a(int x, char y){ x=x+y; return (float)x;}float b(int p,char q){ p=q-p; return p;}Output: 144.000000
  143. 143. void main(){ float num,num1,num2; num1=a(5,a); num2=b(6,0); num=num1+num2; printf("%f",num);}
  144. 144. float a(int x, char y){ x=x+y; return (float)x;}float b(int p,char q){ p=q-p; return p;}Output: Compilation error
  145. 145. Remember this For interview thatFunction’s declaration doesn’t reserve anymemory space.
  146. 146. Function Recursion in CCalling of same function from its function body isknown as function recursion. It is alternative ofloop. Any c program which is possible using loopit must be possible using function recursion.
  147. 147. Find the sum of all even numbers from 0 to 20 using function recursion.Program:void main(){int total; total=sum(2); printf("%d",total);}
  148. 148. int sum(int i){static int even=0; // between successive calls we have to make sure even becomes zero so we use static…… if(i<=20){ even=even+i; sum(i+2); //calling same function } return even;}Output: 110
  149. 149. It is very difficult to understand the execution aswell as to write the function recursion program.If any person is writing such program directly hemay be memorized that program.Now note down the steps for writing theprogram…
  150. 150. Step 1: Write the same program using while loop and function. Here function is sum.void main(){ int total; total=sum(2); printf("%d",total);}
  151. 151. int sum(int i){ int even=0; while(i<=20){ even=even+i; i=i+2; } return even;}
  152. 152. Step 2: Make local variable even as static variable int sum(int i){ static int even=0; while(i<=20){ even=even+i; i=i+2; } return even;}
  153. 153. Step 3: Replace while keyword by if sum(int i){static int even=0; if(i<=20){ even=even+i; i=i+2; } return even;}
  154. 154. Step 4: Since here variable i has used in condition checking. So replace the statement i=i+2 by sum(i+2).int sum(int i){ staic int even=0; if(i<=20){ even=even+i; sum(i+2); } return even;}
  155. 155. Points to note about Recursion are :-It is very slow process.One problem with function recursion is it creates afunction frame in each function call. This makesprogram very slow. This is main reason tointroduce for, while and do-while loop in c eventthat it is also possible by using function recursion.
  156. 156. We cannot use break keyword in functionrecursion.Keyword break can be use to stop any loop. Sincefunction recursion is no a loop .So, we cannot usebreak and continue keyword.
  157. 157. We can not use goto keyword to switch thecontrol from one function to another function.
  158. 158. Nesting of Function calls :-If we are calling any function inside anotherfunction call is known as nesting function call.Sometime it converts a difficult program in easyone.For eg :-
  159. 159. Find the maximum number among five different integers usingnested function call.Answer:int max(int x,int y){return x>y?x:y;}void main(){ int m; m=max(max(4,max(11,6)),max(10,5)); printf("%d",m);}
  160. 160. How to calculate size of a function in c?Answer:Size of any function is calculated as:Size of function = Size of all local variable whichhas declared in function + Size of those globalvariables which has used in function+ Size of allits parameter+ Size of returned value if it is anaddress.
  161. 161. There are two types of function parameter standard:1. ANSI standardIn this standard function definition is written asint calculate(int a, int b){ int c; c=a+b; return c;}This standerd is also called as modern style.
  162. 162. 2. Old Standardint calculate(a, b)int a,b;{ int c; c=a+b; return c;}This standard is also called as old style.You should also known this style ifyou want to read old c code.
  163. 163. 4.Pointers — Before and AfterTheres a lot of nice, tidy code you can write withoutknowing about pointers. But once you learn to use thepower of pointers, you can never go back. There aretoo many things that can only be done with pointers.But with increased power comes increasedresponsibility. Pointers allow new and more ugly typesof bugs, and pointer bugs can crash in random wayswhich makes them more difficult to debugNonetheless, even with their problems, pointers are anirresistibly powerful programming construct.
  164. 164. What is a Pointer???
  165. 165. Shallow and Deep Copying
  166. 166. Bad Pointers
  167. 167. Correct LOGIC ?????
  168. 168. The & operator :- Reference to
  169. 169. Structures
  170. 170. Arrays
  171. 171. Strings
  172. 172. Last Lecture on Saturday….We will do some 10-12 theory interviewquestions and answers….and it will be over..nomore Subah uthna and no more pain…I hope youliked my teaching…Thanks for Attending the Series….
  173. 173. Why n++ executes faster than n+1 ?The expression n++ requires a single machineinstruction such as INR to carry out the incrementoperation whereas, n+1 requires more instructionsto carry out this operation.
  174. 174. What is C language?The C programming language is a standardizedprogramming language developed in the early 1970s byKen Thompson and Dennis Ritchie for use on the UNIXoperating system. It has since spread to many otheroperating systems, and is one of the most widely usedprogramming languages. C is prized for its efficiency, andis the most popular programming language for writingsystem software, though it is also used for writingapplications.
  175. 175. What is the main difference betweenSTRUCTURE and UNION?1.]important difference is in the size allocated to astructure and an union. for eg:struct example {int integer;float floating_numbers; }the size allocated here is sizeof(int)+sizeof(float);
  176. 176. where asin an union union example {int integer;float floating_numbers;}size allocated is the size ofthe highest member. so size is=sizeof(float);
  177. 177. 2 . if we declare two structure variables as struct strct1 x y; then the two structure variables x and y have different memory location. But we declared tow unio variables as union uni1 x y; then the two union variables x and y have same memory location.
  178. 178. Gud sites••••
  179. 179. Thank You Very Much for attending the series….If you wish to contact me :-Mobile :- 9028199481Email :- nanoketan@gmail.comBlog :- www.nanoketya.blogspot.comFacebook :- Ketan PaithankarALL THE BEST FOR YOUR LIFE………