EOF in the context of fscanf? Hints:
man scanf | grep -A 1 -B 4 EOF
and wikipedia article end of file.
CC,
CFLAGS,
CXXFLAGS,
CPPFLAGS,
LDFLAGS,
LDLIBS.
make)
CFLAGS = -Wall -Ofast -std=c1x
C = F
all:
echo CFLAGS
echo $CFLAGS
echo $(C)FLAGS
echo $(CFLAGS)
Suppose you have your whole C-program in one file main.c.
Which of the following makefiles will compile and link the
program into the executable file main?
all: main
main: main.c
main: main.o
all: main
main: main.o
main.o: main.c
all: main
main.o: main.c
main: main.o
main.o: main.c
all: main
main: main.o
main: main.c
cc main.c -o main
main: main.c
$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
all: main
main: main.o
cc main.o -o main
main.o: main.c
cc -c main.c -o main.o
Suppose your main function,
contained in the file main.c, calls the functions
foo and
bar
which are contained correspondingly in the files
foo.c and
bar.c.
Which of the following makefiles will correctly compile and link your
program into the executable file main after the command
make?
main: main.o foo.o bar.o
main: foo.o main.o bar.o
main: foo.o main.o bar.o
cc foo.o main.o bar.o -o main
main: main.o foo.o bar.o
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
main: main.o foo.o bar.o
main.o: main.c
foo.o: foo.c
cc -c $^ -o $@
bar.o: bar.c
main: main.o foo.o bar.o
cc main.o foo.o bar.o -lm -o main
main.o: main.c
cc -c main.c
foo.o: foo.c
cc -c foo.c
bar.o: bar.c
cc -c bar.c
obj = main.o foo.o bar.o
main: $(obj)
#define PI 3.1415927
what is the effect of it?
make
untility and Makefile(s).
input.dat and writes the numbers together with their cosines in a table form
to the file output.dat.
Hint:
double x;
FILE* in = fopen("input.dat","r");
if(in == NULL){ fprintf(stderr,"error: could not open file\n"); return -1;}
FILE* out = fopen("output.dat","w");
while( fscanf(in,"%lg",&x) != EOF ) fprintf(out,"%lg \t %lg\n",x,cos(x));
makefile ($@ stands for the name of the target):
CFLAGS = -Wall -std=c99
LDLIBS = -lm
output.dat: file_io input.dat ; ./file_io
input.dat: makefile ; echo '1.0 1.1 1.2 1.3 1.4' > input.dat
clean: ; $(RM) input.dat output.dat file_io
while( fscanf(stdin,"%lg",&x) != EOF ) fprintf(stdout,"%lg \t %lg\n",x,cos(x));
or even shorter,
while( scanf("%lg\n",&x) != EOF ) printf("%lg \t %lg\n",x,cos(x));
In the makefile:
std_io.out.txt: std_io input.dat; ./std_io < input.dat > std_io.out.txt
for(int i=1; i<argc; i++){double x=atof(argv[i]); printf("%lg\t%lg\n",x,cos(x));}
In the makefile (the automatic variable $@ stands for
the name of the target of the rule):
cmd_io.out.txt: cmd_io makefile ; ./cmd_io 1.0 1.1 1.2 1.3 > $@
typedef
struct {int size; double* data;} vector; with the following
functions:
vector* vector_alloc(int size);
void vector_free(vector* v);
void vector_set(vector* v, int i, double x); /* v_i=x */
double vector_get(vector* v, int i); /* returns v_i */
double* vector_get_ptr(vector* v, int i); /* returns pointer to v_i */
void vector_set_all(vector* v, double x); /* for every i v_i=x */
int vector_fprintf(FILE* stream, const char* format, vector* v);
/* prints the elements of v to the specified stream using the specified
format; returns 0 for success, -1 otherwise */
int vector_fscanf(FILE* stream, const char* format, vector* v);
/* reads (*v).size elements form the stream into the vector v;
returns 0 for success, -1 otherwise */
void vector_add(vector* v, vector* u);
void vector_subtract(vector* v, vector* u);
void vector_scale(vector* v, double a);
int vector_equal(const vector* v, const vector* u);
Use assert.h for range checking.