What is the purpose of this program?

  • Thread starter shermaine80
  • Start date
  • Tags
    Program
In summary, the conversation is about a program that allocates an array of 20 integers, assigns the value 30 to the last 19 entries of the array, and commits undefined behavior. There is also a discussion about the output of a C program and how the modular reduction operator works. The output of the program is 54, not 6, because '0' is a character with an ASCII value of 48, which is added to the remainder of 6%10. The conversation ends with the person understanding the concept.
  • #1
shermaine80
30
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.
 
Technology news on Phys.org
  • #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.
 
  • #3
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)
 
  • #4
Thanks! But could you advise me how come it's 54?
r%10 = 6%10 = 0.6, rite?
what does '0' means?
 
  • #5
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'.
 
  • #6
thanks a lot! i get it :)
 

FAQ: What is the purpose of this program?

1. What is the purpose of an enquiry for this program?

An enquiry for this program is a request for information or clarification about the program, its goals, and requirements. It is typically submitted by individuals who are interested in applying to or participating in the program.

2. How do I submit an enquiry for this program?

The process for submitting an enquiry for this program may vary depending on the institution or organization offering the program. Typically, you can submit an enquiry through their website or by contacting their admissions or program office directly.

3. What kind of information should I include in my enquiry for this program?

When submitting an enquiry for this program, it is important to provide your full name, contact information, and any specific questions you have about the program. You may also want to include your academic or professional background and why you are interested in the program.

4. How long does it take to receive a response to an enquiry for this program?

The response time for an enquiry for this program may vary depending on the institution or organization. Some may respond within a few days, while others may take a few weeks. It is best to follow up if you have not received a response within a reasonable amount of time.

5. Can I submit multiple enquiries for this program?

Yes, you can submit multiple enquiries for this program if you have additional questions or concerns. However, it is best to consolidate your questions into one enquiry to avoid overwhelming the admissions or program office with multiple inquiries.

Back
Top