Bruce
Bruce

Reputation: 35295

Are there global asserts in C?

Say I have a program where the value of an integer i should never be negative. Is there a way I can insert a global assert(i>=0) such that whenever i becomes negative an error is reported. This can be very useful while debugging. I don't want to put an assert everywhere the value of i is manipulated.

Upvotes: 2

Views: 135

Answers (3)

George Kastrinis
George Kastrinis

Reputation: 5182

Why don't you just declare i as unsigned?

Upvotes: -1

noelicus
noelicus

Reputation: 15055

No. You'll have to pick a point in the program's execution to keep checking that variable/assert.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272832

No.

Your debugger might have this sort of facility, though. For instance, GDB has watchpoints.

Upvotes: 6

Related Questions