c/count2.c

31 lines
890 B
C

#include <stdio.h>
#include <stdlib.h>
// Use the indirection operator (*) to declare a variable, point to int and
// assign it the memory address of the counter variable object
void retrieve(int *pcounter) {
printf ("%d", *pcounter);
}
void increment(void) {
// 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
// Initialize the object of the counter variable to zero
static unsigned int counter = 0;
// Use the increment operator (++) to increase the value of the object
counter++;
// Call the retrieve function and send the memory address of the counter
// variable object using the unary operator (&)
retrieve(&counter);
}
int main(void) {
for (int i=0; i<5; i++) {
increment();
}
return EXIT_SUCCESS;
}