C Programming: Adding 16bit and 32bit Integers with Local and Global Variables

Click For Summary
The discussion revolves around a C programming assignment that requires writing a function to add two 16-bit and two 32-bit integers, returning a long integer result. Participants express confusion about whether to add the numbers separately or all together and how to manage local versus global variable declarations. One suggestion is to interpret the task as adding all four numbers within a single function that accepts four parameters. Additionally, it is advised to avoid relying solely on global variables for better design practices. Clarification from the instructor is recommended if the assignment details are unclear.
ineedmunchies
Messages
42
Reaction score
0

Homework Statement


Write a C program to use a function to add two 16bit and two 32bit integers together and return a long integer result.
Call the function twice with different parameters and print the results.
Declare your input variables to one of the function calls as local variables (on the stack) and the other as global variables

Homework Equations





The Attempt at a Solution


Ok so my first problem is the interpretation of the question. Should I add two 16bit numbers, then add the two 32bit numbers, or do I add them all together? Its a little bit confusing.

Calling the function twice would be no problem for me, if I was to declare the variables locally they could be input when the function is called. Or if i was to declare the variables globally then I could just add the variables by referencing their name in the function. But it is doing both of these things with one function that has left me stuck. Anybody any ideas or starting points? Thank you in advance
 
Physics news on Phys.org
ineedmunchies said:

Homework Statement


Write a C program to use a function to add two 16bit and two 32bit integers together and return a long integer result.
Call the function twice with different parameters and print the results.
Declare your input variables to one of the function calls as local variables (on the stack) and the other as global variables

Homework Equations





The Attempt at a Solution


Ok so my first problem is the interpretation of the question. Should I add two 16bit numbers, then add the two 32bit numbers, or do I add them all together? Its a little bit confusing.

Calling the function twice would be no problem for me, if I was to declare the variables locally they could be input when the function is called. Or if i was to declare the variables globally then I could just add the variables by referencing their name in the function. But it is doing both of these things with one function that has left me stuck. Anybody any ideas or starting points? Thank you in advance
My interpretation is that you add all four numbers together in a function that has four parameters. I would advise against just using the global variables inside the function without passing them in as parameters. Even though you could refer to them inside the function without passing them in through the parameter list, it is not a good design practice to do so.
If this is a problem that your instructor created, you should consider asking him/her for some clarification.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K