Basic questions about C

This is a discussion on Basic questions about C within the C Programming forums, part of the General Programming Boards category; Hi I was going threw a book learning about C language and I had a few questions if someone could ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    13

    Question Basic questions about C

    Hi

    I was going threw a book learning about C language and I had a few questions if someone could help me out to clarify a few things.

    Float or double….why is one better than the other? From what I understand the double is more precise than the float. If that is so why use the float?

    What is the difference between getc and getchar? Aren’t they basically the same thing?

    What is the difference between putc and putchar? The only difference I see is that putc requires that you use the variable name and stdout “ putc(variable_name, stdout) “ and putchar only requires the variable name “ putchar( variable_name) “ . Is that the only difference? If so then what is the purpose of “putc”?


    Precision specifier question. When I run the program the integer 17 will print out 017 which I understand, and the 11.7 will print out 11.700, and I understand the .700, but I don’t quit understand the purpose of the -3. what is its purpose. Isn’t the “-“ simply a left specification? I would have thought that 11.7 would have appeared as 011.700. What is the purpose of the number 3 after the negative sign in the second print statement?
    printf("%.3d\n", 17);
    printf("%-3.3f\n", 11.7);

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by FlatLost
    Hi

    I was going threw a book learning about C language and I had a few questions if someone could help me out to clarify a few things.

    Float or double….why is one better than the other? From what I understand the double is more precise than the float. If that is so why use the float?
    If you can do with less precision then why to go for double....un-necessary wastage of memory
    Quote Originally Posted by FlatLost
    What is the difference between getc and getchar? Aren’t they basically the same thing?
    You can specify the stream from which you can take the input in getc whereas getchar gets a character from stdin (default)

    Syntax of getc
    Code:
    int getc( FILE *stream );
    Syntax of getchar
    Code:
    int getchar( void );
    Quote Originally Posted by FlatLost
    What is the difference between putc and putchar? The only difference I see is that putc requires that you use the variable name and stdout “ putc(variable_name, stdout) “ and putchar only requires the variable name “ putchar( variable_name) “ . Is that the only difference? If so then what is the purpose of “putc”?
    Same as above but instead of reading it writes

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by FlatLost
    What is the purpose of the number 3 after the negative sign in the second print statement?
    printf("%.3d\n", 17);
    printf("%-3.3f\n", 11.7);
    It tells the number of decimal points in which you want your answer...that is why its coming out to be .700. if you would not have mentioned .3 then the output would have been 11.700000(default precision of float 6 decimal places)
    Last edited by sunnypalsingh; 11-01-2005 at 06:45 AM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    13

    follow-up

    It tells the number of decimal points in which you want your answer...that is why its coming out to be .700. if you would not have mentioned .3 then the output would have been 11.700000(default precision of float 6 decimal places)

    Thanks for the help sunnypalsingh.

    I understand what you are saying about the .3 and that the .3 is the number of decimal points in which the answer will come out to, but what about that first 3 behind the '-' sign. what is its purpose?

    printf("%-3.3f\n", 11.7);

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    run this little program and you will easily see what the '-' symbol does. It left-justifies the number in the field.
    Code:
    int main(int argc, char* argv[])
    {
    
    printf("%-15.3fHello World\n", 1.72132);	
    printf("%15.3fHello World\n", 1.72132);	
    	
    	return 0;
    }

  6. #6
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by FlatLost
    I understand what you are saying about the .3 and that the .3 is the number of decimal points in which the answer will come out to, but what about that first 3 behind the '-' sign. what is its purpose?
    printf("%-3.3f\n", 11.7);
    '-' sign signifies that the output will be left justified.....by default it is right justified..

    %w.p type specifier

    where w is an integer number that specifies the total number of columns for the output value(including the decimal point .) and p is another integer number that specifies the number of digits to the right of the decimal point(of real number)

    next time use tags

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    13
    Thanks Ancient Dragon that little code sample really helped make things clear. Again thanks to sunnypalsingh as well for clearing things up for me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Questions
    By Panda_ in forum C Programming
    Replies: 15
    Last Post: 09-23-2009, 04:24 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Some Basic questions
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 06-30-2009, 09:51 AM
  4. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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