# Thread: Help an ambitious 16 year old learn C

1. If the standard changes so return 1 is valid, that would suck. Are you sure you want to use that as an argument?

Edit: By "valid", I meant the "accepted result returned from main(), given a lack of a return statement when the end of main() is encountered". Poor choice of a word, but yeah....

2. This is great that you guys are talking about standards, but you all are ignoring a couple key things here: which I have now bolded in my original post. Please re-read.

Originally Posted by Xlayer
I finally figured out how to use Codeform. I had to use the online version though. Here is my code for the Fibonacci Number Program.

I used this website to figure out what a Fibonacci Number was:
http://www.jimloy.com/algebra/fibo.htm

It works all the way up to Line 47 which gives me a negative value:
Picture of Output

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int n; /* Number of Fibonnacci Numbers to Find (Inputed by User) */
int index = 1; /* Index of Fibonnaci Number (Starts with 1) */
long int fib_num = 0; /* Fibonacci Number */
long int num1 = 1; /* Represents 1 Fib# before Last Fib# */
long int num2 = 1; /* Reprsents Last Fib# */

/* Prompts user for n, and reads input */
printf( "Please enter the amount of Fibonacci\n"
"numbers to find: ");
scanf( "%ld", &n );

/* Executed until Fib#'s Found == Fib#'s Wanted by User */
while ( index <= n )
{
if ( index <= 2 )
{
printf( "\nFibonacci Number #%d = %ld", index, num2 );
}
else
{
fib_num = num1 + num2;
num1 = num2;
num2 = fib_num;
printf( "\nFibonacci Number #%d = %ld", index, num2);
}
index++;
}
return 0;

}```
I completely ignored pointers, and I am sure there is probably a better algorithm. Let the corrections/learning begin.

(I don't see a use for pointers even though I am sure there is one, but in this situation it seems that the variables are used until the program terminates so i can't see how you could free the memory much sooner than it already is.)

3. You're having an issue with overflow. The number you have in Line #46 is 1,836,311,903, which still fits in a 32-bit number, which is probably what an int and even a long int are on your system. After that, you can't fit everything properly in a 32-bit number, and you'll have weird issues, which I could explain, but I figure you can research on your own.

Basically, you need to use a 64-bit variable to calculate larger numbers than that.

Just be happy it works for values which fit in 32-bit numbers... oh, and use unsigned variables in cases like this.

4. I started my Calculator program. It compiles fine, but when it reaches "Enter a Math Operator: " it doesn't wait for the user to input an operator. Instead, it automatically skips to "Enter Your Second Number: " and waits there for input.

This is shown in this picture of the output:
Picture of Error

Here is my code:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
float num1;
float num2;
char math_operator;
char enter;

printf(
" ---------------------------------------------------------\n"
"                Math Calculator\n"
" ---------------------------------------------------------\n\n"
" This is program is only intended for Math Operations\n"
" that only require 3 arguments, which in this case is\n"
" 2 numbers and a desired Math Operator. The operations\n"
" which can be performed by this program are explained in\n"
" the explantion of available Math Operators located below.\n\n\n"
" --| Explanation of Math Operators |---------------------|\n"
"   |                                                     |\n"
" + | This operator returns the result of one             |\n"
"   | number added to another number.                     |\n"
"   |                                                     |\n"
" - | This operator returns the result of one             |\n"
"   | number subtracted from another number.              |\n"
"   |                                                     |\n"
" / | This operator returns the result of one             |\n"
"   | number divided by another number.                   |\n"
"   |                                                     |\n"
" * | This operator returns the result of one             |\n"
"   | number multiplied by another number.                |\n"
"   |                                                     |\n"
" ^ | This operator returns the result of the             |\n"
"   | first number inputed, raised to the power           |\n"
"   | of the second number.                               |\n"
"   |                                                     |\n"
" ! | This operator returns the result of square          |\n"
"   | root of the first number, given the index,          |\n"
"   | which is the second number inputed. Use 2           |\n"
"   | as the value of the second number for a             |\n"
"   | regular square root operation.                      |\n"
" --|-----------------------------------------------------|\n\n"
" Press ENTER to begin calculations.\n\n" );

scanf( "&#37;c", &enter );

if( enter == '\n' )
{
printf( " Please Enter Your First Number: " );
scanf( "%f", &num1 );
}

printf( "\n  Please Enter a Math Operator: " );
scanf( "%c", &math_operator );

printf( "\n Please Enter Your Second Number: " );
scanf( "%f", &num2 );

if( math_operator == '+' )
{
answer = num1 + num2;
}

if( math_operator == '-' )
{
answer = num1 - num2;
}

if( math_operator == '/' )
{
answer = num1 / num2;
}

if( math_operator == '*' )
{
answer = num1 * num2;
}

if( math_operator == '^' )
{
answer = pow( num1, num2 );
}

if( math_operator == '!' )
{
answer = pow( num1, ( 1 / num2 ) );
}

printf( "%f %c %f = %f", num1, math_operator, num2, answer );

return 0;
}```
Let the corrections/learning begin.

Also, now that I think about it. Would it be at all useful to use a pointer for the variable "enter" since it does not need to be used throughout the rest of the program?

5. Start reading through the FAQs.

http://faq.cprogramming.com/

You'll just keep asking questions that have already been answered. Hint: This one has to do with how input is being read from the buffer. I've written some detailed posts on this before.

6. Codeform isn't mandatory or anything, I just thought you might like it . . .

Originally Posted by @nthony
Actually, it is usually a good idea to have it there, given:
Leaving the trailing "\n" ensures that it is taken off the input stream, so that the next call to a read won't be taking in stale bytes (which will save many headaches if you decide to use non-scanf functions afterwards, such as fgetc).
And it's a good idea to fail on input such as this?
Code:
`3.14^Z`
Once again, that is actually a good thing; and I would encourage all new programmers to leave it out. Not only does it promote current standards, makes for less typing and reduces errors, if in the future ISO decides to change the standards so that main implicitly returns "1" instead, it means that much less dependancy checking that you have to perform.
I think leaving it out is counter-intuituve. int main() should return an int, right? If you start thinking that it's okay, you can leave out the return statement, then that line of thinking might spill over to other functions that you write, and that would be a bad thing.

Code:
```    printf( " Press ENTER to begin calculations.\n\n" );

scanf( "%c", &enter );

if( enter == '\n' )
{
printf( " Please Enter Your First Number: " );
scanf( "%f", &num1 );
}

printf( "\n  Please Enter a Math Operator: " );
scanf( "%c", &math_operator );

printf( "\n Please Enter Your Second Number: " );
scanf( "%f", &num2 );```
Rather than checking if the character read into enter is a newline, why not read characters until you actually get a newline? My favorite for this is
Code:
`while(getchar() != '\n');`
as long as you don't care about checking for EOF.

Popular pages Recent additions