i just wanted to make a program which reads 5 numbers then writes this 5 number. that's it but
i couldn't. something's wrong and i couldnt find what is it.

#include <stdio.h>




void fun1(void)
{
    int a=0,b=5,c=0;
    int d[5];
        for(a=0;a<b;a++)

        {


                printf("%d\n",d[a]);
        }






}
int main()

{
    printf("enter 5 numbers");
    int a,b=5,c=0;

    int d[5];
        for(a=0;a<b;a++)
        {
            scanf("%d",&d[a]);

        }

fun1();
return 0;
}

Recommended Answers

All 3 Replies

your function requires values to display...these values have been input by the user in the main program so they need to be passed to the fuction...change line 6 to this
void fun1(int d[]) // this passes the values input by the user to the function

also change line 37 to fun1(d) ....

What actually is happening is that. You have created 2 arrays with the same name in two different functions.

So in main the values are getting assigned to the values in the array in line 30 ;

And the values that are being printed are from the array which is in line 9;

Therefore it prints out some values which are stored in the array from fun1 but not the values that are stored in main .

You can stop this from happening by just making your array a global variable.
And remove the other ones.

#include <stdio.h>

int d[5];




	void fun1(void)
	{
		int a=0,b=5,c=0;

			for(a=0;a<b;a++)

			{
				
		
					printf("%d\n",d[a]);
			}




		

	}
	int main()

	{
		printf("enter 5 numbers");
		int a,b=5,c=0;
		
			for(a=0;a<b;a++)
			{
				scanf("%d",&d[a]);

			}

	fun1();
	return 0;
	}

ok thnx guys i understood where my mistake is

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.