LearnFrenzy
Sign in Take a free test

C Variables · Exercise 4 of 4

C Variables Variable Programs

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Programs that use scope, lifetime and storage classes on purpose. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Easy

Make a function that reports how many times it has been called, using a static local.

Question page & discussion
Question 2 Medium

Write next_fib() that returns the next Fibonacci number on every call, keeping its state in static variables.

Question page & discussion
Question 3 Hard

Implement a linear congruential generator with a seed kept in a file-scope static variable, a function to set the seed, and one to get the next number from 0 to 99.

Question page & discussion
Question 4 Easy

Swap two integers using a temporary variable, arithmetic, and XOR, and say which is best.

Question page & discussion
Question 5 Medium

Write record() that remembers the lowest and highest values seen so far and prints them after each reading.

Question page & discussion
Question 6 Medium

Compute factorials up to 20, caching results in a static array so each value is calculated only once.

Question page & discussion
Question 7 Easy

Create a visitor counter whose value lives in a file-scope static variable, with functions to increment, read and reset it.

Question page & discussion
Question 8 Medium

Keep a cart's item count and total (in paise) in private file-scope variables, with a function to add items and one to print the summary.

Question page & discussion
All exercises doneBack to C VariablesRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links