Help with variables (newbee)

This is a discussion on Help with variables (newbee) within the C Programming forums, part of the General Programming Boards category; I am trying to use conditinal statements to define text in variables. ex if (x>0) { somevariable="This is summer"; } ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    Help with variables (newbee)

    I am trying to use conditinal statements to define text in variables.

    ex

    if (x>0)
    {
    somevariable="This is summer";
    }

    i later want to be able to recall the variable.

    such as

    printf("%s",somevariable);

    however, my output reads foreign characters or random lines in the program.

    any help is greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about posting your actual code, and not some non-complete example? My guess is you forgot your null.

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

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    You want to declare your #defines in global space, outside of main(). Also on the line
    Code:
     while (rerun=1) /* user selection handling   */
    you typo'd and forgot the second =. To fix your printf() statment switch %c to %s for curname.

    [edit]umm...I saw code, I replied, and I dont see code anymore . I dont know what just happened, and if a mod wants to delete this post, go ahead.[/edit]
    Last edited by Draco; 08-17-2004 at 01:48 PM.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    was missing an asterisk in the declaration. thanks....

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by Draco
    You want to declare your #defines in global space, outside of main(). Also on the line
    Code:
     while (rerun=1) /* user selection handling   */
    you typo'd and forgot the second =. To fix your printf() statment switch %c to %s for curname.

    [edit]umm...I saw code, I replied, and I dont see code anymore . I dont know what just happened, and if a mod wants to delete this post, go ahead.[/edit]
    #define's dont have scope
    hello, internet!

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    True, but it's still a good idea to always have them very close to the top of the file. That's where they most often are, and since you gain nothing by having them elsewhere (due to the scopelessness), you might as well have them where people would look for them first.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Well, saying they have no scope isn't quite correct. They can't be used anywhere in the file above where they're defined. For example:
    Code:
    int foo = FOO;
    #define FOO 1
    ...won't work.

    Scope could be defined as between the #define and #undef directives or the end of the file. I've used that sort of thing before too:
    Code:
    ATTR atr_list[] =
    {
    #define DOATR(a, b, c, d) { a, d, NULL, c, NULL, NULL },
    #include "attrlist.h"
    #undef DOATR
      { NULL, 0, NULL, 0, NULL, NULL }
    };
    #define DOATR(a, b, c, d) ATTR *b;
    #include "attrlist.h"
    #undef DOATR
    where attrlist.h is a lot of lines like:
    Code:
    DOATR("Password",          A_PASSWORD,         SDARK|NOMOD,        5)
    DOATR("Desc",              A_DESC,             INH|OSEE,           6)
    So anyway, just thought I'd point that out
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21