If I remove scanf the code works perfectly.Even the value of i is a garbage value.
insertCode:#include <stdio.h> #include <stdlib.h> main() { int arr[9],i; for(i=0;i<9;i++) { printf("Enter the %d th number:\n"); scanf("%d",arr[i]); } }
If I remove scanf the code works perfectly.Even the value of i is a garbage value.
insertCode:#include <stdio.h> #include <stdlib.h> main() { int arr[9],i; for(i=0;i<9;i++) { printf("Enter the %d th number:\n"); scanf("%d",arr[i]); } }
you missed the address operator in scanf and also there will be warning for printf.
I think you should have:
or to make it linguisicaly correct:Code:#include <stdio.h> #include <stdlib.h> int main() { int arr[9],i; for(i=0;i<9;i++) { printf("Enter the %dth number:\n",i); scanf("%d",&arr[i]); } return 0; }
Code:#include <stdio.h> #include <stdlib.h> int main() { int arr[9],i; i=1; printf("Enter the %dst number:\n",i); scanf("%d",&arr[i]); i=2; printf("Enter the %dnd number:\n",i); scanf("%d",&arr[i]); i=3; printf("Enter the %drd number:\n",i); scanf("%d",&arr[i]); for(i=4;i<9;i++) { printf("Enter the %dth number:\n",i); scanf("%d",&arr[i]); } return 0; }
Last edited by nerio; 12-23-2015 at 05:22 AM.
Thank You. Your code works good.
I missed the ampersand symbol in the scanf statement.
Thank You.
in case of i has a garbage value, you alrealdy initialized it to 0 in for loop...so there won't be a problem...
Last edited by infinite_loop; 12-24-2015 at 01:17 AM.
Although you certainly can make it linguistically correct.
Personally, I'd just save the world some computing cycles...Code:#include <stdio.h> const char *GetOrdinalSuffix(long n) { n %= 100; if (n < 10 || n > 20) { n %= 10; if (n == 1) return "st"; else if (n == 2) return "nd"; else if (n == 3) return "rd"; } return "th"; } int main() { int i; for (i = 1; i <= 105; ++i) printf("%ld%s loop\n", i, GetOrdinalSuffix(i)); return 0; } 1st loop 2nd loop 3rd loop 4th loop 5th loop 6th loop 7th loop 8th loop 9th loop 10th loop 11th loop 12th loop 13th loop 14th loop 15th loop 16th loop 17th loop 18th loop 19th loop 20th loop 21st loop 22nd loop 23rd loop 24th loop ...