I am a beginner in C and would like to know what's the problem about my code here :
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
My problem is that I have to type a value before having any consol output, for example if I type 7, I get this console output : Please input an integer value: You entered: 7
I tried the exact same code in another computer and it worked pretty well, I guess it's a buffer problem ? but I have no idea how to fix it.. Any ideas please ?
As other already mentioned, to guarantee that that line is going to be printed at that point in your code you can flush the standard output like this,
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
fflush(stdout);
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
you can read this for more details, Why does printf not flush after the call unless a newline is in the format string?
updated thanks to #Osiris comments
Related
I am trying to execute following program in eclipse
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a, base, power;
printf("\nBase:");
fflush(stdout);
scanf("%d", &base);
fflush(stdin);
printf("\nExponent:");
fflush(stdout);
scanf("%d", &power);
fflush(stdin);
a = pow(base, power);
printf("\nAnswer: %d", a);
fflush(stdout);
return 0;
getch();
}
the output to the above program should be as follows
Base :
Exponent :
Answer:
but I am getting different output
Base: 2
Exponent: 2
Enter base value: 2
Enter exponent value: 2
The exponent value of 2 is 4
Answer: 0
as you can see the output is asking me to enter the base and exponent values two times where's it should ask only once.
How to disable this?
A couple of things you should fix in your code:
Remove the getch(); line after the return. It won't be executed and you should be getting a compiler warning about this line. Now you can also remove #include<conio.h> as you don't need it.
You fflush() stdin or stdout (as pointed out in the comments).
The resulting code runs just fine and produces the expected output. You can check it out here: https://onlinegdb.com/S1LLp9GoU
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int
main ()
{
int a, base, power;
printf ("\nBase:");
scanf ("%d", &base);
printf ("\nExponent:");
scanf ("%d", &power);
a = pow (base, power);
printf ("\nAnswer: %d", a);
return 0;
}
I am running the program below, and the only output I get is the question "whats your number?".
If I then enter any number, e.g. 3, then nothing further happens.
Can anyone help?
I am using VS2017.
Thank you in advance.
#include <stdio.h>
int main()
{
int x = 0;
printf("whats your number?\n");
scanf_s(" %d", &x);
printf("%d\n",x);
getchar();
}
I've just started to learn C programming last week and I've learnt about some basics about it. So now I'm trying to make a program which can add up two numbers and show the result.
Here's my code:
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:%d\n");
scanf ("%d",&a);
printf ("Insert the next number:%d\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
It can be compiled and run but the following result is shown.
[1]http://i.stack.imgur.com/4Xjdv.png
Can someone please help me to get rid of that 4200612, which is output at the first printf statement? Thanks for your help and sorry for my bad english.
There is no need of %d in first two printf statement.
printf("Insert a number: ");
scanf ("%d",&a);
printf ("Insert the next number: ");
scanf ("%d",&b);
Since there is no corresponding argument. It will print some random value.
Try getting rid of the extra %d's in your printfs.
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:\n");
scanf ("%d",&a);
printf ("Insert the next number:\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
as expected this prog. should accept a number until it encounters a 4 but it gives some garbage value. why?
int main(void)
{
int a;
printf("Enter a number: ");
scanf("%[^4]d", &a);
printf("You entered: %d\n", a);
return 0;
}
As far as I know scansets are meant to be used with strings (which makes the d not act as an integer placeholder specification). One way to write it is to read the input into a string and then parse it:
int main(void)
{
int a;
char b[255];
printf("Enter a number: ");
scanf("%254[^4]", &b);
a = atoi(b);
printf("You entered: %d\n", a);
return 0;
}
I'm keeping the modified code to a minimum, you'd definitely need some extra checks for input sanity.
To clarify: The 254 prefix limits the amount of data that scanf will capture, so as to not exceed the size of the buffer (strings are terminated with an extra null character, so the read length must be smaller than the actual size)1.
The scanset working with only characters.
Here is my sample code. (but, I don't know what you really want.)
#include <stdio.h>
int main(void) {
char buffer[128];
printf("Enter a number: ");
scanf("%[^4]s", buffer);
printf("You entered: %s\n", buffer);
return 0;
}
The result is,
Enter a number: 12345678
You entered: 123
Additionally, if you want integer value, use atoi().
So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d