In: Computer Science
C Programming question:
Develop a program named “sample” that prints the value of two
functions:
1. Function f1 takes a single input argument of type int and
returns the value of its input argument as float
2. Function f2 takes a single input argument of type char and
returns an int. The output of f2 is the result of the sum of the
values of the global variables, and the input
argument minus the value of char ‘a’ plus the value of char ‘A’
Structural Requirements:
1. Your executable must be named sample (your makefile must ensure
this)
2. Your program should not take any arguments
3. Your solution must have at least two C source files (e.g., a
main and file1.c), two header files (e.g., globals.h, and
externs.h), and a makefile
4. Your program should define two global variables (e.g., p and q),
both of type int, one with value 11 and the second with a value of
32
5. Your main should have two local variables: int i = 5; char c =
‘x’
6. Main should invoke f1 with the sum of i, p, and q as input, and
pass the value of c to f2
IDE Used: Dev C++
Required Files are given below
main.c
#include <stdio.h>
#include <stdlib.h>
#include "globals.h" //for using function f1 and
f2
#include "externs.h" //for using global variables p and
q
{
int i =5;
char c='x';
//sum of i+p+q
i=i+p+q;
//calling function f1
printf("\nResult of function f1: %0.2f",f1(i));
//calling of function
f2
printf("\nResult of function f2: %d",f2(c));
return 0;
}
file.c
#include <stdio.h>
#include <stdlib.h>
#include "globals.h" //to use global variables p and
q
extern int p=11;
extern int q=32;
//defination of function f1
float f1(int i)
{
return (float)i;
}
//defination of function f2
int f2(char c)
{
return (int)c+p+q-'a'+'A';
}
externs.h
#ifndef file1
#define file1
//declaration of function f1 and f2
float f1(int);
int f2(char);
#endif
globals.h
#ifndef globalVar
//declaration of global variales
int p;
int q;
#endif
Makefile.win
# Project: sample
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = main.o file1.o
LINKOBJ = main.o file1.o
LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program
Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib"
-static-libgcc
INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include"
-I"C:/Program Files
(x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program
Files
(x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include"
-I"C:/Program Files
(x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program
Files
(x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
-I"C:/Program Files
(x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN = sample.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CC) $(LINKOBJ) -o $(BIN) $(LIBS)
main.o: main.c
$(CC) -c main.c -o main.o $(CFLAGS)
file1.o: file1.c
$(CC) -c file1.c -o file1.o $(CFLAGS)
Output