why is printf not called when expected? - c

This is my Code [Note: I am using Eclipse for C/C++ on Windows Platform]
#include <stdio.h>
#include<stdlib.h>
int main(void) {
int num;
printf("Enter a number:\n");
scanf("%d",&num);
if(num%2==0)
printf("Number is Even");
else
printf("Number is Odd");
return EXIT_SUCCESS;
}
Here I have to enter an Integer first only then printf is called... I want to call printf first before I enter an Integer...What am I doing wrong here?
for example this is the output that I get
6
Enter a number:
Number is Even
and expected output is
Enter a number:
6
Number is Even

you can call fflush(stdout) after first printf to print the buffered output. But considering in future if you extend the program with more printfs then adding fflush after every printf will be an overhead. So you can add
setbuf(stdout, NULL)
just before all the printfs.
This will make sure no output is buffered and you will see the prints instantaneously.

Related

My command prompt wont run the whole program

This code runs perfectly in my IDE; I even tried it in an online compiler just to be sure, but when I try to open the .exe it will only ask for the integer and automatically close. I tried it with another program from the school where I asked for like 15 numbers but right before a goodbye message it just closes. Any idea how to fix cmd?
#include <stdio.h>
int main()
{
int numberOfWidgets;
printf("Give me a number: ");
scanf("%d", &numberOfWidgets);
printf("You choose the number: %d", numberOfWidgets);
return 0;
}
You need to add two lines to the end of your program:
int main() {
...
getchar();
getchar();
return 0;
}
The first call to getchar will clear the return key you pressed when you entered the number, the second one will stop and wait for a key to be pressed.
That way, your program will not exit until you press a key, and you will be able to read the output.

Running a simple C program using VS code

I recently started taking a C course and I am having some difficulties running a simple I/O program:
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
scanf("%d\n", age);
printf("Your age is: %d", age);
return 0;
}
the problem is that the program does start running, but it never stops and it doesn't allow me to type in the age and doesn't even print out the "Please enter your age: " sentence.
I have a gcc, g++ and a gdb installed and everything works fine if I remove the "scanf".
I would really appreciate some help!
Thank you!
You need to add an & symbol before the variable name while scanning. Check why here.
scanf("%d\n", &age);
Working Code
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
fflush(stdout); // Prints to screen or whatever your standard out is
scanf("%d", &age);
printf("Your age is: %d\n", age);
fflush(stdout); // Prints to screen or whatever your standard out is
return 0;
}
I run this and it is working as expected. If you are using linux os, please follow below command to compile and run.
gcc age.c
./a.out
Please visit here to know more about scanf function in c.

Problems with scanf and or fgets program cuttoff

I am trying to write a code that reads in a integer using scanf and then asks the user if they would like to enter another by entering "yes" or "no". I get no errors when I compile but when I run the program it asks for the number, displays it as it should, ask to continue and then promptly closes without taking any input.
int main()
{
int *data=malloc(1*sizeof(int));
if(data==NULL)
{
printf("out of memory");
exit(1);
}
char *yc="yes";
char uinput[20];
int numinput;
printf("enter a number \n");
scanf("%d",&numinput);
data[0]=numinput;
printf("you entered %d \n",numinput);
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);
int count=0;
int n,i;
int comp=strncmp(uinput,yc,1);
while(comp==0)
{
n=sizeof(data)/sizeof(data[0]);
count=count+1;
data=(int*)realloc(data,n+1);
printf("enter another number:\n");
scanf("%d",&numinput);
data[n+1]=numinput;
printf("the numbers are:\n");
for(i=0;i<=count;i++)
{
printf("%d\n",data[i]);
}
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);
}
printf("Goodbye!\n");
return(0);
}
This is what happens when I run the program
./vector_play.out
enter a number
6
you entered 6
would you like to enter another # ?Goodbye!
I am not sure why it is immediately exiting any help would be great.
Your problem stems from the fact that after numinput has been read using the code in
scanf("%d",&numinput);
the newline character is still left in the input stream. The call to fgets reads the newline as a complete line and returns without waiting for any further input.
You need to add a line to ignore the rest of the input stream. You can use fgets for that purpose too.
scanf("%d",&numinput);
fgets(uinput,sizeof(uinput),stdin); // Read and ignore
printf("you entered %d \n",numinput);
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);

Wrong output when I run code in Eclipse Indigo CDT

I am using Eclipse Indigo CDT and just running simple code which is:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
scanf("%d",&num);
printf("no is %d\n",num);
}
Opuput:
55
enter no
no is 55
But when I run this code it won't print enter no. Instead of that it waits to enter the number. After pressing some number it is printing enter no. What could be the reason?
That would depend on the flush scheme of standard out.
Traditionally, stdout is line buffered when connected to a terminal and page buffered when it's not connected to a terminal.
Apparantly, when you run your program in eclipse, standard out is page buffered.
You can overcome this by flushing stdout:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
fflush(stdout);
scanf("%d",&num);
printf("no is %d\n",num);
}
It is good practice to flush a file handle whenever you expect the recipient of the information to respond. That way you can be sure that the recipient gets everything you have written regardless of the buffering of the file handle.

Please help in finding the error in my program

I know the program I am posting is very basic but I am unable to find the error. I rechecked everything but despite no compilation error, the output is not coming.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
while(1)
{
if((scanf("%d",&i))!=42)
{
printf("%d",i);
}
else
{
break;
}
}
return 0;
}
Thank You for your time and consideration.
scanf does not return the value entered. It returns the number of inputs matched. So your check for 42 is incorrect. It will return 1 if an integer was assigned to i, or 0 if there was invalid input. If you want to exit the loop when the user enters 42, you should explicitly test if (i == 42).
As simonc mentioned, The reason you're not getting any output is because printf("%d",i); is not printing a newline "\n" after the number, and the standard out (stdout) stream is line-buffered. That means the standard library is going to continue to buffer up your printfs until a newline is encountered. At this point the buffer will be flushed to the console.
The solution is to add the newline (which is probably what you desire anyway): printf("%d\n",i); Alternatively, you could call fflush(stdout) to tell the std library to flush the buffered content to the console immediately. Or, writing to stderr will output immediately, because it is unbuffered.
This should demonstrate:
#include <stdio.h>
int main()
{
int i;
while(1) {
fprintf(stderr, "Enter a number (or 42 to quit): ");
if((scanf("%d",&i)) != 1) {
fprintf(stderr, "Invalid input.\n");
continue;
}
if (i==42)
break;
printf("Number provided: %d\n", i);
}
return 0;
}
Output:
$ ./a.exe
Enter a number (or 42 to quit): 17
Number provided: 17
Enter a number (or 42 to quit): 42
$
You are comparing the scanf function's return value to 42, not the variable i's value.
maybe you should read this
http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf

Resources