How codepad know about infinite loop? - c

I tried the following code:
#include<stdio.h>
main()
{
int i;
for(i=1;i<50;i++){
printf("Hello World");
}
}
and
#include<stdio.h>
main()
{
int i;
while(1){
printf("Hello World");
}
}
codepad shows "Time out". Does it have syntax-checks or does it simply check if my program takes up too much time?

It looks like codepad has limits for resources used by submitted programs, and stops the ones that are beyond them.
Your infinite loop program exceeds the time limit, and is stopped with "Time out" message. So there's nothing related to syntax checking here.

Related

debugg not going inside the loop

I am practicing on chars and i wrote a while loop that ends whenver i see a ";"
however when i am trying to debug it doesnt go inside the loop only at the last loop and I dont understand why
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str[] = "abccc;";
int i = 0;
while (str[i] != ';')
{
i++;
printf("%d \n",i);
}
}
thats the code, it works and realy simple but if I debugg to see it only goes inside the loop in the last time
I am not sure why and how to fix
I would suggest keep tracing even if it looks like its not going inside the loop. Sometimes the way the code is generated, the execution in the debugger is not linear. In fact as you keep tracing, keep a watch for output of the printf from the program.

for loop in c is not incrementing on the final iteration of the loop

Hello and thank all who bothered to read. I am currently trying to execute a program in c that contains a function with this for loop.
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
int i, goal;
char resp[1];
printf("How many people would you like to ask?");
scanf("%d" ,&goal);
for(i = 0; i < goal; i++){
printf("-Person %d: Would you like to register to vote?\n",(i+1));
scanf("%s", resp);
if((strcmp(resp, "n"))==0){
printf("\nOk.\n");
}
else if((strcmp(resp, "y"))==0){
printf("\nRegistering...\n");
}
}
printf("\n%d people asked! Taking a break.\n", goal);
}
Yet for some reason, when I run the program, after pressing n for all of the runs, the for loop does not increment, leading to an infinite loop on the final run.
I have tried changing the for loop to a while loop with the same results and have also tried the y option for the last time which led to a whole host of other issues. In order to get the goal value I am using scanf though when I tried using fgets I still came into the same flawed result or worse. I ran this code into a debugger and when executing the program line by line it works fine, but running it normally seems to be the issue. Any help is appreciated and I am willing to clarify or expand on my code. This is my first post here so apologies if this has been asked 50 trillion times.

How does the infinite loop further down stop earlier code from execution?

I've come across this weird situation i can't wrap my head around. In the following code, the Program is apparantly enetering the infinite loop, but doesn't execute the code that comes before it.
#include <stdio.h>
#include <stdlib.h>
int main() {
char buf[100];
if (scanf("%s", buf)==EOF) return 0;
printf("This does not get printed");
while(1) {} //infinite loop
return 1;
}
Somehow, the printf command does not get executed, even after pressing enter or using Ctrl-d.
However, it seems as if the code would end up in the infinite loop.
Does anyone explain whats going on here? I'm using gcc.
Standard output is line buffered by default. If you write less than a line to the output, some or all of it can be delayed until more output occurs.

Is there some sort of a command for creating a delay in a program

Many people told me there is a better way to create a delay for X amount of milliseconds. People told me about sleep command and usleep(), but I failed to make that work.
Currently I'm using this:
void delay(unsigned int mseconds) {
clock_t goal=mseconds+clock();
while(goal>clock());
}
And by doing this
delay(500);
printf("Hello there");
I can make text appear half a second later, but I want to find a better way to do this since people told me this is a bad method to do it and that it can be not very accurate.
I figured everything out!!! God I was dump when trying to make sleep() command work first time
#include<stdio.h>
#include<windows.h>
int main(){
int load;
int Loadwait=100
for(load=0;load<100;load+=5){
printf("Loading %d",load);
Sleep(Loadwait);
system("cls");
}
return 0;
}
Using musl libc you should be able to use thrd_sleep().
#include <threads.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
printf("Time: %s", ctime(&(time_t){time(NULL)}));
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
printf("Time: %s", ctime(&(time_t){time(NULL)}));
}

why am I getting runtime error?

I know that runtime error occurs when program consumes large memory or divide by 0 somewhere.
this is the code which prints all the numbers until user types 42.(does not print 42).
#include<stdio.h>
int main()
{
int n;
while(1)
{
scanf("%d",&n);
if(n==42)
break;
else
printf("%d",n);
}
}
please tell me why am I getting runtime error in such a simple code?
Your main function should return a int and you're not: that's why you get a Runtime Error. Here is the correct code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 0;
while(1) {
scanf("%d",&n);
if(n == 42)
break;
else
printf("%d",n);
}
return EXIT_SUCCESS;
}
You can also change return EXIT_SUCCESS; in return 0; if you don't want to include stdlib.h but here is why it's better.
You should also consider using another IDE than CodeChef. CodeBlocks or VisualStudio are better and more explicit with errors and warnings. And you should set you int n to 0 before using it.
This works perfectly fine as it is except when running on CodeChef.
According to the C standard (at least C99 and C11) a return 0 is implicit when main() ends without returning something. So even though it can be argued that it is a good idea to always have a return 0 at the end of main(), it is not wrong to skip it.
But that's not the case when running on CodeChef. For some reason they tread it as a run time error if main does not end with return 0.

Resources