Thread: Having issues "hardcoding" an integer

  1. #1
    Registered User Terabojin's Avatar
    Join Date
    Aug 2013
    Location
    California
    Posts
    1

    Having issues "hardcoding" an integer

    Hello everyone! I am just starting to play around with C programming, not C++, not C#, but just C. I'm trying to write a program that declares an integer called num and "hardcodes" the value to be 1000. Then I need to use a printf() statement that displays "1000 is the value of the variable num". I have some of the programming done so far but when I use the %d format specifier instead of 1000 being displayed I get a 0. Having issues "hardcoding" an integer-1000num-jpgHaving issues "hardcoding" an integer-0num-jpg Can someone please help me? I can't figure out what I've done wrong/not done.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I believe you will find this link quite useful:
    printf - C++ Reference
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your printf() statement, put "\n" inside the format string quotes on the left hand side of it. Then add a comma, after the last ", and num);

    By the way, we have a great C Tutorial here - just click on the "C Tutorial" at the top of this forum.

    And Welcome to the forum, Terabojin!
    Last edited by Adak; 08-29-2013 at 01:42 AM.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Don't define a number twice. You've already declared an int and I think C, by default, initializes it to 0. Or is that just C++? I knew this would happen if I started learning other languages.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MutantJohn View Post
    Don't define a number twice. You've already declared an int and I think C, by default, initializes it to 0. Or is that just C++? I knew this would happen if I started learning other languages.
    Global and static variables are initialized to 0 or NULL, by default. Automatic variables are not.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To clarify and expand on what Adak said:
    In the OP's case, num is an automatic storage duration variable. Whether it is initialized is hard to say, the code they posted wont compile, so no way to know what the code they ran actually was.

    Variables with static storage duration (global variables and static local variables) are implicitly (by default) initialized to 0/NULL only if they are not explicitly initialized.

    Variables with automatic storage duration (non-static local variables) are not implicitly initialized to anything (i.e. they contain "garbage" values and shouldn't be used without first giving them a reasonable value). They may, however, be explicitly initialized.

    There's more to it than that, for full details, see C99/C11 6.7.8 Initialization and C99/C11 6.2.4 Storage duration of objects

  7. #7
    Registered User Stormboy's Avatar
    Join Date
    Aug 2013
    Location
    Planet Earth
    Posts
    12
    %d is a formatter for integer values. When you use %d in a printf statement always specify the variable after string quotes followed by a comma with an integer. In your case you are don't doing that. Also, when you define a variable, you don't need to define it again to assign something to it. Furthermore, you don't need to open new quotes to put \n. And whenever you finish with the main function, always return 0.
    Code:
    //This is wrong
    int x;
    int x = 100;
    
    //This is correct
    int y;
    y = 100;
    
    //This is also correct
    int z = 100;
    Try this code, its a fixed version of yours.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 1000;
        printf("%d is the value of the variable num\n", num);
        return 0;
    }

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Compile with warnings enabled, and the compiler itself would have told you this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  2. Replies: 6
    Last Post: 04-05-2010, 09:09 AM
  3. Replies: 2
    Last Post: 09-12-2006, 04:50 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM