static Local Variables
Completed
Write int next_id() using a static int inside it, so each call returns 1, 2, 3, … Read names and print #id name for each.
Input
n, then n names.
Output
n lines.
Example
Input
3 ann bob cy
Output
#1 ann #2 bob #3 cy
Editor
C
Hints
A static local keeps its value between calls.
Solution explanation
static locals are created once and live for the whole program, but are only visible inside the function.
Show solution
#include <stdio.h>
int next_id() {
static int id = 0;
id++;
return id;
}
int main() {
int n;
char name[50];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", name);
printf("#%d %s\n", next_id(), name);
}
return 0;
}