I want to create a program that displays 10 numbers while inputting them.

Asked 2 years ago, Updated 2 years ago, 32 views

I'd like to create a program to display while entering 10 numbers.

#include<stdio.h>
int main (void)

{
int a[10], i;
for (i=1; i<=10;i++)
{
scanf("%d", & a[i])
printf("%d\n", a[i]);
}
return(0);

It says, but where should I correct it be corrected?
I would appreciate it if you could let me know.

c

2022-09-29 22:00

2 Answers

inta[10];

The range of accessibility for arrays declared as is
Limited to 10 from a[0] to a[9].

Your code doesn't follow this.


2022-09-29 22:00

One closed parenthesis } is missing, so if you make up for it, you can compile and run it.The other problem is that there is no element a[10] as already commented/answered.

for(i=1;i<=10;i++) to
for(i=0;i<10;i++) will ensure that "display while typing" is achieved correctly.
printf("a[%d]=%d\n", i, a[i]); and so on may be less confusing.)

is:

- Visual Studio (or MSVC) generates warning C4996 for scanf - If you accidentally enter non-numeric characters (a or .), all of the following will fail.
- scanf() can read multiple elements in one line because of buffering.
- If you want to read only one number per line, you need a different approach
I can think of many factors such as "different behavior from what programmers expect."Even if it's a "right program," it's normal to "behave differently than the designer's intentions," and if you don't explain what behavior you don't like, you'll end up with a generalization.


2022-09-29 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.