开发者

Loop Limit in C/C++

Is there a limit to how many time开发者_如何转开发s a loop can run in C?


void main()
{
    int T,N,x,X,i;
    x=0;
    scanf("%d",&T);
    while(T>0)
    {
        T--;
        scanf("%d",&N);
        X=0;
        while(N>0)
        {
            N--;
            scanf("%d",&x);
            if(x>X){X=x;}
        }
        printf("Case: %d",x);
    }
}

T has a range of 0-250, and N has a range of 0-1000. x has a range of 0-10,000. Whenever the N exceeds something above 800, my console stops taking input. Can this be due to a limit on the input buffer?


There is no limit to how many times a loop may loop. There are limits to the max and minimum values of an int, and those may play into your loop. In this case, 800 should be fine, so there is something else going on here.

Edit: Works for me... the only weird thing I see is that you reset X inside the innermost loop, so the output is always the last integer entered if it's >0, or 0.


Are you piping input to this program? There may be some buffering limitation with that. Try putting the data in a file and read from the file.


It's common practice to loop 10,000 times or (much) more to compare the performance of two small calculations.

If there was a limit, there wouldn't be such as thing as an infinite loop. ;)


Because the comments have gone irreverent on you, I will make this a community wiki and commence with an interrogative-like suggestion that belongs in comments:

Check the return values from all scanf calls. Do this first to determine whether the standard library API is already transmitting information to you -- via "electrostatic transmission", otherwise known as the contents of register eax after you call scanf on x86 architecture. Do not let the light in that register die unobserved. Take the register's electrical charges (bits) into a variable and compare them to both zero (0) and EOF. Those alien transmission were sent to you from the year 1976, when scanf was first written to return an informative numerical value to the caller.

As tomlogic pointed out in comments to an answer, if you are pasting the data, you should instead try using the technique known as "input redirection" or "piping." First, get your data into a file, let's say name filename.dat. Then, issue a command such as the following:

executable-name < filename.dat

Where executable-name is the file you are generating with the C compiler. Technically, the above syntax creates an "input redirection" or "stdin redirection" -- the shell opens the file for read access as file descriptor zero (0), also know as stdin. The child program as spawned from the shell will scanf from the file, rather than the terminal (your paste buffer).

Another approach is to create a "pipe indirection" in which the shell opens another process's output for reading and passes this to the child, again as stdin file descriptor. In this case, the shell probably uses popen rather than open. The syntax for this might be:

cat filename.dat | executable-name as if on a Unix-clone, or

type filename.dat | executable-name if in the context of an IBM® PC-DOS® clone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