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

Click For Summary
SUMMARY

The discussion centers on writing a C program that adds two 16-bit integers and two 32-bit integers, returning a long integer result. Participants clarify that the function should accept four parameters, one for each integer, and emphasize the importance of good design practices by recommending against using global variables directly. The confusion arises from whether to add the integers separately or together, but the consensus is to combine all four integers in one function call. Participants suggest seeking clarification from the instructor if the assignment remains unclear.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of data types, specifically 16-bit and 32-bit integers
  • Familiarity with function declaration and parameter passing in C
  • Concepts of local and global variables in programming
NEXT STEPS
  • Learn about function parameter passing in C programming
  • Study the differences between local and global variables in C
  • Explore integer data types and their memory implications in C
  • Practice writing C functions that return values and handle multiple parameters
USEFUL FOR

C programmers, computer science students, and anyone looking to improve their understanding of function design and variable scope in C programming.

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
2K
  • · 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