i want to make my first program in C.A program that will calculate two numbers , and show me the result.
this is my code.
but the result is c=-1079924084Code:#include <stdio.h> main() { int a,b; float c; a=10; b=3; c=a+b; printf ("c=%d"); }
i want to make my first program in C.A program that will calculate two numbers , and show me the result.
this is my code.
but the result is c=-1079924084Code:#include <stdio.h> main() { int a,b; float c; a=10; b=3; c=a+b; printf ("c=%d"); }
OK "sk8harddiefast" -- the correct format is %f. Floating point numbers are represented very differently in memory.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
i try ("%d", c); but the result was zero and i try ("%f", c); and the result is 13.00000.why with so much zero?
Last edited by sk8harddiefast; 05-17-2009 at 04:40 PM.
Be sure to read documentation on functions when you have a question: printf
For instance, if you wanted to limit the number to be represented with 2 numbers to the left of the decimal point and 3 numbers to the right, you would put the following:
Code:printf("%2.3f", c);
Opps my bad, didn't relieze you used a float for the storing of adding two integers. Cause thats how many 0's are in a floating point number. If you wanted to print just two zeros it would beand just change the (2) for how many ever floating points you'd like to have displayed.Code:("%.2f", c);
Last edited by strickyc; 05-17-2009 at 05:03 PM.
That's what a "floating point" number is.
You can control the zero in printf() with:
Now it will look like 13.00Code:printf("%.2f",number);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Dang, I should like slow down a bit I suppose. Both my replies on this simple matter was flawed.
Last edited by strickyc; 05-17-2009 at 05:04 PM.
ok.i wrote printf ("%.0f",c); and now i have result 13!!!!thanks
When you are adding two integers the result will always will be an integer, so it is not necessary to declare the result(c) as float. In some machines float may take more bytes than int then also it'll not be a better idea(although it's a small program memory management is not an issue).
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
so i could tell int c; ?