Click here to Skip to main content
12,952,792 members (35,493 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi im very new to c and i decided to write this program, can anyone tell me why its not counting down from the number entered to 0?
#include <stdio.h>
 
int printloop(int k )
{
    int b;
        for (b = k; b < 0; b--)
     printf("%d\n",b);
}
 
int main()
{
int c;
 
scanf("%d",&c);
if( c > '0' && c < '6')
 
printloop(c);
 
return 0;
}
Posted 25-Sep-12 8:14am
Updated 25-Sep-12 8:19am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

To start with:
if( c > '0' && c < '6')

c is an integer defined but you compare it with a character '0' & '6'. Remove quotes around 0 & 6
  Permalink  
Comments
Sergey Alexandrovich Kryukov 25-Sep-12 14:26pm
   
One bug nailed down, a 5.
--SA
Sandeep Mewara 25-Sep-12 14:29pm
   
Thanks SA.
Marcus Kramer 25-Sep-12 14:32pm
   
+5 for the "other" issue.
Sandeep Mewara 25-Sep-12 14:39pm
   
:)
ridoy 25-Sep-12 15:29pm
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Because in your for (b = k; b < 0; b--), you aren't allowing it to do any work.
The second clause "b<0" indicates that this loop will only run if b < 0.
Since you set b == k which is greater than 0, the loop will not run.
Your solution is for (b = k; b >= 0; b--)
  Permalink  
Comments
Sergey Alexandrovich Kryukov 25-Sep-12 14:26pm
   
Another bug nailed down, a 5.
--SA
Marcus Kramer 25-Sep-12 14:32pm
   
That error was simply too common a mistake that rookies make. I've learned a long time ago to always check the for loop args before anything else when someone says the loop isn't doing anything. Thanks.
Sergey Alexandrovich Kryukov 25-Sep-12 17:41pm
   
Sure...
--SA
Sandeep Mewara 25-Sep-12 14:29pm
   
5! Marcus!

:)
Marcus Kramer 25-Sep-12 14:32pm
   
Thanks Sandeep.
flaringdon 25-Sep-12 14:34pm
   
thanks marcus and sandeep
ridoy 25-Sep-12 15:29pm
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,389
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100