Thread: error while using #define. plz help!!

1. error while using #define. plz help!!

n numbers are entered from the keyboard into an array. The number to be searched is

also entered by the user. Write a program to find if the number to be searched is present in the array and if
it is present, display the number of times it appears in the array.

Sample Input #1: (e.g. if n is set as 10 i.e. #define n 10)
Enter 10 numbers : 10 20 23 -6 52 34 22 66 -22 -1
Enter number to search: 40

Code:
```#include <stdio.h>
#define i 10

int main()

{
int nos[10]={0};
int i=0,j=1,chck,count=0;

for (i =0; i<10; i++){
printf("Enter no %d: ",j);
scanf("%d",&nos[i]);
j++;
}

printf("Enter no to search for: ");
scanf("%d",&chck);

for (i=0; i<10; i++){
if(nos[i]==chck){
count=count+1;
}
}

if(count>0){
printf("Number %d is found %d times.",chck,count);
}

getch();
return 0;
}```
please help me removing this error and plz guide me a bit how to use #define especially with arrays.... I will be thankful to u...

2. You #define i to be 10, then declare an int variable i. But the preprocessor first goes through and replaces all occurances of 'i' with 10 in your code, so it looks something like:
Code:
```int main()

{
int nos[10]={0};
int 10=0,j=1,chck,count=0;

for (10 =0; 10<10; 10 ++){
printf("Enter no %d: ",j);
scanf("%d",&nos[10]);
j++;
}

printf("Enter no to search for: ");
scanf("%d",&chck);

for (10=0; 10<10; 10 ++){
if(nos[10]==chck){
count=count+1;
}
}

if(count>0){
printf("Number %d is found %d times.",chck,count);
}

getch();
return 0;
}```
All that stuff in red is where i got replaced by 10. It's common convention to make anything you #define be all caps so you know what is a preprocessor symbol and what isn't. Also, picking a more sensible name than 'i' would be good, like MAX_NUMS. So try #define MAX_NUMS 10. Then your code should be like:
Code:
```#define MAX_NUMS   10
...
int nos[MAX_NUMS] = {0};
...
for (i = 0; i < MAX_NUMS; i++)```

3. thank u 100X .. anduril462