-1

I recently started coding in c. I was wondering how you can repeat/loop a task as many times as the user wants (by input).

int a,i;

scanf("%d", a);
for(i=0; i<a; i++){...}

This is the code I came up with, but it doesn't work. It's an infinite loop.

surjit
  • 338
  • 4
  • 15
  • 4
    Read the documentation carefully. `scanf("%d", a);` ---> `scanf("%d", &a);` – StoryTeller - Unslander Monica Jun 20 '17 at 08:21
  • 2
    What is a "structure" that you want repeat? – JeffRSon Jun 20 '17 at 08:21
  • try to print a to check if the user input really got there... if not, check your scanf carefully... – CIsForCookies Jun 20 '17 at 08:24
  • Enable all warnings & debug info in your compiler (so with [GCC](http://gcc.gnu.org/) compile with `gcc -Wall -Wextra -g`....), you should get a warning. – Basile Starynkevitch Jun 20 '17 at 08:29
  • by structure do you mean a code, function or an actual C structure ? – GAURANG VYAS Jun 20 '17 at 08:30
  • 1
    Apart from the `scanf` issue mentioned int previous comments, there may be other problems in the `{...}` part. Please edit the question and show your actual code. – Jabberwocky Jun 20 '17 at 08:30
  • its better to initialize the local variables to zero or some value instead of just define. If you do `int a=0` you won't see any output from `for loop` and second you need to follow `scanf` format properly use `man scanf` in linux system or google – ntshetty Jun 20 '17 at 08:35
  • 1
    @ThiruShetty If `a` is initialized to zero, the call to `scanf` will most likely segfault. – Jabberwocky Jun 20 '17 at 08:37
  • @MichaelWalz i don't know to which compiler you referring, but it works with gcc no segment fault. – ntshetty Jun 20 '17 at 08:42
  • @ThiruShetty [On ideone](http://ideone.com/12uJe5) I get a runtime error. – Jabberwocky Jun 20 '17 at 08:51
  • @MichaelWalz you will get runtime error if you do either of this `int a=0;` or `int a;` `scanf("%d",a);` because `scanf` expects variable reference and not direct address – ntshetty Jun 20 '17 at 08:57
  • @ThiruShetty that's exactly the point. But _`scanf` expects variable reference and not direct address_ is not quite correct: _`scanf` expects the **address** of the variable and not the **value** of the variable_. – Jabberwocky Jun 20 '17 at 09:03
  • @MichaelWalz thanks for correcting me, you are correct, `scanf` doesn't expect value – ntshetty Jun 22 '17 at 04:09

3 Answers3

0

You need a & to scan into.

int a,i;

scanf("%d", &a);
for(i=0; i<a; i++){...}

Read a bit more about scanning integers here.

How to scanf only integer?

N.Y
  • 60
  • 6
0

Change scanf("%d", a) to scanf("%d", &a)

The code didn't work for you because you failed to store the value entered by the user.
Before modifying your code as my suggestion try to print the value of a in your code, you'll get it, why it's not working..

surjit
  • 338
  • 4
  • 15
  • Actually, it is **not** an infinite loop - just one running a *very* long time (assuming i is not modified any more within the loop body...). – Aconcagua Jun 20 '17 at 08:33
  • you can't predict the value of `i` since compiler assigns garbage to it – ntshetty Jun 20 '17 at 08:59
0

The problem is in line:

scanf("%d", a);

You have to use:

scanf("%d", &a);

& sign represents address, you want to store input value on the address of variable a.

Read more about ampersand here: When should I use ampersand with scanf()

Aleksandar Makragić
  • 1,957
  • 17
  • 32