Thread: 2 simple C program questions.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    2 simple C program questions.

    1. How do you declare a variable s containing the string hello?

    My attempt:
    char s = "hello";

    2. Why is it that when the following code executes:

    int i = -1;
    int k = i++;

    k is -1, isn't k 0?

    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Both of these questions should be easy to answer after having read some introductory material on C, so re-read your book/notes on strings and on post-increment.
    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
    Sep 2011
    Posts
    7
    Okay I found out the first one is chars[] = "hello";

    But for the second question I cannot find it in my notes :S.

    I was thinking that i++ only works when it is on it's own, ex:

    int i = -1;
    i++;
    int k = i;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by justiliang
    Okay I found out the first one is chars[] = "hello";
    With the space you had before, yes

    Quote Originally Posted by justiliang
    But for the second question I cannot find it in my notes :S.

    I was thinking that i++ only works when it is on it's own, ex:
    If you cannot find "post-increment", then just try "increment", or look back to where the operator was introduced to you. You might also recall pre-incement, e.g., ++i. The crux of this question is how does i++ differ from ++i
    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

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    OOOO, yeah I found increment.

    So i++ is a postfix position and it looks like

    int k = i++;

    is the same as

    int k = i;
    i = i + 1;

    ?

    Thanks for the help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by justiliang
    So i++ is a postfix position and it looks like

    int k = i++;

    is the same as

    int k = i;
    i = i + 1;

    ?
    For the purposes of this discussion, yes, you have the right idea.
    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. simple chat program questions
    By surefire in forum C Programming
    Replies: 5
    Last Post: 11-05-2009, 08:16 PM
  2. <<simple stack program questions
    By rdt253 in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2005, 03:10 AM
  3. simple questions
    By DeathDealer in forum C++ Programming
    Replies: 6
    Last Post: 02-13-2005, 05:01 PM
  4. Some simple questions
    By xErath in forum C++ Programming
    Replies: 18
    Last Post: 07-08-2004, 05:34 PM
  5. Simple questions
    By jdinger in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 03:49 AM