Thread: printing numbers starting with 0 in struct

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    printing numbers starting with 0 in struct

    I need to make a struct that contains a long for student id. In my case, my student id is 0xxxxxxx

    my code looks like this:
    Code:
    struct student {
    		char *name;
    		int age;
    		long int id;
    		float gpa;};
    
    int main(int argc, char *argv[])
    
    {
    	struct student student1 = {"name",xx,0xxxxxxx,x.x};
    
    	printf("%s\n",student1.name);
    	printf("%i\n",student1.age);
    	printf("%i\n",student1.id);
    	printf("%.1f\n",student1.gpa);
    }
    When I try to compile it, I get an octal constant error, which, or course, I don't want. So is there any way I can get this to work?

    Also, a less important question. My full id actually starts "A", but since the teacher wants me to use long, it can't be helped, right?

    Oh, and I am very new to C, so I'm aware that my code is probably horribly inefficient. Any tips would be appreciated. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    struct student student1 = {"name",xx,0xxxxxxx,x.x};
    Try
    Code:
    struct student s1 = {"Joe", 10, 10000, 0.0};
    A long can store a number - XXX isn't a number.

    Any number starting with 0 (like 0315) (except 0 itself) is an octal number.

    Any number starting with 0x (like 0xFF, or 0x4fAa4) is a hexadecimal (hex) number.

    The standard Windows calculator can convert between these formats.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't have to initialize a struct. You can do that later:
    Code:
    struct student s1;
    
    /* ... */
    
    strcpy(s1.name, "hippo");
    [edit]
    Actually, you would need to use malloc, since student::name is a pointer, not a character array:
    Code:
    s1.name = malloc(6);
    strcpy(s1.name, "hippo");
    /*...*/
    free(s1.name);
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    I can't seem to type 0 first into the calculator.
    And the xxx was supposed to represent numbers, since I didn't want to post the actual numbers. Sorry if there was any confusion, I don't know the syntax.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I can't seem to type 0 first into the calculator.
    Of course not. Look, go into Scientific mode. On the upper left will be an Oct button. Click it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And the xxx was supposed to represent numbers, since I didn't want to post the actual numbers.
    Of course, sorry. It's just telling you that you're using an octal number.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The "constant error" could be due to this:
    Code:
    char *name;
    Make it
    Code:
    char name[30];
    and see what happens.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Nothing different happens when I change the char part, so I think the problems stems only from the octal error. And I went to scientific mode and clicked the oct button, but I still can't put 0 first. ArGhh. Ok, say my id is 06688363. What would it convert to and how did you get it?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're in Octal mode, the numbers are automatically octal. Type a number, then click on "Dec" to convert it to decimal.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    wait wait. I'm confused.

    what I'm trying to do is get an output of 06688363
    that's my decimal.

    So what I have to do is convert 06688363 to octal so I can put the octal into the code, so it'll print out 06688363 when I run the program, right?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No. You can't have an integer hold a number like 06688363. It will become 6688363. Use a string or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    ok, I think I got it. Thanks for your quick and frequent help. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Armstrong Numbers from 1-500
    By duffmckagan in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:26 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM