Unexpected Output- long double [duplicate] - c

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.

Related

Couldn't print a double long variable 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 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

Very simple multi array c program - problem noob [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?
You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.

Wrong output of program [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ float t=1/2;
printf("\n%.4f",t);
return 0;
}
i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not.
cant seem to find the problem.
please help.
Thanks.
I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.
TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

Confused with GCC output [duplicate]

This question already has answers here:
Uninitialized variable in C [duplicate]
(6 answers)
Closed 7 years ago.
Recently I have installed Ubuntu and obviously I am compiling my C code in gcc. I came across the following code :
#include <stdio.h>
main()
{
int i = 10,j = 20, k;
printf("i=%d j=%d k=%d\n", i, j, k);
}
The output is coming as ::
i=10 j=20 k=0
But as far as I know that the output for the value of k should be a Garbage value since it has not been initialized.
Is there something that I am missing here?
You cannot expect anything from the program you posted, 0 is a perfectly possible garbage value.
Also, main() must have a return value and it must be int.
It's undefined behavior, so anything is possible, including 0 to be printed.
How is 0 not a garbage value?

How to succesfully pass a 2D array to a function? [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 7 years ago.
GNU GCC compiler
Here is a function: int sumsintriangle(int *a,int n)
where a is a n*n matrix .
for some purpose I added
if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))
condition to my code which was working properly ;as the condition is true for the correct values.
but for the same code when I added
sum=sum + *(a+(i+1)*n+(j+1));
then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why??
Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?
Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?
If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly.
example:
int a[]; //declare an array
a[n] // will give you the element in position 9 of the array.
*a // will give you the first element, cause an array can be treated as a pointer (indeed it is).
I hope this answer help you. If not please tell me. Good luck!
Use This code code may be its work.
*(a+(i+1))*n+(j+1)

Resources