Thread: Type of Error in C MCQ

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    47

    Type of Error in C MCQ

    Q. Consider the following C declaration.
    int x[10], y,z;
    What kind of error does following statement contain?
    z == x +y;

    a. Syntax error
    b. Semantic error
    c. logical error
    d. All of the above

    According to me it should be option a, but I am confused, can anybody get confirm answer with explanation.

    Thanks a lot for your replies

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you think there is a syntax error?
    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

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by laserlight View Post
    Why do you think there is a syntax error?
    because x is declared as array and z is not so

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But syntax can be correct and meaningless at the same time. Observe it in English: "The flow of quizzes revels in authority." (Nonsense generator) The meaning of the statement is the problem.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by whiteflags View Post
    But syntax can be correct and meaningless at the same time. Observe it in English: "The flow of quizzes revels in authority." (Nonsense generator) The meaning of the statement is the problem.
    so what should be the answer according to you

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shyjuu
    because x is declared as array and z is not so
    Yeah, but that is somewhat incomplete. Go further: the left hand side expression of the == is of type int. The right hand side expression is of type int*, since the array is converted to an int*, then when you add the int to that, the resulting expression is still an int*. Hence, this compares an int with an int*.

    The thing is, syntactically, the statement is valid. We can refer to the syntax from Clause 6.5.9 of C99:
    Code:
    equality-expression:
        relational-expression
        equality-expression == relational-expression
    I do not want to delve too deeply into this, so take my word that both z and x + y are relational expressions.

    The operand types provide semantic information; they give meaning to the expressions (e.g., "we're comparing two pointers"), and when we look at the constraints on operator==
    Quote Originally Posted by C99 Clause 6.5.9 Paragraph 2
    One of the following shall hold:
    — both operands have arithmetic type;
    — both operands are pointers to qualified or unqualified versions of compatible types;
    — one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or
    — one operand is a pointer and the other is a null pointer constant.
    We see that actually the expression has no meaning because the types of the operands of operator== violates the constraints required to interpret it. Therefore, there is a semantic error.

    It may be possible to argue that there is also a logical error because the author presumably intended some logic that did not happen because of the semantic error.
    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

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Thanks for such a good explanation
    yes I am also confused about logical error, so finally what should be the answer SEMANTIC or LOGICAL

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the program might still compile but produce a wrong result (but note that as given, the snippet has no net effect). However, I believe that a compiler could reject the program. So, what answer would you pick?
    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

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by laserlight View Post
    Well, the program might still compile but produce a wrong result (but note that as given, the snippet has no net effect). However, I believe that a compiler could reject the program. So, what answer would you pick?
    may be syntax or semantic, because complier would reject if syntax is not correct, what do you say

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by shyjuu View Post
    may be syntax or semantic, because complier would reject if syntax is not correct, what do you say
    Gah! The point is for you to take what you have learned, analyze and decide for yourself. You have been given enough info to eliminate two of the 3 types of errors. If you are trying to decide between syntax and semantic, reread laserlight's post #6 very carefully.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by anduril462 View Post
    Gah! The point is for you to take what you have learned, analyze and decide for yourself. You have been given enough info to eliminate two of the 3 types of errors. If you are trying to decide between syntax and semantic, reread laserlight's post #6 very carefully.
    I had read it carefully before according to that answer is SEMANTIC error

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by shyjuu View Post
    I had read it carefully before according to that answer is SEMANTIC error
    Good. I agree with your conclusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type name error?
    By Pztar in forum C Programming
    Replies: 4
    Last Post: 06-12-2011, 09:30 AM
  2. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  3. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM
  4. a new type of error to me?
    By pastitprogram in forum C++ Programming
    Replies: 5
    Last Post: 09-04-2008, 11:21 AM
  5. does not name a type ERROR
    By DarrenY in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2007, 04:54 AM