Mathematic operations and structures in C

In summary: WarrenIf you put "using namespace std;" somewhere near the top of your file, does anything magic happen?No, but this is a common practice in C++ and will allow you to use some C++ features (e.g. templates).- WarrenNo, but this is a common practice in C++ and will allow you to use some C++ features (e.g. templates).
  • #1
lewis198
96
0
Hello guys, I was wondering:

say you have a structure:

struct tag{

int a
char b
int b
int c
}

how would you make int c equal to int a + int b for all variables of type struct tag? I have tried using pointers but it baffles me how you would make a global change in that all variable s of type struct tag would be affected, rather than just specific variables.

thank you for your time.
 
Technology news on Phys.org
  • #2
*variables rather than variable s
 
  • #3
You'd have to perform the assignment t.c = t.a + t.b for every variable of type tag that you create. There is no such thing as a "global change."

If you decide to use C++ features, you can turn this structure into a class, and then use a member function to calculate c, rather than storing it directly.

- Warren
 
  • #4
chroot said:
If you decide to use C++ features, you can turn this structure into a class, and then use a member function to calculate c, rather than storing it directly.

If you want to do that it would look like:

Code:
struct tag{

int a;
char b2;
int b;
int c() { return a + b; }
};

But remember, like chroot said, you cannot do this in C, you must be using C++; also did you notice you have two structure elements named "b"...?
 
  • #5
oops, sorry-heehee!

but wow you can do that in c++, i'd better translate
 
  • #6
would you have to declare a and b in the meber function c?
 
  • #7
*member
 
  • #8
No, a and b would be class variables (variously called 'member variables,' 'class fields,' 'member fields,' etc.).

- Warren
 
  • #9
Hey guys I have a problem. When trying to compile this on dev c++ it wpon't let me, as it says I'm trying to make it a function, and when a change struct to a class, it says error before the class name, as well as calling 'private:' and 'public:' errors.
 
  • #10
in fact, my devc++ compiler does not seem to accept any c++ language-when i enter cout it says 'undeclared variable'. Do you guys know what the problem might be?
 
  • #11
Sounds like the problem is that you don't know C++.

Show us some examples of the code that doesn't work and we'll try to help.

- Warren
 
  • #12
lewis198 said:
in fact, my devc++ compiler does not seem to accept any c++ language-when i enter cout it says 'undeclared variable'. Do you guys know what the problem might be?

Did you say #include <iostream>?

If you put "using namespace std;" somewhere near the top of your file, does anything magic happen?
 
  • #13
You mentioned "C", but you're trying to use a "C++" feature. If you want your source code to be compiled as "C++", the file name extension should be "cpp", not "c", such as "example.cpp" instead of "example.c".
 

1. What are the basic mathematical operations in C?

In C, the basic mathematical operations are addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operations follow the same order of operations as in mathematics and can be used with numerical values or variables.

2. How do you perform exponentiation in C?

In C, exponentiation is performed using the caret (^) operator. For example, 2^3 would result in 8. This operator can also be used with variables and decimal values.

3. What is the difference between integer division and floating-point division in C?

Integer division, denoted by the / operator, results in a truncated integer value. For example, 5 / 2 would result in 2. Floating-point division, denoted by the / operator, results in a decimal value. For example, 5.0 / 2.0 would result in 2.5. It is important to note that dividing by 0 in either type of division will result in an error.

4. How do you use parentheses in mathematical expressions in C?

Just like in mathematics, parentheses can be used in C to change the order of operations. Expressions within parentheses are evaluated first. For example, (2 + 3) * 4 would result in 20, while 2 + (3 * 4) would result in 14.

5. What are some commonly used mathematical functions in C?

C provides a variety of mathematical functions, including sin(), cos(), tan(), sqrt(), pow(), and abs(). These functions can be used to perform more complex mathematical operations and often require the inclusion of the library.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
52
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
925
  • Programming and Computer Science
Replies
17
Views
1K
Replies
10
Views
961
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
4K
Back
Top