Couldn't print a double long variable in C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am a beginner, just learning C; I am trying to print a 'double long' variable. When I try to run the program, It produces incorrect output! Here is the code:
#include<stdio.h>
int main()
{
double long x=10;
printf("%.Lf",x);
return 0;
}
/*
Output:
-0
Output in online compiler:
10
*/
I use vs code with gcc. When I try to run the program, it displays wrong value. I tried using an online compiler, It works fine. Here is the online compiler I used:
https://rextester.com/l/c_online_compiler_gcc
Thanks.
edit: I tried using %Lf instead of %.Lf, but still the output is -0.000000

There's a similar problem with MinGW under Windows.
If you run the code with the extension .cpp, it'll show you the correct output.
However, to get the correct output in c , you can use __mingw_printf instead of printf while printing long double values.
Sample code:
#include<stdio.h>
int main()
{
long double x=10;
__mingw_printf("%Lf",x);
return 0;
}
Output:
10.000000
To know more, please check out the following resources:
Printing "long double" type via printf() on MinGW g++ 3.2.3
can't print correctly a long double in C

Related

Unexpected Output- long double [duplicate]

This question already has answers here:
printf and long double
(8 answers)
Closed 1 year ago.
I have written the code as follows
#include <stdio.h>
int main()
{
long double var = 3.1415926535;
printf("%.6Lf", var);
}
The output of the code is 0.000000. According to me output should be 3.141592. Please tell what mistake I am doing.
Try to use GDB compiler, run your code in steps and see where is the problem and your variable changes its value. This is how we learn program dont expect every time to others to solve your problem, this method must be the last. Take a look at this GDB tutorial and fix this problem at your own.

wrong output C after successful attempts [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
So i'm trying to solve this codeforce problem https://codeforces.com/contest/431/problem/A
.Basically i input 4 integers(a[0]...a[3]) and an array of integers between 1 and 4 then i need to output the sum of the string values according to the 4 initial integers.(check the codeforce's exemples)
So my code did work on the 5 initial tests but i had a wrong output on the 6th test
enter image description here
Here's the code
#include <stdio.h>
int main()
{
int test=1;
long s,result=0;
long a[3];
int i,x;
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
if (a[i]==0)
test=0;
}
scanf("%d",&s);
while (s!=0)
{
if (test==0)
break;
x=s%10;
s=s/10;
result=a[x-1]+result;
}
printf("%d",result);
return 0;
}
Your help would be much appreciated.
There have several problems with your code. But the most severe problem for which you are getting wrong output because you are using "%d" format specifier for long values, but its "%ld" actually. Using %ld will solve the problem.
Leaving rest of the problems for you to find out. Happy coding!

Extracting Numbers from String in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have an ASCII string coming from UART that looks something like "43.533a,5532" and I'd like to extract the two numbers. My idea was to separate them using strtok with the comma as delimiter and then remove the last character from the first string and afterwards convert the numbers using atoi() or is there an easier way with sscanf()? String manipulation is nothing I'm regularly using.
Another problem is, if the String looks different, how could I catch that beforehand?
Yes you can do this easily with sscanf().
Following is an example. See it working here:
#include <stdio.h>
int main(void) {
float a;
int b;
char *sNum = "43.533a,5532";
sscanf(sNum, "%fa,%d", &a, &b);
printf("a= %f || b= %d", a,b);
return 0;
}
Output:
a= 43.533001 || b= 5532
Note: Since float is having precision to 6 decimal place by default, so you may need to consider it and correct it if necessary.

Code output explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Variable enters C function as nonzero, turns to zero inside by itself [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm debugging a piece of code and found that when I print out a variable prior to function execution it is nonzero. The same variable is then used as an input argument for a function, but when printing it out right at the beginning of that function it says it's zero.
Outside the function call it looks like this:
printf("sigma_b_m: %f \n",sigma_b_m);
oprq_init(sigma_b_m, b_m, r_m, x, P);
Inside the function call I did the following:
void oprq_init(const float sigma_b_m, const float b[3], const float r[3], float K[16], float P[256]) {`
printf("sigma_b_m: %f \n",sigma_b_m);
}
I removed the rest of the function for clarity but the printf line is really at the very start of the function.
The output is then:
sigma_b_m: 0.001745
sigma_b_m: 0.000000
Any idea how it now registers as zero rather than the 0.001745 it told me it was prior to going into the function?
Ok I realized my own dumb mistake... the header file had a typo in the #ifndef statement... I'm sorry for the trouble guys and my obstinacy.

Resources