0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int list[]; /*creating an arry call list*/
int main()
{
    srand(time(NULL));
    void element()/*creating the first element function*/
    {
        int i; 
        for (i=0; i<100; i++){
            list[i] = 100+rand()%900  ;
        }

        for (i=0; i<100; i++){
            printf("%d", list[i]); 
        }
    }
    return 0;
}

I don't know why does it give me nothing as result, the purpose of the code is to generate 100 random numbers and puts the number into the array.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • That's not valid C code. C doesn't have nested functions. – Some programmer dude Jun 24 '17 at 23:57
  • Also, remember that arrays in C have a *fixed size*. Creating an empty array is not valid. – Some programmer dude Jun 24 '17 at 23:58
  • It shouldn't give nothing. It should throw a score of errors and the kitchen sink at you during compilation. – cs95 Jun 24 '17 at 23:59
  • 1
    For starters I would recommend [finding a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Then read your way up to and including the chapters about pointers, arrays and *dynamic memory allocations*. – Some programmer dude Jun 25 '17 at 00:01
  • the posted code contains a second function contained within the `main()` function. That is not valid code. (although `gcc` does have an 'extension') It would be much better (and massively more portable) to declare a second function and have the `main()` function call that second function at the appropriate time. – user3629249 Jun 26 '17 at 03:33
  • this: `int list[]; /*creating an arry call list*/` is not a valid way to declare an array. Suggest: `int list[1]; /*creating an arry call list*/` – user3629249 Jun 26 '17 at 03:35
  • even with the incorporation of my prior comment about the declaring of 'list[]', accessing anything beyond the first element is undefined behavior and can lead to a seg fault event. Given the rest of the code, strongly suggest using: `int list[100];` – user3629249 Jun 26 '17 at 03:37

2 Answers2

0

You need to make a couple of changes:

1)You can do it everything inside the main function unless you need to move it.

2)Remember you need to set the array size (in this case 100).

3)If you use a function you need to declare the prototype before the main function.

4)You can use the same for loop once to iterate and print the numbers.

  #include <stdio.h>
  #include <stdlib.h>
  #include <time.h>

  int main()
 { 
     int list[100]; /*creating an arry call list*/
     srand(time(NULL));

   int i; 
  for (i=0; i<100; i++){
  list[i] = 100+rand()%900  ;
    printf("%d \n", list[i]); 
}

}
Giorgio
  • 148
  • 4
  • 12
0

If you want a function to be executed you must invoke that function. Without making an explicit call a function cannot be executed.

You must also provide the array size in your code before compilation.

Alternatively you can also choose to use the variable length array feature in C which allows you to specify the array size at run time instead of forcibly specifying before compilation.

Krishh
  • 31
  • 1
  • 4