Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Discuss a very basic C function

  1. Nov 15, 2009 #1
    http://u1.imgupload.co.uk/1258243200/e9fd_image.png

    today my friend gave me this picture and ask me what is the purpose ,in terms of function, of

    the program shown in the figure. It's really inspired me a lot. The reason is although we always

    use some functions, did we really know what is the purpose of that function?

    so if you were me, how would you answer my friend?
     
  2. jcsd
  3. Nov 15, 2009 #2

    Borek

    User Avatar

    Staff: Mentor

    I even tried to read your post backward, but I still don't understand what you are asking about :uhh:

    :eek:

    Which one? printf? scanf? Technically main() is a function as well...
     
  4. Nov 15, 2009 #3
    i also feels confused too.

    actually my friend's question is " what is the purpose (in terms of function) of

    the program shown in the figure ( picture)."

    i tried to guess what he is asking and my guess is, he is asking about the the purpose of function "char" and "while".
     
  5. Nov 15, 2009 #4

    jtbell

    User Avatar

    Staff: Mentor

    I think you should ask your friend for further clarification of what he/she is asking about.
     
  6. Nov 15, 2009 #5

    Mark44

    Staff: Mentor

    char and while aren't functions. char and while are keywords, with char being a data type, and while being used to control a loop of a certain kind.

    The apparent purpose of the while loop in the code you showed is to ask whether the user would like to run the program again. Presumably the loop is supposed to stop when the user enters a character other than 'y' or 'Y'. However, because of the control string that is used in the call to scanf, the loop probably won't work as expected.

    You could write a short program and test this out for yourself...
     
  7. Nov 16, 2009 #6
    just as you said, i wrote the program above and try to test it.

    PHP:
    #include<stdio.h>
    main()
    {
        char again;
        again='y';
        while (again=='y'||again=='Y')
        {printf("would you like to ?");
        scanf("\n%c", &again);
    }
    }
    but when i enters a character other than 'y' or 'Y', it really stop.
    why?
     
  8. Nov 16, 2009 #7

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    The key is that "while (again== 'y' || again== 'Y')"
    The || is, of course, "or". While "again" is either of those, the program runs the lines inside the "while" block.

    The program checks to see whether the character "again" is either y or Y. If it is, it runs the lines inside the "while", which involves getting another character for "again", then rechecks. If not, if "again" is any character other than y or Y, it exits the block, ending the program.
     
  9. Nov 16, 2009 #8

    Mark44

    Staff: Mentor

    I think this is more subtle than HallsOfIvy's explanation. The key really is in the call to scanf(), with its control string of "\n%c". There are some real problems when you try to input a single character using scanf, namely that you actually enter two characters: the character in question and ENTER. Since you're storing the result in a memory location that can hold only one character, the input buffer still holds the second character you typed, so in loops like the one you have, the next iteration of the loop uses a character that's still left over in the input buffer, which can produce results you might not have expected.
     
  10. Nov 16, 2009 #9

    Mark44

    Staff: Mentor

    There are also problems if you enter yyyyyyy<ENTER>. The while loop will continue looping as long as there are characters in the input buffer, without letting you enter a new choice.
     
  11. Dec 2, 2009 #10

    harborsparrow

    User Avatar
    Gold Member

    I don't think this loop really repeats unless you flush the buffer.
     
  12. Dec 18, 2009 #11
    is the /n really necessary in the scanf? if you are having problems with the iterations after the first time through the loop i'd just try adding a space in the scanf before the %c
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Discuss a very basic C function
  1. C++ function (Replies: 3)

  2. Functions in c (Replies: 1)

  3. C# with C++ functions (Replies: 2)

  4. Basic C++ question (Replies: 3)

Loading...