Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on January 31, 2011, 09:08:38 am

Title: C programming problem
Post by: Munchor on January 31, 2011, 09:08:38 am
Code: [Select]
#include <stdio.h>

main() {
    // This program solves the f91 recursive function for a given number
    // This program was coded by David
    printf("Enter number: ");
    int input;
   
    scanf("%d",input);
   
    printf("The result is: ",f91(input));
   
    getch();
    return 0; 
}

int f91(n) {
    if (n<101)
    {
        return f91(f91(n+11));           
    }   
    else
    {
        return n-10;
    }
}

This is my code, which is supposed to solve the f91 function. I was converting it from my other python program here (http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=23513).

The program allows input but then crashes. Any idea?
Title: Re: C programming problem
Post by: Galandros on January 31, 2011, 09:52:54 am
Try     scanf("%d",&input);

Note the & as pointer to operator.
Title: Re: C programming problem
Post by: Tribal on January 31, 2011, 04:31:39 pm
Also change the printf to:
Code: [Select]
printf("The result is: %i",f91(input));So that it will show the output of the function ;)
Title: Re: C programming problem
Post by: Munchor on February 02, 2011, 02:18:03 pm
Thank you two, now who can explain me what %i stands for?
/me just googled!

Thanks ;D
Title: Re: C programming problem
Post by: Ranman on February 02, 2011, 02:44:50 pm
Thank you two, now who can explain me what %i stands for?
/me just googled!

Thanks ;D

The % is a special token. It tells the compiler that the next symbol (in this case 'i') will be used for specialized text formatting. The 'i' is for a 16 bit integer. Hence, the integer (returned from the call to f91(input)) will be inserted into the string to print to the screen.

Take a look at the The C Book (http://publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html): Formatted I/O chapter.
Title: Re: C programming problem
Post by: calc84maniac on February 02, 2011, 02:49:33 pm
Thank you two, now who can explain me what %i stands for?
/me just googled!

Thanks ;D

The % is a special token. It tells the compiler that the next symbol (in this case 'i') will be used for specialized text formatting. The 'i' is for a 16 bit integer. Hence, the integer (returned from the call to f91(input)) will be inserted into the string to print to the screen.

Take a look at the The C Book (http://publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html): Formatted I/O chapter.
Actually, I'd think it would be a 32-bit integer unless you're using a really old computer. At any rate, it should be used for "int" values.
Title: Re: C programming problem
Post by: Ranman on February 02, 2011, 02:56:29 pm
Actually, I'd think it would be a 32-bit integer unless you're using a really old computer. At any rate, it should be used for "int" values.
Well... You are correct. 'i' stands for the native "word" length of a particular CPU. For our 68K calcs (Motorola 68000 cpu) the "word" length is 16 bits... you would have to use 'l' to properly print a 32 bit integer. For modern CPUs, the word length is 32bits... so 'i' would print a 32bit integer.
Title: Re: C programming problem
Post by: Munchor on February 02, 2011, 06:50:30 pm
I knew what % did, just not %i, INTEGER.

Thanks anyways ;D