Sei sulla pagina 1di 11

C programming questions and answers C language tricky good pointers questions answers and explanation operators data types

arrays structures questions functions recursion preprocessors, looping, f ile handling, strings questions switch case if else printf advance c linux objec tive types mcq faq interview questions and answers with explanation and solution for freshers or beginners. Placement online written test prime numbers Armstron g Fibonacci series factorial palindrome code programs examples on c c++ tutorial s and pdf C tutorial C Programming Questions Interview c questions C Programs C Test C programming pdf Program of c++ Java questions Sql Server Looping in c Explanation of loops or looping in c programming language by examples and questi ons Looping is the process of repeating of same code until a specific condition does nt satisfy. In c there are three types of loop: (a)loop (b)while loop (c)do while for loop: This loop is used when we have to execute a part of code in finite times. It is per tested loop. Syntax of for loop: for (Expression 1; Expression 2; Expression 3){ Loop body } Order of movement of control in for loop: First time: Expression 1-> Expression 2->Loop body -> Expression 3 Second time and onward: Expression 2->Loop body -> Expression 3 That is expression 1 only executes in the first iteration. From second iteration and onward control doesnt go to at the expression 1.For example: #include<stdio.h> int main(){ int i; for(i=0;i<=4;i++){ printf("%d ",i); } return 0;

} Output: 0 1 2 3 4 Explanation of each term of syntax: Expression 1: It is called initialization expression. Task of this expression is to initialize the looping variables. Properties of expression 1: 1. Expression1 can initialize the more than one variable. For example: #include<stdio.h> int main(){ int i,j,k; for(i=0,j=2,k=1;i<=4;i++){ printf("%d ",i+j+k); } return 0; } Output: 3 4 5 6 7 2. Expression1 is optional. For example: #include<stdio.h> void main(){ int i=1; for(;i<=4;i++){ printf("%d ",i); } return 0; } Output: 1 2 3 4 3. Unlike to the java in c we cannot declare the variable at the expression1. Fo r example: #include<stdio.h> int main(){ for(int i=0;i<=10;i++){ printf("%d ",i); } return 0; } Output: ce Expression 2: It is called as conditional expression. Task of expression is to c heck the condition and if it is false then it terminates the loop. For example: #include<stdio.h> int main(){ int i; for(i=1;i<=3;i++){ printf("hi ");

} printf("%d",i); return 0; } Output: hi hi hi 4 Properties of expression2: 1. Expression2 can have more than one checking condition and if any condition i s false loop will terminate. For example: (a) #include<stdio.h> void main(){ int i,j=2; for(i=0;i<=5,j>=0;i++){ printf("%d ",i+j); j--; } return 0; } Output: 2 2 2 (b) #include<stdio.h> int main(){ int i,j=2; for(i=0;j>=0,i<=5;i++){ printf("%d ",i+j); j--; } return 0; } Output: 2 2 2 2 2 2 2. Expression2 is also optional. For example: #include<stdio.h> int main(){ int j; for(j=0; ;j++){ printf("%d ",j); if(j>=2) break; } return 0; } Output: 0 1 2 3. It can perform task of expression1 as well as expression3. That is it can ini

tialize the variables as well as increment the variables. For example: (a) #include<stdio.h> int main(){ int i; for(;i=0,i<=3 ;i++){ printf("%d ",i); } return 0; } Output: Infinite Loop (b) #include<stdio.h> int main(){ int i=0; for(;i+=2,i<5 ;i++){ printf("%d ",i); } return 0; } Output: 2 4. If expression2 is zero means condition is false and any non zero number means condition is true. For example (a) #include<stdio.h> int main(){ int i; for(i=0;-5 ;i++){ printf("%d ",i); if(i==3) break; } return 0; } Output: 0 1 2 3 (b) #include<stdio.h> int main(){ int i; for(i=5;0 ;i++){ printf("%d ",i); } return 0; }

Output: 5 Expression 3: It is called as instrumentation expression. Task of this expression is to increm ent the variable. Properties: 1. We can increment more than one variable at the same time in the expression3. For example (a) #include<stdio.h> int main(){ int i,j,k; for(i=0,j=0,k=0;i<=5,j<=4,k<=3;i++,++j,k+=2){ printf("%d ",i+j+k); } return 0; } Output: 0 4 (b) #include<stdio.h> void main(){ int i,j=0; for(i=0;i<=3;++i,i++,++j ){ printf("%d %d ",i,j); } return 0; } Output: 0 0 2 1 2. Expression 3 is also optional. For example #include<stdio.h> int main(){ int i; for(i=0;i<=3; ){ printf("%d ",i++); } return 0; } Output: 0 1 2 3 Loop body: Loop body contains the part of code which we have to execute multiple numbers of times. Properties of loop body: 1. If loop body contain only one statement then brace is optional. For example: (a)

#include<stdio.h> int main(){ int i,j=0; for(i=0;i<=3;++i,i++,++j ) printf("%d %d ",i,j); } return 0; } Output: 0 0 2 1 (b) #include<stdio.h> int main(){ int x,y=5; for(x=0;x<3;x++) if(y>=5) printf(" %d",x); return 0; } Output: 0 1 2 2. Loop without body is possible. For example #include<stdio.h> int main(){ int i; for(i=0;i<=10;i++); printf("%d",i); return 0; } Output: 11 3. Braces of loop body behave as block. For example #include<stdio.h> int main(){ int i; for(i=0;i<=2;i++){ int i=8; printf("%d ",i); } printf("%d",i); return 0; } Output: 8 8 8 3 While loop Do while loop

