Thread: Identify the invalid C expression

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    14

    Identify the invalid C expression

    Can you please explain which is invalid and why? Thanks

    CAN YOU PLEASE EXPLAIN.
    Identify the invalid C expression or choose "all are
    valid". Assume all variables are integer and non-zero.
    7.
    a) a+b-0 b) c+-a%4 c) xm6-24 d) xf3r6+2
    e) all are valid

    8.
    a) xx+yy%zz b) z%(z%z%z)) c) ha+ha+ha d) x23-20.4
    e) all are valid

    9.
    a) 0-0-0 b) mx+bb/c c) xd-wd+2ax d) 32+45%x
    e) all are valid

    10.
    a) ant+int b) one+twx c) three*3 d) four/-4
    e) all are valid

    11.
    a) -a+b-33 b) 3.4%7-ab c) scanf-3/3 d) xaab+-37
    e) all are valid

    12.
    a) max3*3 b) CHAR-5 c) x0x-c+a%a d) a*abc+
    e) all are valid

    13.
    a) 3-x/z+4.3 b) a/b/cd c) x+2-2 d) x²/x-77
    e) all are valid

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Same as before, we wont do your homework for you. Tell us what you think the answers are and why, and we'll confirm or correct and explain.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    Can some explain to me how which are invalid and why? I am not asking you to do my homework for me, just explain.

    p.s this is not homework, im studying for midterm.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Read post #2 again, and do what it suggests.

    If we tell you which are invalid, then we'd be doing the work for you, and that's not how it works here.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Breakdown the problem.
    1. Identify the operations, constants and variables in each possible answer
    2. Are the variables valid or not
    3. Are the operations done on the constants and variables valid or not
    4. Are the constants valid or not

    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

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    ok so for 7- all are valid
    8- b is not valid because it need bracket at beginning
    9 - all are valid
    10 - a is not valid cant have int
    11 - c is invalid, can't have scanf
    12 - d is invalid, you cant have + at the end

    are these correct?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Everything looks good, except 11. Note, scanf is not an keyword, like int. It's simply the name of a function in the standard library. You can see it working here:
    Code:
    $ cat foo.c
    int main(void)
    {
        int scanf = 42;
        return scanf;
    }
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    $ ./foo
    $ echo $?  # This echos the exit value of the last command
    42
    Note, I specifically chose not to #include <stdio.h>, which is where scanf is defined. If it's not defined there, it's a perfectly valid, although dumb and confusing, variable/function name.

    What's your next guess? Hint, one of them uses an operator with an invalid type, i.e. that type does not work with the operator used.

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    i think its d, you cant have +-.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you can.
    Code:
    int main()
    {
        int xaab = 0;
        int c = xaab + -37;
        return 0;
    }
    Compiles with no issues because unary minus just makes a number negative.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To explain why something doesn't work you need to reference variable naming rules (what are the rules for choosing C identifiers?) and you should also learn how to add whitespace between tokens to make things clearer, e.g. 3.4%7-ab looks a little hard on the eyes but it is the same as 3.4 % 7 - ab (5 tokens).

    It's also good to try out the expressions in some example program as whiteflags has done. For a beginner you should do this for ALL of the expressions, even if you're pretty sure they're right. You will learn to use the output of your compiler to your advantage.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    On the scanf question, you can also get an integer result with this, because scanf where scanf refers to the function will be decayed to an int in your expression (this is legal but good compilers will issue a warning because its probably not what you want).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int res = scanf - 3 / 3;
        printf("%d\n", res); // Print some integer value related to the location of scanf in memory
    }
    If you declare scanf within the inner block with your own version, then your own version shadows the outer version of scanf.

    Code:
    #include <stdio.h>
    
    int scanf(const char *fmt, ...); // outer scanf is a function declared in stdio.h
    
    int main(void)
    {
        int scanf = 9;            // this inner scanf is now just like any old int
        int res = scanf - 3 / 3;  // 9 - 3 / 3
        printf("%d\n", res);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: Invalid use of void expression
    By pillaw in forum C Programming
    Replies: 3
    Last Post: 03-16-2014, 10:47 PM
  2. "invalid use of void expression" error
    By smith283 in forum C Programming
    Replies: 19
    Last Post: 09-04-2012, 09:10 AM
  3. invalid use of void expression
    By kittycase in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 11:21 AM
  4. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  5. invalid use of void expression
    By bradleyd in forum C Programming
    Replies: 7
    Last Post: 07-05-2007, 04:09 PM