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.
Related
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.
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.
I'm a novice programmer getting introduced to C and I'm missing something fundamental about the way my scanf() works. I want to read a single int from the keyboard with code like this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int userBookSelection;
scanf("%i", &userBookSelection);
printf("Printing userBookSelection: %i", userBookSelection);
return EXIT_SUCCESS;
}
When I run the code, the console stays black until I stop debugging. There is never a cursor waiting for keyboard input. When I stop debug I can see this output in the console, same every time:
Printing userBookSelection: 2130567168
I'm debugging in Eclipse with MinGW GCC compiler on Windows. The code syntax seems to be correct -- is it possible there's something wrong in my build path to make this happen? I need to know why scanf() isn't reading for keyboard input.
So I've gotten a line of code from my professor which takes care of this bug -- whether it's a necessary solution particular to Eclipse and/or MinGW I'm not sure. In any case, here's the code with the additional line:
int main(void) {
int userBookSelection;
setvbuf (stdout, NULL, _IONBF, 0);//<---The magic line
scanf("%i", &userBookSelection);
printf("Printing userBookSelection: %i", userBookSelection);
return EXIT_SUCCESS;
}
I'd appreciate any additional wisdom on what's going on, what setvbuf() is doing and how scanf() works more fundamentally.
I just discovered that the function fprintf can be used to print something to the screen.
I have this minimal just below, however it doesn t output anything to the screen. Why?
#include <stdio.h>
int main(void)
{
int i,j,k;
for(i=0;i<4;i++)
{
for(j=0;j<0;j++)
{
for(k=0;k<3;k++)
{
printf("test\n");
fprintf(stderr, "test\n");
}
}
}
return 0;
}
I am running ubuntu 14.04 and compiling this code as follows:
gcc main.c -o main
Why should it print anything? One of your loops has an impossible condition:
for(j=0;j<0;j++)
^---
since j starts at 0, it can never be LESS than 0, so the loop immediately exits without ever executing the body.
Your second loop is wrong. j is initialized to zero and the conditional is j<0. With for loops the conditional is evaluated before the first iteration.
I have this minimal just below, however it doesn t output anything to the screen. Why?
It wouldn't because control never reaches to the printf() statement. Because your middle for loop has a test condition which always fails which prevents even inner for loop to execute.
for(j=0;j<0;j++)
.
.
The condition j<0 when j is initialized with 0 will always evaluates to false. Fix the middle for loop and your problem will be solved.
You can debug your program using gdb. Learn few commands to work with gdb and you can see on your own where is the problem lies.
I use ubuntu 14.04 version system for below code.
I use below code : (below code is infinite Loop)
#include <stdio.h>
#include <unistd.h>
int flag=0;
int main(void){
printf("program start.\n");
printf("PID=%d\n",getpid());
printf("flag=%d\n",flag);
//-----------I feel weired below do...while... sentences----------//
do{
printf("loop_");
sleep(1);
}while(flag==0);
printf("program exit.\n");
return 0;
}
in the beginning below print result :
root#ubuntu:~/Desktop/my_test_code# ./issue
program start.
PID=3113
flag=0
...........//start waiting here,and don't print "loop_"
then after I waited for long time this program prints a lot of "loop_".
I find it very strange, and should print a string "loop_" and then, wait a second, then print a "loop_" again, and so on, why me waiting for a long time, do it start to print a lot of "loop_" ?
anybody has any idea for my issue . thank you in advance.
printf bufferizes its output (until a given size or \n), so you don't see the output until it flushes it.
changing your loop to
printf("loop_");
fflush(stdout);
sleep(1);
should solve your issue.
Root cause:
Usually stdout's buffer are getting flushed by \n so here loop goes on and stdout did not get chance to buffered its data properly.
Solution:
There are three way here
1) Use \n at the end of printf
printf("loop_\n");
2) call fflush(stdout): after printf in loop.
3) Disabled the buffering for stdout stream using below call in your program
setvbuf(stdout, NULL, _IONBF, 0);