Hi, I'm starting to get used to coding a C program using different source files. I would like to know when should a function be encapsulated (so it can only be used on the *.c file it was defined in) with the static keyword.
For example, I have three file called main.c, enter-name.h and enter-name.c with the following contents:
main.c:
Code:
#include <stdio.h>
#include "enter-name.h"
int main(void) {
char name[NAME_SIZE];
void EnterName(name);
void greet(name);
return 0;
}
enter-name.h:
Code:
#define NAME_SIZE 21
void RemoveNewline(char *name);
void EnterName(char *name);
void greet(char *name);
enter-name.c:
Code:
#include <stdio.h>
#include "enter-name.h"
void RemoveNewline(char *name) {
for (int i = 0; i < NAME_SIZE; i++) {
if (name[i] == '\n') {
name[i] = '\0';
break;
}
}
}
void EnterName(char *name) {
printf("Please, enter your name: ");
fgets(name, NAME_SIZE, stdin);
RemoveNewline(name);
}
void greet(char *name) {
printf("Hello, %s\n", name);
}
In this example, the function RemoveNewline() is only used in the enter-name.c file. As I understand it, that function should have the static keyword. Is this correct?
Thanks in advance.