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

In summary, the task is to write a C program that uses a function to add two 16bit and two 32bit integers together and return a long integer result. The function should be called twice with different parameters and the results should be printed. One of the function calls should have the input variables declared as local variables (on the stack) and the other should have them declared as global variables. It is recommended to add all four numbers together in a function with four parameters, rather than just using the global variables inside the function without passing them in as parameters. If there is any confusion or difficulty, it is best to ask for clarification from the instructor.
  • #1
ineedmunchies
45
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
  • #2
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.
 
  • #3
for any help.

my expertise lies in the field of scientific research and experimentation. I am not an expert in programming, but I can provide a general response to the content provided.

From my understanding, the goal of this C program is to create a function that can add two 16bit and two 32bit integers together and return a long integer result. This function will be called twice with different parameters and the results will be printed. The input variables for one function call will be declared locally (on the stack) and for the other function call, they will be declared globally.

To begin, it is important to understand the difference between local and global variables in C programming. Local variables are declared within a function and can only be accessed within that function. They are stored on the stack and are not accessible outside of the function. Global variables, on the other hand, are declared outside of any functions and can be accessed by any function in the program.

To create a function that can add two 16bit and two 32bit integers, you can use the data types "int16_t" and "int32_t" respectively. These data types ensure that the variables are 16 or 32 bits in size. Within the function, you can declare the input variables as local variables and use the "+" operator to add them together. The result can then be returned as a long integer.

For the second function call, you can declare the input variables as global variables and use the same logic to add them together. Since global variables can be accessed by any function in the program, the function does not need to explicitly pass the variables as parameters.

Once the function is created, you can call it twice with different input variables and print the results. This will demonstrate the difference between using local and global variables in the function.

In summary, the key to solving this problem is to understand the difference between local and global variables and how they can be used in a function. I hope this helps and provides a starting point for your solution.
 

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

What is C programming and why is it important?

C programming is a high-level, general-purpose programming language used for developing operating systems, applications, and embedded systems. It is important because it allows for efficient and fast coding, is widely used in industry, and serves as the basis for many other programming languages.

What are the common challenges in C programming?

Some common challenges in C programming include managing memory, debugging, and dealing with pointers. Additionally, the syntax and structure of C can be complex for beginners to grasp.

How do I improve my C programming skills?

To improve your C programming skills, practice regularly, read and understand code written by others, and keep up with new updates and developments in the language. Collaborating with other programmers and seeking feedback on your code can also be helpful.

How do I overcome common errors in C programming?

To overcome common errors in C programming, it is important to thoroughly understand the language's syntax and structure. Debugging tools and techniques, such as using print statements and stepping through code, can also help identify and resolve errors.

Are there any resources for learning C programming?

Yes, there are many resources available for learning C programming, such as online tutorials, courses, and books. Additionally, many universities and colleges offer courses in C programming. It can also be beneficial to join online communities or coding forums to connect with other programmers and learn from their experiences.

Similar threads

Replies
23
Views
3K
Replies
4
Views
4K
Replies
6
Views
2K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
6
Views
2K
Back
Top