c/count2.c

31 lines
890 B
C
Raw Permalink Normal View History

2023-01-23 21:01:46 -05:00
#include <stdio.h>
2023-01-24 08:52:48 -05:00
#include <stdlib.h>
2023-01-23 21:01:46 -05:00
2023-01-24 08:52:48 -05:00
// Use the indirection operator (*) to declare a variable, point to int and
2023-01-24 19:29:16 -05:00
// assign it the memory address of the counter variable object
2023-01-23 21:01:46 -05:00
void retrieve(int *pcounter) {
2023-01-23 21:32:23 -05:00
printf ("%d", *pcounter);
2023-01-23 21:01:46 -05:00
}
void increment(void) {
2023-01-24 12:35:59 -05:00
// Set the counter variable as static so its value does not change when it
// goes out of scope
// Set the counter variable as an unsigned (non-negative, greater than or
// equal to zero) integer
2023-01-24 19:29:16 -05:00
// Initialize the object of the counter variable to zero
2023-01-23 21:01:46 -05:00
static unsigned int counter = 0;
2023-01-24 19:29:16 -05:00
// Use the increment operator (++) to increase the value of the object
2023-01-23 21:01:46 -05:00
counter++;
2023-01-24 12:53:39 -05:00
// Call the retrieve function and send the memory address of the counter
2023-01-24 19:29:16 -05:00
// variable object using the unary operator (&)
2023-01-23 21:01:46 -05:00
retrieve(&counter);
}
int main(void) {
for (int i=0; i<5; i++) {
increment();
}
2023-01-24 08:52:48 -05:00
return EXIT_SUCCESS;
2023-01-23 21:01:46 -05:00
}