Skip to content

Instantly share code, notes, and snippets.

@fcayci
Last active October 7, 2021 08:45
Show Gist options
  • Save fcayci/61c60602ac98304741fe1728a80d1d01 to your computer and use it in GitHub Desktop.
Save fcayci/61c60602ac98304741fe1728a80d1d01 to your computer and use it in GitHub Desktop.
Starting template for C projects
/*
* banana.c
*
* author: Furkan Cayci
*
* description: calculate and give the result for Rosenbrock's banana function.
* https://en.wikipedia.org/wiki/Rosenbrock_function
*
*/
#include "banana.h"
#include <math.h>
#include <stdio.h>
/* define variables */
double a = 1.0;
double b = 100.0;
/* Rosenbrock's banana function definition */
double banana(double x, double y) {
printf(
"banana.c: Calculating Rosenbrock's banana function with a: %.3f, "
"b: %.3f..\n",
a, b);
return pow((a - x), 2) + b * pow((y - pow(x, 2)), 2);
}
#ifndef BANANA_H_
#define BANANA_H_
/* function declaration */
double banana(double, double);
#endif /* BANANA_H_ */
/*
* main.c
*
* author: Furkan Cayci
*
* description: Basic program that calls Rosenbrock's banana function for
* a given x an y values and prints the result
*/
#include <stdio.h>
#include <stdlib.h>
#include "banana.h"
/* bring a and b from other file if -really- needed */
extern double a;
extern double b;
int main(void) {
printf("main.c: Hello from main \n");
double x = -1.9;
double y = 2.4;
double res = banana(x, y);
printf("main.c: Result for x: %.1f, y: %.1f is: %.3f\n", x, y, res);
/* change initial conditions if -really- needed */
a = 1.0;
b = 1.0;
res = banana(x, y);
printf("main.c: Result for x: %.1f, y: %.1f is: %.3f\n", x, y, res);
return EXIT_SUCCESS;
}
TARGET = banana
CC = gcc
LD = gcc
# Add your sources here
SRCS += main.c
SRCS += banana.c
INCLUDES += -I.
OBJS = $(SRCS:.c=.o)
CFLAGS += -std=c11 # c11 standard
CFLAGS += -O0 # optimization is off
CFLAGS += -fno-common
CFLAGS += -Wall # turn on warnings
CFLAGS += -Wextra # extra warnings
CFLAGS += -pedantic # strict ISO warnings
CFLAGS += -Wno-unused-variable # Do not show unusued warnings
CFLAGS += -Wmissing-include-dirs
CFLAGS += -Wsign-compare
CFLAGS += -Wcast-align
CFLAGS += -Wconversion # neg int const implicitly converted to uint
LDFLAGS += -lc -lm
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)
@echo "Successfully finished..."
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
@rm -rf $(TARGET)
@rm -rf *.o
.PHONY: all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment