Here is a C code:
# include <stdio.h>
# include <stdlib.h>
#include <string.h>
#define MAX 41
int main (void){
FILE *fp;
char words[MAX];
if((fp = fopen(“wordy”, “a+”)) == NULL){
fprintf(stdout, “Can’t open “wordy” file.”);
exit(EXIT_FAILURE);
}
puts(“Enter words to add to the file; press the #”);
puts(“key at the beginning of the line to tewminate,”);
while ((fscanf(stdin, “%40s”, words) == 1) && (words[0] != ‘#’))
fprintf(fp, “%sn”, words);
puts(“File content:”);
rewind(fp);
while (fscanf(fp, “%s”, words) == 1)
puts(words);
puts(“Done!”);
if (fclose(fp) != 0)
fprintf(stderr, “Error closing file.n”);
return 0;
}
I’m trying to run this code with the terminal in Mac, but it didn’t work.
How to execute this code with terminal? What kind of file and name I should use for the code and how can I do can I run it?
Thanks!
Expert Answer
Hi,
Please use below steps-
1) First you need to install a GCC Compiler for mac (You can find it on Google )
2) Make note of the path where you are storing the C file
3) Go to Terminal and set the path
Example- if you have saved in a new folder ProgramC in Document folder
then type this in Terminal
cd Document
cd ProgramC
4) Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)
5) Now Compile your program
make Hello
./hello