When I try to use scanf
int main() {
int x1,x2,x3,y1,y2,y3;
printf("Enter 3 pairs of positive integers separated by spaces:\n");
scanf("%u %u %u %u %u %u", &x1, &y1, &x2, &y2, &x3, &y3);
I get the program running, like for an input.
Then I put the input, but it prints the "enter 3 pairs..." and does nothing
why is that?
%u is unsigned integer. %d or %i is signed integers. Please take care of these quirks and gotchas in C. Be careful to oblige to correct format specifiers.
Maybe you have to enter values; it is the goal of scanf.
By the way, your program contains an undefined behavior : %u mismatchs with int pointers. Use rather %d/%i format in printf. An other solution is to declare your variables as unsigned int type, to match with the printf format. Moreover, a part of your source code is missing.
You might find it more convenient to use fgets instead of scanf. The eclipse terminal is a little funky.
Refer to this post
Related
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
I apologies in advance if there is a stupid error in this code, but I can't seem to trouble shoot it. My problem is this, I compile with GCC-8 (installed on Mac via home-brew), then execute in the terminal. When using int do define variables s & a, I get zeros as output using the print statements below. If I declare s & a variables as double I still get zeros for the first two print statements, and 1024 for the last print statement. I'm just lost as to what is going on. Appreciate any help!
/* square code */
#include <stdio.h>
int main() {
int s, a;
printf("enter the length of your square \n");
scanf("%f", &s);
a= s * s;
printf("the area of your square is %f cm using f placeholder \n", a);
printf("the area of your square is %lf cm uning fl placeholder\n", a);
printf("the area of your square is %d cm using d placeholder \n", a);
return(0);
}
int s;
scanf("%f", &s);
If s is an int then you need to scan it in like an int using "%d" instead of "%f". What you are doing is undefined behavior, both in scanf() and printf(). An integer needs to be scanned and printed as an integer.
I think the misunderstanding is that you treat the conversion specifiers %d and %f as defining only the output formatting, regardless of the type of value passed.
Actually each conversion specifier is tightly coupled with a particular type of argument it will accept; For example, %d is defined for integral types only, and all other types like floating point or string used together with %d will yield undefined behaviour (cf, for example, cpp-reference of printf for a more detailed explanation).
So double f=1.2;printf("%d",f) will not format a floating point value 1.2 as integral (someone might expect 1 being the output); It rather yields undefined behaviour, having any output (even no output), a crash, a... whatever.
Same applies to scanf-format specifiers, of course.
So if data type is int, use %d; if data type is double, use %f.
In your code you use s as an int, and when you use scanf you are using %f instead of %d. That's the reason why you are getting the first two outputs as a zero.
As for the third output, you will get the expected value, because in that case you are correctly using %d. See the code below:
int s;
printf("enter the value");
scanf("%d", &s);
printf("the area of square is=", s*s);
Hey guys thanks so much for the help. Before checking back this morning I tried one more thing, and alas it was a stupid error.
When declaring "double s,a", I realized my scanf() function was using scanf("%f",s) as opposed to the correct scanf("%lf", s)".
Also thanks for the help for use of %d when using "int s,a".
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)