Antoni4040
Antoni4040

Reputation: 2319

Why is this C for-loop not working properly?

int main()
{
    int t, i;
    int *nums;
    scanf("%d", &t);
    nums = malloc(t * sizeof(int));
    for(i = 0; i < t; i++)
    {
        scanf("%d", &nums[i]);          
    }
    printf("hey");
}

For some reason, it hangs, waiting for more input! Any ideas?

Upvotes: 3

Views: 8652

Answers (3)

chux
chux

Reputation: 153338

Remember to flush when you are done.

In chat, OP's comments "No... BTW, if I add a printf in the loop that prints the current input, it prints everything except from the last one...". This hinted that the issue was on the final output.

The following sends data out to be printed. But stdout is typically buffered. Actual output may not occur until sometime later. Output is flushed when 1) output contains an end-of-line '\n', 2) fflush() is called 3) the program ends. I am surprised output did not appear when the program ended, but possibly OP's true code differs from the post.

printf("hey");

Change to

printf("hey\n");

Or @Cool Guy

printf("hey");
fflush(stdout);

BTW: This is also hinted in @Diversity answer.


Note: Always a good idea to check the result of scanf()

// scanf("%d", &t)
if (1 != scanf("%d", &t)) Handle_BadInput_or_EOF();

Upvotes: 1

LawfulEvil
LawfulEvil

Reputation: 2337

Just a guess here...

But I'm guessing you ran it and entered :

4
1234

For example, you put in 4 and then 1234 and it hangs. Well, that would be because 1234 is the first number and not 4 distinct numbers so its waiting for the second number. You have to press enter or some such delimiter between each number.

Try this set of inputs instead:

4
1234
29
4
5

You should get :

hey

Pro grammatically, you should be checking return values from your function calls. Make sure malloc didn't return zero. Make sure scanf returns the number of inputs you expected to read. Add in printouts to make sure the values it read in are what you expected/wanted to read in.

EDIT : Guessing you have a typo in the program which isn't displayed here. such as :

scanf("%s", &t);

Or you are getting the 'hey' and just not seeing it.

[ov@MARVIN sotest]$ ./a.out
5 5 4 3 23 1
hey[ov@MARVIN sotest]$ 

See the 'hey' is sort of hidden in my prompt because you are missing the '\n' new line in the printout?

Upvotes: 1

Tarantula
Tarantula

Reputation: 19843

This code is correct, except for the fact that you're not freeing your memory (not a big issue for this simple code) and you're not checking scanf for errors, which may be the cause of your problem.

A better implementation with error checking and correct memory handling is described below:

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

int main()
{
    int t, i, ret;
    int *nums;

    ret = scanf("%d", &t);          
    if(ret != 1)
    {
        /* something wrong */
    }

    nums = malloc(t * sizeof(int));
    if(nums==NULL)
    {
        /* memory error */
    }

    for(i = 0; i < t; i++)
    {
        ret = scanf("%d", &nums[i]);          
        if(ret != 1)
        {
            /* something wrong */
        }

    }
    free(nums);
    printf("hey");

    return 0;
}

Upvotes: 1

Related Questions