scanf_s not returning any value - c

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();
}

Related

Function not giving normal output

I am trying to compile my practice on functions. I tried many times to compile this and I bumped into this issue.
Here below is my code:
#include <stdio.h>
int displayFlow();
int main(){
int displayFlow();
}
int displayFlow(){
int try1,try2,try3;
printf("enter any given number...\n");
scanf("%d", &try1);
printf("if u entered above 10, ur out\n...please enter another guess...\n");
scanf("%d", &try2);
printf("if u entered below 5 then u won! !");
}
and this is the output I got:
function.c:22:5: note: declared here
int test(){
You declared your function again inside main. Remove int from infront of displayFlow.
#include <stdio.h>
int displayFlow();
int main(){
displayFlow();
}
int displayFlow(){
int try1,try2,try3;
printf("enter any given number...\n");
scanf("%d", &try1);
printf("if u entered above 10, ur out\n...please enter another guess...\n");
scanf("%d", &try2);
printf("if u entered below 5 then u won! !");
}
I come up with another mistake:you define the function displayFlow as 'int' type,but you don't write 'return' in the function when you realize it.
#include <stdio.h>
int displayFlow();
int main(){
displayFlow(); //you just abandon the return value?
}
int displayFlow(){
int try1,try2,try3;
printf("enter any given number...\n");
scanf("%d", &try1);
printf("if u entered above 10, ur out\n...please enter another guess...\n");
scanf("%d", &try2);
printf("if u entered below 5 then u won! !");
//Here is something absent. What do you want to return ? or you just want to return nothing? if so, you should modify the function return value type to 'void',please.
//return ??;
}
Just set return in method main, because he has to return int
int main() {
return displayFlow();
}

console input C

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

Blank output window in dev c++

Why is there blank output window?
#include <stdio.h>
main()
{
int x, y, sum, product;
scanf("%d", &x);
scanf("%d", &y);
sum = x + y;
product = x * y;
printf("%d\n", sum);
printf("%d\n", product);
}
I understand your problem what actually happens is that you enter 2 numbers and the result doesn't get displayed right?
So what actually happens is that your result does get displayed but for a split second (remember the output window will close as soon as it finishes executing the code)
You could try this
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,sum,product;
scanf("%d",&x);
scanf("%d",&y);
sum=x+y;
product=x*y;
printf("%d\n",sum);
printf("%d\n",product);
getch();
}
see we are using getch(); what this function do is actually waits for a user to enter a key so the program hasn't ended unless you press a key, hence you are able to see your output.

C: Extra output in printf

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;
}

Unable to find any valid shell. Required for execution in an external terminal

so I'm currently running my C program in NetBeans IDE 8.1, but as soon as I tried using the scanf function, I began running into issues. I have MinGW download and have added C:\MinGW\bin; to my path environment variable. I looked up and found that I should be running external terminal to use scanf but I am receiving this error. Does anyone have any idea how to fix this. I'm pretty new to C and this IDE so simpler instructions would be appreciated.
Here is the code:
#include <stdio.h>
int main()
{
int int1, sum, int2;
printf("Enter\n");
scanf("%d", int1);
printf("Enter\n");
scanf("%d", int2);
sum = int1 + int2;
printf("sum is %d", sum);
return 0;
}
You need to pass an int *, not an int into scanf. This is because scanf must fill in each argument in the variable argument list. Your code should be
int main()
{
int a, b;
printf("Enter first number\n");
scanf("%d", &a);
printf("Enter second number\n");
scanf("%d", &b);
printf("sum is %d\n", a + b);
return 0;
}

Resources