Thread: Am I doing these correctly?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    25

    Am I doing these correctly?

    Ok, so I have no programming experience and I started using "Let Us C" by Yashavant Kanetkar to teach myself C a few days ago. Unfortunately, I don't have the solutions to the exercises in the book, so I was wondering if I could get a little help from y'all. Here's the exercise, with each statement followed by my answer:

    Point out the errors, if any, in the following C statements:

    1.
    Code:
    int = 314.562 * 150;
    no error

    2.
    Code:
    name =  'Ajay';
    both quotes should point to the left (i can't show this on my computer, but one quote is pointing one way and the other is pointing the opposite way)

    3.
    Code:
    varchar = '3';
    both quotes should point to the left (i can't show this on my computer, but one quote is pointing one way and the other is pointing the opposite way)

    4.
    Code:
    3.14 * r * r * h = vol_of_cyl;
    variable should be on the left, constant on the right

    5.
    Code:
    k = (a * b)(c + (2.5a + b)(d + e);
    * must be used for multiplication


    6.
    Code:
    m_inst = rate of interest * amount in rs;
    variable names can't have spaces in them

    7.
    Code:
    si = principal * rateofinterest * numberofyears/100;
    no errors

    8.
    Code:
    area =  3.14 * r**2;
    should be re2, not r**2

    9.
    Code:
    volume = 3.14 * r^2 * h;
    should be re2, not r^2

    10.
    Code:
    k = ((a * b)+c)(2.5 * a + b);
    must use * for multiplication. can't have parenthesis within parenthesis

    11.
    Code:
    a=b=3=4;
    3 and 4 aren't the same? that's about all i can come up with.

    12.
    Code:
    count = count + 1;
    count isn't defined

    13.
    Code:
    date = '2 Mar 04';
    variables can't have spaces in them





    Ok, as you can see, I'm pretty sorely in need of help. I really appreciate any assistance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. int is a keyword
    2. What you said
    3. What you said
    4. Technically it's not a constant...
    5. What you said
    6. What you said
    7. What you said
    8. What you said
    9. Isn't technically incorrect (bitwise XOR), but given the context then it's wrong.
    10. Is perfectly legal, your answer is wrong.
    11. What you said
    12. If it's defined then it's legal. Otherwise, most of the others would be wrong =)
    13. Sure they can, this is valid but doesn't mean what you think it does. It's a multibyte character constant, all pasted together:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       unsigned int a = '1234';
    
    
       printf("&#37;X & %u\n", a, a);
    
       return 0;
    }
    [zac@neux ~]$ gcc -Wall -pedantic -std=c89 ln.c -o ln
    ln.c:5:12: warning: multi-character character constant
    [zac@neux ~]$ ./ln 
    31323334
    Notice that, '1' = 0x31, '2' = 0x32, '3' = 0x33 & '4' = 0x34
    Last edited by zacs7; 12-02-2008 at 12:25 AM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    9. What is bitwise XOR? I know what an exclusive or circuit is, but that's about it.
    10. Do you mean the part of my answer about parenthesis within parenthesis being wrong? I thought you couldn't use (yaddayadda)(blahblah) to mean yaddayadda * blahblah.
    13. Ok, I think you lost me here completely. I'm completely new to all this. Why does '1' = 0x31, '2' = 0x32, '3' = 0x33 & '4' = 0x34? Where are the 30's coming from, and where is th multiplication coming from? I don't see any spaces in the constant there.

    And by the way, thanks so much for your help. Taking the time to go through each one - very cool of you, and very helpful to me. Who says there's no manners left in the world? Hopefully I'll eventually know enough to pay it back!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > What is bitwise XOR?
    exclusive-or at a bit level. It does the same thing as it does in a circuit. And as you know, a byte is made up of bits (usually 8 of them). It does an XOR on each bit of the byte (or bytes) -- it's an operator. See http://www.cprogramming.com/tutorial...operators.html for a tutorial. XOR is explained near the bottom of the page.

    > Do you mean the part of my answer about parenthesis within parenthesis being wrong? I thought you couldn't use (yaddayadda)(blahblah)
    You can use parenthesis withing parenthesis. That's not what (yaddayadda)(blahblah) is though :-) which is wrong. Your explanation of why it's wrong is wrong...

    > Ok, I think you lost me here completely.
    Sorry :-). Anything in single quotes is a "character constant". If you specify more than one byte in the single quotes then they're pasted together. '1' in hex is 31... anything prefixed with 0x means it's in hex. If you look-up the ASCII table you'll see that '1' does indeed equal 0x31 and so on. For example, a space ' ' is equal to 0x20 (32 in decimal).

    These "numbers" come from the ASCII table, which most computers use (or extensions of, ie Unicode) these days.
    Last edited by zacs7; 12-02-2008 at 01:17 AM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Ok, let me work on what you've given me, and then I'll get back to you if I'm befuddled by anything else. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String search not returning correctly?
    By Blackroot in forum C++ Programming
    Replies: 5
    Last Post: 08-18-2008, 06:41 PM
  2. Did I Install the Platform SDK Correctly?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2008, 09:33 AM
  3. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  4. Why does this not read in correctly?
    By bluebob in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 09:17 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM