Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Enquiry For this program

  1. Nov 25, 2006 #1
    May i know what does this program does?

    int days[20];
    int size;

    size = sizeof(days) / sizeof(int);
    for (j =1; j <= szie; j++)
    days[j] = 30;

    Can anyone advise me? Thanks.
     
  2. jcsd
  3. Nov 26, 2006 #2
    May i know the output for this?

    Hi,

    May i know what's the output for this?

    #include<stdio.h>
    main()
    {
    int r; int s;
    r = 6;
    s = r%10 + '0';
    printf("%d",s);
    }

    The output i get from c program is 6.
    How come it's 6? I'm a bit puzzled. r%10 = ?
    Can someone help me? Thanks.
     
  4. Nov 26, 2006 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It looks like

    (1) It allocates an array (days) of 20 ints.
    (2) It allocates an int. (size)
    (3) It assigns the value 20 to size.
    (4) It assigns the value 30 to the last 19 entries of days.
    (5) It commits undefined behavior.


    The output is 54, not 6.

    % is the modular reduction operator. a%b is the remainder when you divide a by b. (I forget precisely how the signs work when a or b are negative)
     
  5. Nov 26, 2006 #4
    Thanks! But could you advise me how come it's 54?
    r%10 = 6%10 = 0.6, rite?
    what does '0' means?
     
  6. Nov 26, 2006 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Not quite: remember that

    numerator / demonator = quotient + (remainder / denominator)

    '0' is a character: something of type char. If you look it up in the ASCII tables, you'll find that the character '0' is represented by the integral value of 48.

    Now, if you had used %c instead of %d as your format specifier, printf would have printed the character that 54 represents: '6'.
     
  7. Nov 26, 2006 #6
    thanks a lot!! i get it :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Enquiry For this program
  1. Programming in mips (Replies: 1)

  2. Root programming (Replies: 3)

  3. Explain this program. (Replies: 3)

  4. Programming languages (Replies: 16)

  5. HMI programming (Replies: 1)

Loading...