Thread: Why does this code work?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Why does this code work?

    This very small program is an example from a book on C programming I'm currently working through. It runs perfectly. I understand most of it. That being said, I cannot understand why the initialization of num does not cause an error.

    Code:
    #include <stdio.h>
    
    long	factorial( long num );
    
    int main (int argc, const char * argv[]) {
    	long	num = 5L;
    	
    	printf( "%ld factorial is %ld.", num,
               factorial( num ) );
    	
    	return 0;
    }
    
    
    long	factorial( long num ) {
    	if ( num > 1 )
    		num *= factorial( num - 1 );
    	
    	return( num );
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Whitewings View Post
    This very small program is an example from a book on C programming I'm currently working through. It runs perfectly. I understand most of it. That being said, I cannot understand why the initialization of num does not cause an error.

    Code:
    #include <stdio.h>
    
    long factorial( long num );
    
    int main (int argc, const char * argv[]) {
      long num = 5L;
    
      printf( "%ld factorial is %ld.", num,
        factorial( num ) );
    	
      return 0;
    }
    
    long factorial( long num ) {
      if ( num > 1 )
        num *= factorial( num - 1 );
       return( num );
    }
    Although it may be of no consequence on most compilers, some older ones will balk at tabs between the type and variable names...

    Why would.... long num = 5L;.... not work? The L suffix merely tells the compiler to treat the number 5 as a long int.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Thank you. I had forgotten the significance of the L suffix.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Although it may be of no consequence on most compilers, some older ones will balk at tabs between the type and variable names...
    *boggle* - which brain-damaged compiler would barf so badly on white space?
    I've used some pretty ropey compilers in the past, but none have failed that badly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Whitewings View Post
    This very small program is an example from a book on C programming I'm currently working through. It runs perfectly. I understand most of it. That being said, I cannot understand why the initialization of num does not cause an error.
    When you declare something like, long num, int is implied. This is guaranteed by the standard.

    EDIT: C99 6.7.2.2/5
    Last edited by AndrewHunter; 08-16-2011 at 10:46 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > Although it may be of no consequence on most compilers, some older ones will balk at tabs between the type and variable names...
    *boggle* - which brain-damaged compiler would barf so badly on white space?
    I've used some pretty ropey compilers in the past, but none have failed that badly.
    Oh gees now you want me to go and find that? I don't remember the name... but it had a ton of other problems too.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm guessing it was for some embedded C compiler for some rather obscure bit of hardware.
    Your average desktop user (especially a student) using a popular modern compiler isn't likely to find an actual compiler bug.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Oh gees now you want me to go and find that? I don't remember the name... but it had a ton of other problems too.
    Heh, I'm curious as to why you mentioned that little piece of trivia out of the blue.
    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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Heh, I'm curious as to why you mentioned that little piece of trivia out of the blue.
    Before settling on Pelles C I'll bet I tried just about every language/compiler available at the time. A lot --and I do mean a LOT-- of it was pure crap. Switching from a murdered language (Pascal) to something else was neither easy nor graceful. I spent most of a year looking for something that actually did what I expected... Found some really strange stuff along the way.


    @Salem ... then you should visit the Pelles C forums... they have an entire section for bug reports and people do find things... Fortunately most of them are minor or easily worked around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 08:35 AM
  2. My Code Does Not Work? Why?
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 02:03 PM
  3. This code does not work in Bloodshed-Dev C++ 4.9.9.2. Why?
    By neo_phyte in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2006, 06:54 AM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM