Like Tree2Likes
  • 1 Post By root4
  • 1 Post By rags_to_riches

How it happen...?

This is a discussion on How it happen...? within the C Programming forums, part of the General Programming Boards category; hai.. I am new one to C. the following code makes infinite loop ...!! how it happen.. Code: main() { ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    28

    How it happen...?

    hai.. I am new one to C. the following code makes infinite loop ...!! how it happen..

    Code:
    main()
    {
    int x;
    char y[10];
    for(x=0;x<10;x++)
    x=scanf("%c",&y[x]);
    printf("%d",x);
    }
    Thank you..

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    What do you expect scanf() to return?

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    28
    number of characters entered

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    It returns the number of successful assignments, so in your case, it always returns 1.
    Last edited by root4; 08-11-2012 at 02:51 PM.
    veera likes this.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    28
    But in this case it return garbage value.....
    Code:
    main()
    {
    int x,count;
    char y[10];
    for(x=0;x<10;x++)
    count+=scanf("%c",&y[x]);
    printf("%d",count);
    }

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Because count was never given an initial value.
    veera likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this happen?
    By metros in forum C Programming
    Replies: 11
    Last Post: 02-27-2010, 12:29 AM
  2. Why this happen always...
    By jawwadalam in forum Tech Board
    Replies: 1
    Last Post: 11-16-2002, 08:03 PM
  3. Hey, what would happen if...
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-05-2002, 07:54 AM
  4. What would happen?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-04-2002, 09:54 AM
  5. Why Does This Happen?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-28-2001, 11:55 AM

Tags for this Thread


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