Some basic C questions

This is a discussion on Some basic C questions within the C Programming forums, part of the General Programming Boards category; I was going through a few C quizzes and was quite baffled by the amount of questions i could answer ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Some basic C questions

    I was going through a few C quizzes and was quite baffled by the amount of questions i could answer correctly. Never thought C was such a confusing language !

    Well, anyway... here are some doubts i've had..

    1. Can a pointer be typecasted ? Why or why not ?

    2. I've heard that pointer can hold the address of a function. What does that mean ? Where is a function stored ?

    3. What does #define f(g,g2) g##g2 mean ?

    4.
    Code:
    Output of this program ?
    int i= 5;
    printf("%d",i++ + ++i)
    Answer : cannot be predicted.
    Why ?

    5.
    Code:
    Output of this ?
    int i=5;
    printf("%d %d %d %d %d",i++,i--,++i,--i,i);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    With those questions, I suggest you provide you thoughts on what the answers are. People will then be more inclined to give you pointers (no pun intended) to confirm or help you improve your understanding. But if you don't do that, it will be assumed you are just asking us to do homework for you.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by grumpy View Post
    With those questions, I suggest you provide you thoughts on what the answers are. People will then be more inclined to give you pointers (no pun intended) to confirm or help you improve your understanding. But if you don't do that, it will be assumed you are just asking us to do homework for you.
    Thats not homework.. I asked these questions because i have no clue as to what the answer is. If i knew so, i wouldnt be asking.

    But then, to elaborate on them more..

    1. Can a pointer be typecasted ? Why or why not ?

    Absolutely no clue about this.
    2. I've heard that pointer can hold the address of a function. What does that mean ? Where is a function stored ?

    According to my knowledge, when a function is called, the function details are stored in an activation frame which is pushed into the system stack.
    I've never heard of function arguments being pushed into anything. So, what exactly is goin on here ?
    3. What does #define f(g,g2) g##g2 mean ?

    What does ## between g and g2 means ??
    4.

    Output of this program ?
    int i= 5;
    printf("%d",i++ + ++i)

    Answer : cannot be predicted.
    Why ? Shouldnt this yield 5 + 6 = 11 ?

    5.


    Output of this ?
    int i=5;
    printf("%d %d %d %d %d",i++,i--,++i,--i,i);
    According to the answer to the above question, the function arguments are pushed from last to first and evaluated while being pushed. Please elaborate on this.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Do you understand what a typecast is? If not, you need to read your C programming literature a bit more. Then the answer will probably be apparent.

    2. You are not at all answering the question - what you replied was how a call-stack works, which has extremely little to do with the function pointers - about as much as tyres have to do with the air-fuel mixture of a petrol engine.

    3. Read your book (or search the web - this one should definitely be searchable).

    4. Because the order of i++, ++i and + is not defined. If i++ and ++i are performed before the addition of the result of the two, then you get 6 + 7 = 13, if ++i is done before addition and i++ after, then you get 11 or 12 (depending on if the value of the first instance of i is before or after ++i). [And technically, the compiler is "allowed" to come up with any answer, such as 3128357912 as the answer, since this is "undefined behaviour", and thus "anything can happen" - but it's likely that the answer is in the range 10-13, depending on which compiler, compiler optimization level, etc].

    5. In this case, the comma is separating the statements, so the answer is predictable, and you can then figure out (from the order of evaluation) what the result would be.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    1. Yes. But the result isn't necessarily valid (eg. a char pointer to an int pointer). Only to void* and back to the SAME type is allowed for sure.

    2. Yes. Look at the declaration of qsort in stdlib.h

    3. Read about token concatenation operator in the C preprocessor section of your manual / book

    4. http://c-faq.com/expr/index.html
    5. http://c-faq.com/expr/index.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Thanks a lot for the help.

    Quote Originally Posted by Salem View Post
    1. Yes. But the result isn't necessarily valid (eg. a char pointer to an int pointer). Only to void* and back to the SAME type is allowed for sure.

    2. Yes. Look at the declaration of qsort in stdlib.h

    3. Read about token concatenation operator in the C preprocessor section of your manual / book

    4. http://c-faq.com/expr/index.html
    5. http://c-faq.com/expr/index.html
    Quote Originally Posted by matsp View Post
    1. Do you understand what a typecast is? If not, you need to read your C programming literature a bit more. Then the answer will probably be apparent.

    2. You are not at all answering the question - what you replied was how a call-stack works, which has extremely little to do with the function pointers - about as much as tyres have to do with the air-fuel mixture of a petrol engine.

    3. Read your book (or search the web - this one should definitely be searchable).

    4. Because the order of i++, ++i and + is not defined. If i++ and ++i are performed before the addition of the result of the two, then you get 6 + 7 = 13, if ++i is done before addition and i++ after, then you get 11 or 12 (depending on if the value of the first instance of i is before or after ++i). [And technically, the compiler is "allowed" to come up with any answer, such as 3128357912 as the answer, since this is "undefined behaviour", and thus "anything can happen" - but it's likely that the answer is in the range 10-13, depending on which compiler, compiler optimization level, etc].

    5. In this case, the comma is separating the statements, so the answer is predictable, and you can then figure out (from the order of evaluation) what the result would be.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Questions
    By Panda_ in forum C Programming
    Replies: 15
    Last Post: 09-23-2009, 04:24 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Some Basic questions
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 06-30-2009, 09:51 AM
  4. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21