What is the purpose of this program?

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around understanding the functionality and output of two C programs. Participants seek clarification on the behavior of array initialization and the output of a modular arithmetic operation combined with character representation.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant asks about the purpose of a program that initializes an array and assigns values, noting potential undefined behavior due to an out-of-bounds access.
  • Another participant inquires about the output of a program that uses the modulus operator and character arithmetic, expressing confusion over the result.
  • A later reply clarifies that the output of the modulus operation combined with character addition results in 54, not 6, and explains the significance of the character '0' in ASCII.
  • Further discussion includes a correction regarding the interpretation of the modulus operation, with participants discussing how the output relates to character encoding.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the output of the programs, with some clarifying misconceptions while others remain puzzled about specific details. There is no consensus on the initial confusion regarding the modulus operation.

Contextual Notes

Some participants exhibit uncertainty about the behavior of the programs, particularly regarding array indexing and character representation in C. There are unresolved questions about the implications of undefined behavior in the first program.

Who May Find This Useful

Individuals interested in C programming, particularly those seeking to understand array manipulation and character arithmetic in programming contexts.

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.
 
Technology 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 :)
 

Similar threads

Replies
12
Views
3K
Replies
20
Views
3K
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K