Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on January 31, 2011, 09:08:38 am
-
#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?
-
Try scanf("%d",&input);
Note the & as pointer to operator.
-
Also change the printf to:
printf("The result is: %i",f91(input));
So that it will show the output of the function ;)
-
Thank you two, now who can explain me what %i stands for?
/me just googled!
Thanks ;D
-
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.
-
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.
-
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.
-
I knew what % did, just not %i, INTEGER.
Thanks anyways ;D