C - fprintf and printf inside loops don t print to screen - c

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.

Related

Why continue statement not execute the below lines of codes in infinite loop

#include<stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
{
continue;
}
printf("%d",++i);
}
}
This above code makes the output as without print anything.But by my logic it prints the value from 1 to infinite times except 10.The continue statement doesn't run the below code after continue statement but the continue statement is inside the if condition then why it shouldn't run the statement that are below the continue statement?
The output of C-programs is normally buffered, which is likely the reason why you don't see anything. Try printf("%d\n", ++i) as the linefeed will flush the buffer.
Once i reaches 10, the number will not be increased any more, so your expectation that "it prints the value from 1 to infinite times except 10" will not be met.

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.

Break statement not within loop or switch in C

I am getting this error in my C code. I don't know what I am doing wrong. If I comment this code my program works. This piece of code is inside int main().
if(argc!=2 && strcmp(argv[0],"selection-sort")==0 && strcmp(argv[1],"input.txt")==0 && strcmp(argv[2],"output.txt")==0)
{
printf("The command line arguments are correct.\n");
}
else
{
printf("The command line arguments are wrong.I am exiting.\n");
break;
}
The way it looks I think you're not in a loop but just checking args in main. You probably want something like return 1 or exit(1) instead of the break.
First of all make sure you are including the needed header files:
#include <stdio.h>
#include <stdlib.h>
The break command is used for exiting loops, you are not in a loop you are just in an else statement, you have nothing to break from. The code flow executes normally after passing that else statement. If you want to exit the program in that else statement you could do something like this:
else
{
printf("The command line arguments are wrong.I am exiting.\n");
return 1; //exit program with status 1 to indicate a non normal exit
}
Or if you want to continue the program after displaying that message you could just do this:
else printf("The command line arguments are wrong.I am exiting.\n");
//more code here
You only use break in loops like so:
while(foo) //while foo is true
{
break; //exit the loop
}
The error message in the title says it all: break can only be used to exit a loop or prevent a case from falling through. MSDN quote:
The break statement terminates the execution of the nearest enclosing
do, for, switch, or while statement in which it appears.
To leave a function use return.
Break is supposed to be used in loops.
Use a return statement, which causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called (return address).
The other answers are correct, this is just a slight addition.
To return probably in this specific case you should include errno.h like this:
#include <errno.h>
And furthermore return like this:
return EINVAL;
Then you are signaling that the program is terminating due to an error and the return value specifically states that the error is invalid arguments.
'break' will only get you out of the innermost loop or switch. You can use 'return' to exit out of a function at any time.
"A break statement may appear only in an iteration statement or a switch statement, and terminates execution of the smallest enclosing such statement".
And it makes sense too - you can "escape" from a method with "return" , and you can skip code in other situations with an if/else. I don't know what a "break" outside a case would be expected to do.
'break' is really only a restricted form of 'goto' anyway. Ideally, you want a single point of exit from any block of code. You should really only use 'break' in a switch statement because that is the only way to make it work. In any other context, there are better ways to accomplish the same thing. The same applies to 'continue'.

This program shows segmentation fault,I think infinite recursion occurs.Can you suggest me the correction

#include <stdio.h>
int f(int n)
{
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f((n-1)/2) + f((n-1)/2+1);
}
/*To test above function */
int main()
{
int a = 11;
printf("%d", f(11));
getchar();
return 0;
}
Why not just to trace it in debugger, and see what's goind wrong?
This code compiles and runs just fine for me. The result is 5. Have you tried the explicit keyword to see if it is an optimization flaw? EDIT: If the problem is getchar() the problem could be your operating system at run time or your library at compile time. Is that a clean compile? What compiler are you using?
There is nothing wrong with this program, it doesn't give me any sort of infinite loop or seg fault (and since you have the <=1 condition, the program will not crash out for sure).
Something tells me that your machine here is faulty for some reason... maybe lack of memory or confilcting programs? Or even compiler freaking out? (that one already happened to me). Try to run the code on other machine if possible and find out if it still gives you seg fault or not

Resources