noob.please help

This is a discussion on noob.please help within the C Programming forums, part of the General Programming Boards category; i want to make my first program in C.A program that will calculate two numbers , and show me the ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21

    noob.please help

    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.

    Code:
    #include <stdio.h>
    main()
    {
    int a,b;
    float c;
    a=10;
    b=3;
    c=a+b;
    printf ("c=%d");
    }
    but the result is c=-1079924084

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by sk8harddiefast View Post
    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.

    Code:
    #include <stdio.h>
    main()
    {
    int a,b;
    float c;
    a=10;
    b=3;
    c=a+b;
    printf ("c=%d");
    }
    but the result is c=-1079924084
    almost correct

    your printf statement is wrong.("%f", c);
    Last edited by strickyc; 05-17-2009 at 06:04 PM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    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 05:40 PM.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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);

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    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 be
    Code:
    ("%.2f", c);
    and just change the (2) for how many ever floating points you'd like to have displayed.
    Last edited by strickyc; 05-17-2009 at 06:03 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That's what a "floating point" number is.

    You can control the zero in printf() with:
    Code:
    printf("%.2f",number);
    Now it will look like 13.00
    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

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    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 06:04 PM.

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    ok.i wrote printf ("%.0f",c); and now i have result 13!!!!thanks

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by sk8harddiefast View Post
    ok.i wrote printf ("%.0f",c); and now i have result 13!!!!thanks
    Congratz, next lesson is? I hope use of proper data-types.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Quote Originally Posted by sk8harddiefast View Post
    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?
    Because the default is to print 6 digits after the decimal point if the precision is missing or let the program decide for itself by using the %g specification.

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sk8harddiefast View Post
    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

  13. #13
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    so i could tell int c; ?

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by sk8harddiefast View Post
    so i could tell int c; ?

    Afcourse you can.......Moreover your adding two int's the result can never be a decimal(float)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21