"Syntax error at end of input"

This is a discussion on "Syntax error at end of input" within the C Programming forums, part of the General Programming Boards category; When I try to compile this code it tells me there is a "syntax error at end of input" at ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    "Syntax error at end of input"

    When I try to compile this code it tells me there is a "syntax error at end of input" at line 21. This makes no sense to me, the code is as simple as it can be. What's going on here?

    Code:
    #include <stdio.h>
    
    long power(int, int);
    
    int main()
    {
    int input1, input2;
    
    while (1) {
       printf("Enter base and exponent ");
       scanf("%d%d", &input1, &input2);
       printf("%d\n", power(input1, input2) );
       }
    
    long power(int base, int exponent)
    {
       if (exponent == 1)
          return base;
       else
          return base * power(base, (exponent - 1) );
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include <stdio.h>
    
    long power(int, int);
    
    int main()
    {
    	int input1, input2;
    	
    	while (1) 
    	{
    		printf("Enter base and exponent ");
    		scanf("&#37;d%d", &input1, &input2);
    		printf("%d\n", power(input1, input2) );
    	}
    	return 0;
    }
    	
    long power(int base, int exponent)
    {
    	if (exponent == 1)
    		return base;
    	else
    		return base * power(base, (exponent - 1) );
    }
    PS. There is such thing like proper indentetion
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thank you. That's something I really need to work on.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're assuming the exponent is >= 1. You could allow 0 by having the recursion terminate with exponent == 0, and since exponent is nonnegative, you should make it unsigned. Then you use &#37;u instead of %d for input2 in the scanf statement.

    Edit: Actually, it's debated whether 0^0 should be defined to be 1. If not you would have to disallow a zero base with a zero exponent.

    http://www.faqs.org/faqs/sci-math-fa...lnumbers/0to0/
    Last edited by robatino; 08-05-2007 at 02:33 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    are there performance gains from using an unsigned variable?

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I doubt there would be, but doing maths on negative numbers requires more work than if they were positive.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by yougene View Post
    are there performance gains from using an unsigned variable?
    You normally get roughly twice the positive range (which is the only range you care about if the variable is always nonnegative). Certain bitwise operations such as right shift are only portable with unsigned types. It also makes the code self-documenting by indicating that the value is always nonnegative.

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by robatino View Post
    Certain bitwise operations such as right shift are only portable with unsigned types. It also makes the code self-documenting by indicating that the value is always nonnegative.
    If you use a signed variable and do right shift on it, the sign bit will propagate, which in turn can lead to unwanted result, so you really SHOULD use unsigned variable when you are doing right shift, but I believe this has nothing to do with portability. But I might be wrong, i'm not totally 100% sure.


    Be careful if you mix unsigned and signed variable, this can lead to unwanted results. Binary pattern will always stay the same but it won't mean the same thing. But in your case that's not really important.

    Oh, and beside being cool, your recursive fonction is slower than an iterative one. And you should have some entry validation, so if someone enter something like -1 for the exponent you won't have a close-to infinte recursive fonction (which i guess would take all the memory from the stack before being able to terminate).

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thanks for the input everyone

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > If you use a signed variable and do right shift on it, the sign bit will propagate,
    Not necessarily. Some systems do propagate the sign bit, but others propagate zero.
    It is this which makes right-shift of negative numbers very non-portable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Salem View Post
    > If you use a signed variable and do right shift on it, the sign bit will propagate,
    Not necessarily. Some systems do propagate the sign bit, but others propagate zero.
    It is this which makes right-shift of negative numbers very non-portable.
    It's even worse than that. The answer depends not only on whether it propagates the sign bit (arithmetic shift) or zero (logical shift), but on whether the signed arithmetic the system uses is two's complement, ones complement, or sign-magnitude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 06:17 PM
  5. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 11:05 AM

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