Thread: C quiz questions

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    C quiz questions

    I need help with the following questions
    1)
    Code:
    int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;
    output?
    2)
    Code:
    int a=10,b;b=a++ + ++a;printf("%d,%d,%d,%d",b,a++,a,++a);
    I felt the answer would be 21,13,13,13 but the answer is 22,13,13,13.How b is coming as 22?
    3)
    Code:
    void myFunc (int x) {    if (x > 0)   myFunc(--x); 
       printf("%d, ", x); } 
    int main() 
    {    myFunc(5);    return 0; }
    I felt the answer should be 0, 1, 2, 3, 4, but is 0,0,1,2,3,4.How is the extra 0 coming?
    4) why does 11 ^ 5 show 14
    5)
    Code:
    int a=5;       a*=2+3
    .here a should have been 13(5*2+3) but it is coming as 25.How?
    6)
    Code:
    #include  int i;
     void increment( int i ) {    i++; } 
    int main() { 
       for( i = 0; i < 10; increment( i ) )
        {    } 
       printf("i=%d\n", i);    return 0; 
    }
    Why is this creating an infinite loop?

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    	
    void myFunc (int x) {    if (x > 0)   myFunc(--x);
       printf("%d, ", x); }
    int main()
    {    myFunc(5);    return 0; }
    I felt the answer should be 0, 1, 2, 3, 4, but is 0,0,1,2,3,4.How is the extra 0 coming?

    it comes out 0,1,2,3,4 for me :/
    Last edited by camel-man; 01-12-2012 at 12:49 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest looking up operator precedent and pass by value.

    C++ Operator Precedence - Cppreference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I also suggest you read up on sequence points. At least one of your questions has undefined behavior.

    Jim

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I felt the answer would be 21,13,13,13 but the answer is 22,13,13,13.How b is coming as 22?
    Because the code is garbage, and has undefined behaviour.
    Expressions

    If your teacher "claims" to know an answer, find another teacher. You're not going to learn anything useful from them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    1. Look up how pre/post increment and decrement work, and operator precedence. You can calculate this easily by hand. Give it a shot.
    2. Read this link, particularly questions 3.2 and 3.3: Expressions.
    3. Again, look up pre-decrement. Trace the code by hand, for each recursive call.
    4. What does the ^ operator do? Look it up in your book.
    5. Study operator precedence and order of operations.
    6. Like Tim said, study pass by value and how it works.



    @camel-man: Pay careful attention to where the recursive call happens in relation to the printf statement. Think of it like pre-order and post-order traversal in binary trees, if you're familiar with that.

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Yes I see that now thanks, Andruil462, how come when you change --x; to x-- it gives a seg fault?
    Last edited by camel-man; 01-12-2012 at 01:58 PM.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by camel-man View Post
    Yes I see that now thanks, Andruil462, how come when you change --x; to x-- it gives a seg fault?
    Because it will then exhaust the stack space and crash the program.
    Hint: No matter how many times the function is called with 5 it will always be greater than zero.

    Tim S.
    Last edited by stahta01; 01-12-2012 at 02:16 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by ayanbizz View Post
    I need help with the following questions
    1)
    Code:
    int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;
    output?
    None, obviously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quiz on C
    By ganesh bala in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 03:49 PM
  2. my Quiz
    By hanadi in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2009, 12:43 PM
  3. Quiz
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-27-2003, 09:16 AM
  4. Quiz
    By coolazz0 in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2003, 11:57 PM
  5. Quiz me!
    By Liger86 in forum C++ Programming
    Replies: 7
    Last Post: 01-31-2003, 08:04 PM

Tags for this Thread