trying to learn C programming...stuck on addition exercise
CODE:
#include <stdio.h>
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d",integer1);
printf("Enter second integer\n");
scanf("%d",integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
warning: return type defaults to 'int'|
||In function 'main':|
|10|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
|12|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
|10|warning: 'integer1' is used uninitialized in this function|
|12|warning: 'integer2' is used uninitialized in this function|
||=== Build finished: 0 errors, 5 warnings ===|
program crashes when i input 2nd integer
2 replies to this topic
#1
Posted 10 March 2011 - 09:05 PM
|
|
|
#2
Posted 10 March 2011 - 09:50 PM
Scanf's second argument takes a variable, and writes to it. To reference to a variable you wish to write to, you have to apply the & before the variable to get the memory address of it.
You can read more about scanf's parameters here:
scanf - C++ Reference
And a brief knowledge of pointers can help you along the way to understand C's basic functions:
Codecall: Pointers - Crash course
scanf("%d", &integer1);This basically says "Scan an integer from input, in to the memory location of integer1."You can read more about scanf's parameters here:
scanf - C++ Reference
And a brief knowledge of pointers can help you along the way to understand C's basic functions:
Codecall: Pointers - Crash course
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 11 March 2011 - 06:22 AM
So simple now that you've explained it. Worked like a charm.
Thanks a lot. Will be working my way through those tutorials.
Thanks a lot. Will be working my way through those tutorials.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









