Thread: I have a question about ansi c

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    I have a question about ansi c

    Hi guys I have a textbook question about ansi c and I could not understand the answer.is there anyone who can help me ?

    question :
    determine the values of i,j,k after execution of the following program frangment:

    int i,j,k:
    i=j=k=1;
    i-=-j-------k;


    the answer according to book is i=2 but why ?

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    i-=-j-------k;
    What has science done?

    i - (-j) (ignoring that bullcrap after it)

    1 - (-1) = 2

    Also you ended line 3 with a colon instead of a semi-colon and the random ----------k so... yeah...
    Last edited by Rodaxoleaux; 03-07-2012 at 01:28 PM.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It doesn't compile. The only sensible thing I can make out of it that's close is
    Code:
    i -= - j-- - --k;
    which gives i=2, j=0, k=0
    but I had to remove two minus signs.
    Last edited by oogabooga; 03-07-2012 at 02:07 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Ohhhhh. I see it now. That's horribly formatted.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    As others have pointed out the first line with the variable defintions should end with a colon. I assumed that was a typo and replaced it with a semi-colon.

    So now concentrating on the primary line:
    Code:
    i-=-j-------k;
    As someone has already point out, this poorly formated. No real code should be written with that many operators in a row with out spaces and/or parentheses to emphasize the programmers intent.

    I ran this through two different compilers which gave the same diagnostics. The rest of this is my analysis of the source of those diagnostics.

    The rest of the references are based on the C99 standard in the form of the JTC1/SC22/WG14 working document N1256 which includes the C99 standard with the three Technical Corrigendum included.

    I have added spaces to separate the preprocessor tokens as I understand section 6.4 paragraphs 4 through 6:
    Code:
    i -= - j -- -- -- - k;
    So based on operator precedence based on the grammar in section 6.5 and its subsections, I believe the inner most expression is:
    Code:
    j --
    I do not see a problem with this; it returns the value in j and then decrements it.

    However, when we add the second postfix decrement operator (see below), we now have an error. From section 6.5.2.4 paragraph 1, "The operand of the postfix .... decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue." And from section 6.3.2.1 paragraph 2, "Except when it is the operand of ..., the -- operator, or ..., an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue)." (Emphasis added in both quotations.)
    Code:
    j -- --
    The error is repeated for the third postfix decrement operator. So this code segment will not compile on a C99 compliant compiler. (I suspect that the same is true for a C89 compiler, but I do not have access to that standard.)

    And if this was not a translation error, we are modifying j three times between sequence points and that would be undefined behavior.

    So in my opinion your book is wrong. However, you may want to check if your book as a published errata sheet. They may have already recognized their error.
    Last edited by pheininger; 03-09-2012 at 07:14 AM. Reason: Added minor sentence.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pheininger View Post
    I have added spaces to separate the preprocessor tokens
    There are no preprocessor tokens there. Even if there were, you would have no way of knowing if this: j------k is:

    a) j - -- -- k
    b) j -- - -- k
    c) j -- -- - k

    You can't do this: j-- --; "invalid lvalue in decrement".


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by quzah View Post
    There are no preprocessor tokens there.
    This is an example why the standard is a standard and not training material. And any code where you have to be a language lawyer to explain it (like I did earlier in the thread) needs to be rewritten. Back to language lawyer mode for a few minutes.

    Yes, there are no preprocessing directives here (section 6.10) but there are preprocessing tokens (described in section 6.4 paragraph 3). [I incorrectly used the term preprocessor tokens when the standard term is preprocessing tokens.] They are
    preprocessing tokens in translation phases 3 through 6 and then become just tokens in phases 7 and 8. (in section 6.4 paragraph 3, translation phases are defined in 5.1.1.2).
    Quote Originally Posted by quzah View Post
    Even if there were, you would have no way of knowing if this: j------k is:

    a) j - -- -- k
    b) j -- - -- k
    c) j -- -- - k

    You can't do this: j-- --; "invalid lvalue in decrement".
    I am going to assume that you meant 5 hyphens in all four examples.

    If I were maintaining this code, I do not know what the original programmer meant. (Since it won't compile, it will probably not get to maintanence any how.) But the standard gives us a direction. Section 6.4 paragraph 4 says "If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token. There is one exception to this rule: a header name preprocessing token is only recognized within a #include preprocessing directive ..." So the language decides choice c) for us.

    As as you point out, that choice is not a valid expression.

    End of language lawyer mode.
    Last edited by pheininger; 03-10-2012 at 02:54 AM. Reason: wording change: statement --> expression

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pheininger
    I incorrectly used the term preprocessor tokens when the standard term is preprocessing tokens.
    Aye, but when you want to be a language lawyer, you need to be accurate

    Otherwise, you could have just mentioned "greedy matching" instead, and hopefully quzah would have had a better chance of recognising the rule.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ansi-c?
    By Encrypted in forum C Programming
    Replies: 3
    Last Post: 01-22-2003, 11:11 PM
  2. ansi C question
    By moi in forum C Programming
    Replies: 1
    Last Post: 08-13-2002, 07:51 PM
  3. Ansi C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 05:44 PM
  4. ADO And ANSI C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-12-2002, 04:50 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM