How old is your book? This code will not be compiled by any decent recent compiler
Code:
#include<stdio.h>
int add(p,q);
int sub(p,q);
int main() {
int n1,n2;
char s[4];
printf("Type in an expression:\n");
scanf("%x%s%x",&n1,&s,&n2);
if(s=="add")
printf("%x",add(n1,n2));
}
int add(p,q) {
float c;
c=p+q;
return c;
}
int sub(p,q) {
float c;
c=q-p;
return c;
}
Code:
Compiling src/test.cpp -> obj/test.o
Makefile:52: recipe for target 'obj/test.o' failed
src/test.cpp:3:9: error: ‘p’ was not declared in this scope
int add(p,q);
^
src/test.cpp:3:11: error: ‘q’ was not declared in this scope
int add(p,q);
^
src/test.cpp:3:12: error: expression list treated as compound expression in initializer [-fpermissive]
int add(p,q);
^
src/test.cpp:4:9: error: ‘p’ was not declared in this scope
int sub(p,q);
^
src/test.cpp:4:11: error: ‘q’ was not declared in this scope
int sub(p,q);
^
src/test.cpp:4:12: error: expression list treated as compound expression in initializer [-fpermissive]
int sub(p,q);
^
src/test.cpp: In function ‘int main()’:
src/test.cpp:10:30: error: format ‘%x’ expects argument of type ‘unsigned int*’, but argument 2 has type ‘int*’ [-Werror=format=]
scanf("%x%s%x",&n1,&s,&n2);
^
src/test.cpp:10:30: error: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘char (*)[4]’ [-Werror=format=]
src/test.cpp:10:30: error: format ‘%x’ expects argument of type ‘unsigned int*’, but argument 4 has type ‘int*’ [-Werror=format=]
src/test.cpp:11:11: error: comparison with string literal results in unspecified behaviour [-Werror=address]
if(s=="add")
^
src/test.cpp:12:30: error: ‘add’ cannot be used as a function
printf("%x",add(n1,n2));
^
src/test.cpp: At global scope:
src/test.cpp:15:9: error: redefinition of ‘int add’
int add(p,q) {
^
src/test.cpp:3:5: note: ‘int add’ previously defined here
int add(p,q);
^
src/test.cpp:15:9: error: ‘p’ was not declared in this scope
int add(p,q) {
^
src/test.cpp:15:11: error: ‘q’ was not declared in this scope
int add(p,q) {
^
src/test.cpp:20:9: error: redefinition of ‘int sub’
int sub(p,q) {
^
src/test.cpp:4:5: note: ‘int sub’ previously defined here
int sub(p,q);
^
src/test.cpp:20:9: error: ‘p’ was not declared in this scope
int sub(p,q) {
^
src/test.cpp:20:11: error: ‘q’ was not declared in this scope
int sub(p,q) {
^
cc1plus: all warnings being treated as errors
make: *** [obj/test.o] Error 1