What is the purpose of this program?

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
shermaine80
Messages
30
Reaction score
0
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.
 
Physics news on Phys.org
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.
 
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.
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.


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.
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)
 
Thanks! But could you advise me how come it's 54?
r%10 = 6%10 = 0.6, rite?
what does '0' means?
 
6%10 = 0.6, rite?
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'.
 
thanks a lot! i get it :)