The Terminal on Mac: Coding

So found this C code online and need to compile it in Terminal on Mac.
Here is the code:

#include <stdio.h>
#include <math.h>
 
int option;
char q;
int number;
int currentsum;
int value;
 
void calc()
{
     printf("\t\t\t\t\tBegin\n"); // says begin in top middle of screen
     option = 0; number = 0; currentsum = 0; value = 0;
     while(1){
              value = getchar(); // gets user input and sets equal to value
              if (value >= '0' && value <= '9')
              {
                 number = number*10 + (value - '0'); /* a way of reading the "values" left to right. If someone puts in 729 as their first number then it will go across
              and do number = 0*10 (because number was set equal to 0) + 7. then it goes through again. number is now 7 so it goes through and does 7*10 + 2 thus making
              72. Number is now 72 which means it goes 72*10 + 9 making number 729. It will follow this process if the input is a number 1-9*/
              }
              if (value == '+' || value == '-' || value == '*' || value == '/' || value == '^' || value == '=' || value == '\n')
              {
                   if(value == '+') currentsum = currentsum + number; // if I come across one of these symbols then it performs the operation described
                   if(value == '-') currentsum = currentsum - number;
                   if(value == '*') currentsum = currentsum * number;
                   if(value == '/') currentsum = currentsum / number;
                   if(value == '^') currentsum = pow(currentsum, number);
                   if(value == '=') printf(currentsum);  // when it reads an equal sign or a new line it displays the result of the equation.
                   if(value == '\n') printf(currentsum);
              }
              else break; // come across any other symbols or characters break the loop     
             }
}
int main()
{
    system("title Calculator"); // titles windo "Calculator
    system("color 1A"); // makes background blue
    calc(); // goes to void calc
    system("pause");
    scanf("%c",&q);
    if( q == 'q')exit(0);
}

Now just to let you know i do not want to use anything else to compile it except for the Terminal.

There are a few issues here:

  • "Terminal" is an application that emulates a terminal with characteristics similar to a DEC VT-220 terminal with a variable screen size. It is incapable of executing commands, (including compiling any type of source code). OS X runs some command (usually a shell such as bash , csh , ksh , sh [a version of bash on OS X], tcsh , or zsh ) and that command running in terminal may be able to execute commands.
  • I have never seen a shell with a built-in C language compiler either, although in theory that is possible.
  • So, to compile a C program, in addition to terminal and a shell, you need a compiler. OS X out of the box doesn't contain a compiler, but you can download the Xcode package from the app store which will give you some compilers (including cc, gcc, c++), the tools you need to turn a compiled program into an executable object, utilities like lex , make , and yacc ), and graphical tools to build OS X and iOS applications to run on Macs, iPads, iPhones, iWatches, and other devices. So, download and install Xcode.

Then (assuming your source code is in a file named calc.c ), you can compile your code into an executable file named calc with either of the following commands typed into the shell you have running in terminal:

make calc
     or
cc -o calc calc.c

But, I wouldn't consider this code to be a good example of C coding, and would be very surprised if it produced the output you might expect:

  • it is missing several headers that are needed to define function prototypes,
  • it calls printf() without a format string operand seemingly assuming that it will magically use an integer argument passed in as a pointer to a format string to print the value of that integer,
  • it uses the system() function to invoke a command that is not available on OS X systems (for no apparent reason),
  • (if you have written and installed a command named pause and it exits), waits for the user to type in another character for no apparent reason,
  • and then (if the character typed was "q" or any other character), exits.

I hope this helps...

1 Like

To summarize that, your mistake is akin to mistaking the monitor for the computer, and expecting it to work by itself. It does not work that way, and cannot be made to work that way, and if you insist on using it that way, all we can tell you is good luck.

Install the right programs and you will have the right programs. Don't and you won't.

Thank you Don Cragun this was very helpful