# Thread: Problem in my first program :/

1. ## Problem in my first program :/

this is my first program, a simple arithmetic, but it wont add!

Code:
```main()
{
int x,y,z;

scanf("%d", x);
scanf("%d", y);

z=x+y;

printf("sum is:", z);

}```
where did I go wrong? compiler shows no error,
takes input and prints only--> sum is:

pretty demotivating for my first program!

2. Originally Posted by RattleSlash
this is my first program, a simple arithmetic, but it wont add!
No worries... a very simple fix...

Code:
```main()
{
int x,y,z;

scanf("%d", x);
scanf("%d", y);

z=x+y;

printf("sum is: %d \n", z);

}```
And don't let it bug you... I've been at this for a long time and I still make some rather obvious mistakes... It kinda comes with the territory.

3. You really should read through whatever study material you are using, you seemed to have missed some crucial knowledge. Have a read through the site's C Tutorials. As for your immediate questions:
Code:
```#include <stdio.h>

int main(void){ //note how main is defined

int num1, num2, result; //note descriptive variable names

printf("Enter two numbers:");
/*scanf takes pointers to the variables
to store the input. Note the use of the & operator*/
scanf("%d %d", &num1, &num2);
result = num1+num2;

printf("Result is: %d", result);

return(0); //Note returning a value to the OS
}```

4. Make one more simple fix: (in red)

Code:
```int main(void)
{
int x,y,z;

scanf("%d", &x);
scanf("%d", &y);

z=x+y;

printf("sum is: %d \n", z);

return 0;

}```
Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.

int main(void) with a return of 0, is something you want to add to your programs as well. May not be necessary right now, but it is ONE of the standard ways to structure a C program -- main() is not.

5. Originally Posted by AndrewHunter
You really should read through whatever study material you are using, you seemed to have missed some crucial knowledge. Have a read through the site's C Tutorials. As for your immediate questions:
Code:
```#include <stdio.h>

int main(void){ //note how main is defined

int num1, num2, result; //note descriptive variable names

printf("Enter two numbers:");
/*scanf takes pointers to the variables
to store the input. Note the use of the & operator*/
scanf("%d %d", &num1, &num2);
result = num1+num2;

printf("Result is: %d", result);

return(0); //Note returning a value to the OS
}```
thanx andrew!
did it! and it worked!

6. Originally Posted by Adak
Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.
To be even more precise, any function that you expect it to change whatever you are passing it, without assigning that value via a return value, needs the address of (a pointer to) whatever it is expected to change: Pointers in C - Tutorial - Cprogramming.com

Quzah.

7. Originally Posted by Adak
Make one more simple fix: (in red)

Originally Posted by CommonTater
No worries... a very simple fix...

Code:
```main()
{
int x,y,z;

scanf("%d", &x);
scanf("%d", &y);

z=x+y;

printf("sum is: %d \n", z);

}```
Scanf() needs the address of the variable it is supposed to change. Printf() doesn't need it, but scanf() does.
Nice catch Andrew and Adak...

(I'm thinking it's time to get my reading glasses prescription updated... "What's that?" ... "I can CLEAN them?".... Who knew! )

8. [QUOTE=CommonTater;1052036]
Originally Posted by Adak
Make one more simple fix: (in red)

Nice catch Andrew and Adak...

(I'm thinking it's time to get my reading glasses prescription updated... "What's that?" ... "I can CLEAN them?".... Who knew! )
You are spending too much time with your other projects, it is killing your eyes my friend.

9. Originally Posted by quzah
To be even more precise, any function that you expect it to change whatever you are passing it, without assigning that value via a return value, needs the address of (a pointer to) whatever it is expected to change: Pointers in C - Tutorial - Cprogramming.com

Quzah.

I hope some day soon I understand watever you just said!

10. Quote tags: You are doin' it wrong.

Quzah.

11. Originally Posted by RattleSlash
I hope some day soon I understand watever you just said!
You will... just keep working your textbooks and doing those examples and exercises... It'll come soon enough.

12. Originally Posted by AndrewHunter
You are spending too much time with your other projects, it is killing your eyes my friend.
<sigh> I fear you may be right...

13. Originally Posted by quzah
Quote tags: You are doin' it wrong.
Quzah.
Who me? No way...

14. Originally Posted by RattleSlash
I hope some day soon I understand watever you just said!
Don't count on it; English is not quzah's first language.

15. Originally Posted by quzah
Quote tags: You are doin' it wrong.

Quzah.
Interesting enough, these errors have showed up after the switch to the new format. In all cases I have just hit the "Reply with Quote" link.

Popular pages Recent additions