break and continue Nested loop C tutorial home. 19 comments: karthik said... can any one help me why 4 b got answer as 5 even the condition is false 4/23/10 4:50 PM sofi said... can anyone temme why the ans for 3a is infinite loop 5/4/10 3:20 PM Ritesh kumar said... Hi karthik, You are right. It is bug of turbo c compiler. Loop will execute at one time even condition is false. Hi sofi, You can see in each iteration value of i becomes zero. so loop condition will al ways true. 7/31/10 12:10 AM anish said... Please explain 1(b) and 3(b) more clearly 5/29/11 9:09 PM anish said... sorry not 3(b) but 3(a) how it will be infinite loop and not 0 1 2 3 5/29/11 9:13 PM Ritesh kumar said... Hi Anish, 3(a) As you know i=0,i<=3 will be evaluated in each iteration. So due to i=0 value of i will alway 0. So loop will print 0, 0, 0 and so on 5/30/11 11:15 AM anish kumar said... ya it is clear now ,thanks 6/3/11 4:02 PM Anonymous said... thanks for this site dude 7/21/11 1:05 AM Anonymous said... void main() int i; clrscr(); for(i=0;7<=6;i++) { printf("santosh"); } getch(); } HOW THIS CODE IS WORKING ..PLZ XPLAIN IT..I AM CHKED THIS CODE IN TURBO C , I AM

GETTING O/P SANTOSH EVEN THE CODITION IS FALSE. 9/26/11 11:31 PM Anonymous said... void main() { int i; clrscr(); for(i=0;7<=6;i++) { printf("santosh"); } getch(); } HOW THIS CODE IS WORKING ..PLZ XPLAIN IT..I AM CHKED THIS CODE IN TURBO C , I AM GETTING O/P SANTOSH EVEN THE CODITION IS FALSE 9/26/11 11:32 PM cyberway91@gmail.com said... nice yar! i love this 9/26/11 11:50 PM chandan said... need triangular floyd s program code 9/30/11 1:57 PM Anonymous said... can any one explain the last code plz its confusing.. the condn value is i.e 8<2 10/7/11 9:21 PM Anonymous said... Dear first Anonymous, see the comment below Ritesh kumar said... Hi karthik, You are right. It is bug of turbo c compiler. Loop will execute at one time even condition is false. 7/31/10 12:10 AM 11/14/11 2:10 AM Anonymous said... i think the condition 7>6 is not checked 1/11/12 3:36 PM Anonymous said... @ 8<2:-Default storage class of local variable is auto. Scope of auto variables are block in which it has been declared. When program control goes out of the sc ope auto variables are dead. So variable i which has been declared inside for lo op has scope within loop and in each iteration variable i is dead and re-initial ized. 1/11/12 4:04 PM jyoti said... can anyone tell me the ans of last question is 8,8,2 how is it why it it is not 8,8,8,3 2/10/12 12:14 PM

Nitin Verma said... Hey Karhtik, brother u r doing such a nice job keep it up and God will keep u in hearts of us.... thank you and ur blogspot...:) 2/20/12 6:36 PM vivek said... #include int main(){ int i; for(i=0;i<=2;i++){ int i=8; printf("%d ",i); } printf("%d",i); return 0; } Output: 8 8 3 but it should be 8 8 8 3.if not why 3/17/12 12:21 PM Post a Comment Links to this post Create a Link Newer PostOlder PostHome Subscribe to: Post Comments (Atom) MUST VISIT Grab this Headline Animator C COMPILER: GCC 4.1.2 C QUESTIONS AND ANSWERS C program examples C interview questions and answers Data type questions Variable naming rule questions Operators questions Control flow questions Switch case questions Looping questions Pointer questions String questions Printf,Scanf questions Preprocessor questions Structure questions Commad line argument C questions in Linux C online test C mixed practice sets C tricky questions Example of recursion in c C programming forums C TUTORIAL Memory mapping tutorial in c Variables tutorial in c

Data types tutorial in c Storage classes tutorial in c Looping tutorial in c Pointers tutorial in c Function tutorial in c Array tutorial in c Preprocessor tutorial in c Advanced c tutorial POPULAR POSTS C program examples C interview questions and answers Data type questions in c C multiple choice questions and answers pdf Find out the perfect number using c program C pointers questions Tricky c questions and answers C questions and answers C objective questions and answers pdf Looping questions in c and answers SUBSCRIBE VIA EMAIL Enter your email address: Delivered by FeedBurner C PROGRAMMING QUESTIONS AND ANSWER C questions and answers Debugging questions in c with answers Aptitude questions and answers in c C basic questions Multiple choice questions in c STANDARD OF QUESTIONS ? SUBSCRIBE TO Posts Comments SHARE POSTS MY HEADLINES C programming questions and answer C questions and answers Debugging questions in c with answers Aptitude questions and answers in c C basic questions Multiple choice questions in c Objective questions of c Program in c language Simple program example in c language Tricky c questions and answers C programming online test C Linux interview questions and answers Looping questions in c and answers C string questions and answers with explanation C operator questions with answers Check the given number is palindrome number or not using c program C pointers questions Data type questions in c Check given number is prime number or not using c program struct bit fields questions in c C questions answers C interview questions and answers How to test palindrome in c++ Palindrome number in c++

Program of palindrome number in c++ C++ code to get sum of all odd numbers in given range

ABOUT ME ritesh kumar View my complete profile C LOVER COMMUNITY Help the community by publishing your posts Copyright@ritesh kumar. Powered by Blogger.

Potrebbero piacerti anche