Thread: How it happen...?

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

    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
    396
    What do you expect scanf() to return?

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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    33
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Because count was never given an initial value.

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, 01:29 AM
  2. Why this happen always...
    By jawwadalam in forum Tech Board
    Replies: 1
    Last Post: 11-16-2002, 09: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, 08:54 AM
  4. What would happen?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-04-2002, 10: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