Thread: My doubts

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    Red face My doubts

    Hi guys,
    I am learning C and coming up with a lot of doubts. Kindly spend some time to clear my doubts

    Here is my first doubt:

    Code:
    void main()
    {
    int i;
    for(;scanf("%d",&i);printf("%d",i));
    }
    How many times will the for loop get executed?

    When I ran the program, it didn't even execute once but in the book, the correct answer is given as it will run infinite times.

    Kindly throw some light into it.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The loop will terminate when scanf() returns zero. Look up the documentation for scanf() to discover the conditions in which scanf() will return zero.

    Yes, I realise that is not answering your question. However, I have provided enough of a hint so you should be able to work out the answer for yourself.

    Also, main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Mr. grumpy, like I said earlier, I am not an expert in c. I am just a beginner. so, if you can explain a bit more, it would be of great help to me.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You don't need to be an expert, but I am not here to spoonfeed you. I have told you the condition in which the loop will terminate. Find (using google if you like) information on scanf().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by grumpy View Post
    You don't need to be an expert, but I am not here to spoonfeed you. I have told you the condition in which the loop will terminate. Find (using google if you like) information on scanf().
    I just wanted someone to clear my doubt. I didn't invite you to this thread and asked you to spoonfeed me. If you do not know the answer, why do you post the reply unnecessarily?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I know the answer, but you will learn more by reasoning it out for yourself. I have given you hints that you can follow up on, and work out the answer for yourself.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    @tony2011

    Grumpy wasn't trying to be offensive, its just that many people come here and expect homework/assignments to be done for them. That's not why we are here, so if someone doesn't do everything for you and then wants you to show more effort before doing more, then that's nothing for you to get upset about: it's how it is here.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by Fordy View Post
    @tony2011

    Grumpy wasn't trying to be offensive, its just that many people come here and expect homework/assignments to be done for them. That's not why we are here, so if someone doesn't do everything for you and then wants you to show more effort before doing more, then that's nothing for you to get upset about: it's how it is here.
    there is a polite way to say everything. I didn't ask anyone to write any code for me. I just asked a doubt in a code snippet that I found in a book(which has some wrong answers). If google could clear my doubts, I wouldn't be posting them here.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by grumpy View Post
    The loop will terminate when scanf() returns zero. Look up the documentation for scanf() to discover the conditions in which scanf() will return zero.

    Yes, I realise that is not answering your question. However, I have provided enough of a hint so you should be able to work out the answer for yourself.

    Also, main() returns int, not void.
    This is just a code snippet given in a book. So, i just have to work out with whats given. What difference does it make if the return type is int or void here?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tony, welcome to the forum!

    The great thing about programming is that you can simply print out the answer that you're looking for:
    Code:
    int number;
    printf("Enter an integer: \n");
    printf("%d", scanf("%d",&number));
    put that into a program and see what you get for the return from scanf(). Now what would the return be from fscanf(), on a file number?

    What about a string? A char?, A floating point number?

    We're glad to have you here, but also want to encourage that sense of exploration that every professional should have.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tony2011 View Post
    I just wanted someone to clear my doubt. I didn't invite you to this thread and asked you to spoonfeed me. If you do not know the answer, why do you post the reply unnecessarily?
    Grumpy gave you a perfectly good answer... "Look it up"...

    Looking in reference materials for information is a skill all programmers must cultivate at some point in time. On a typical day I spend half my time (or more) looking up the correct syntax and other information about function calls. This is very simply because there's just too much for any one person to even hope to memorize correctly...

    Why don't you see expert programmers (like Grumpy) on here begging for answers then getting upset when they don't get the exact response they wanted? Because they know how to and are willing to look up the information on their own.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tony2011 View Post
    This is just a code snippet given in a book. So, i just have to work out with whats given. What difference does it make if the return type is int or void here?
    It matters because even if it's just a little "test program" it is still a program and the operating system has certain expectations of it's behaviour. There is no excuse for sloppy work...

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    Grumpy gave you a perfectly good answer... "Look it up"...

    Looking in reference materials for information is a skill all programmers must cultivate at some point in time. On a typical day I spend half my time (or more) looking up the correct syntax and other information about function calls. This is very simply because there's just too much for any one person to even hope to memorize correctly...

    Why don't you see expert programmers (like Grumpy) on here begging for answers then getting upset when they don't get the exact response they wanted? Because they know how to and are willing to look up the information on their own.
    Here we go again. Another unwanted reply. Guess there aren't any real programmers around here. I can see only people giving advices. And, I am not begging you 'Please help me! Please help me!' I posted a snippet and asked for a clarification. If you know the answer and of you are willing, post the explanation. Else, keep your figers away from the keyboard. Don't reply unnecessarily just to increase your post count.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    It matters because even if it's just a little "test program" it is still a program and the operating system has certain expectations of it's behaviour. There is no excuse for sloppy work...
    What is your problem? Did you read my question? Did you understand it? Don't reply just for the sake of it.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tony2011
    When I ran the program, it didn't even execute once but in the book, the correct answer is given as it will run infinite times.
    Well, I can tell you that your book's answer is not correct, but then you could have guessed as much by your own observation of the output that you got. Again, if you want to know why your book's answer is not correct, read up on the return value of scanf. Predict what might happen if you try different input, then actually try the different input and confirm your answers.

    Quote Originally Posted by tony2011
    Guess there aren't any real programmers around here.
    Yeah, Real Programmers code in assembly language, not C
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++0x - a few doubts
    By Mario F. in forum C++ Programming
    Replies: 33
    Last Post: 09-23-2006, 01:22 PM
  2. Some doubts about programming in C
    By Olimpo in forum C Programming
    Replies: 8
    Last Post: 09-23-2006, 01:07 AM
  3. Two doubts about C for my project
    By j0nnyX in forum C Programming
    Replies: 4
    Last Post: 10-11-2004, 02:31 PM
  4. doubts on scanf
    By aqua in forum C Programming
    Replies: 1
    Last Post: 10-28-2002, 04:20 AM
  5. ASP,PHP doubts
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-14-2002, 11:17 AM