Need help to see if this is C that is used in Linux..... :)

B

blackneos940

Guest
So here is this code I got from http://fresh2refresh.com/c/c-code-for-calculator-application/ to study so that I can get better at C..... :D
Do I remove all the "voids" here, so that this is more modern.....? :) I appreciate the help....... :D I also have the Linux src, but that seems too complex for me right now..... :(

Code:
// Calculator example using C code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define KEY "Enter the calculator Operation you want to do:"
// Function prototype declaration
void addition();
void subtraction();
void multiplication();
void division();
void modulus();
void power();
int factorial();
void calculator_operations();
// Start of Main Program
int main()
{
    int X=1;
    char Calc_oprn;
    // Function call
    calculator_operations();
    while(X)
    {
        printf("\n");
        printf("%s : ", KEY);
        Calc_oprn=getche();
        switch(Calc_oprn)
        {
            case '+': addition();
                      break;
            case '-': subtraction();
                      break;
            case '*': multiplication();
                      break;
            case '/': division();
                      break;
            case '?': modulus();
                      break;
            case '!': factorial();
                      break;
            case '^': power();
                      break;
            case 'H':
            case 'h': calculator_operations();
                      break;
            case 'Q':
            case 'q': exit(0);
                      break;
            case 'c':
            case 'C': system("cls");
                      calculator_operations();
                      break;
            default : system("cls");
    printf("\n**********You have entered unavailable option");
    printf("***********\n");
    printf("\n*****Please Enter any one of below available ");
    printf("options****\n");
                      calculator_operations();
        }
    }
}
//Function Definitions
void calculator_operations()
{
    //system("cls");  use system function to clear
    //screen instead of clrscr();
    printf("\n            Welcome to C calculator \n\n");
    printf("******* Press 'Q' or 'q' to quit ");
    printf("the program ********\n");
    printf("***** Press 'H' or 'h' to display ");
    printf("below options *****\n\n");
    printf("Enter 'C' or 'c' to clear the screen and");
    printf(" display available option \n\n");
    printf("Enter + symbol for Addition \n");
    printf("Enter - symbol for Subtraction \n");
    printf("Enter * symbol for Multiplication \n");
    printf("Enter / symbol for Division \n");
    printf("Enter ? symbol for Modulus\n");
    printf("Enter ^ symbol for Power \n");
    printf("Enter ! symbol for Factorial \n\n");
}
void addition()
{
    int n, total=0, k=0, number;
    printf("\nEnter the number of elements you want to add:");
    scanf("%d",&n);
    printf("Please enter %d numbers one by one: \n",n);
    while(k<n)
    {
        scanf("%d",&number);
        total=total+number;
        k=k+1;
    }
    printf("Sum of %d numbers = %d \n",n,total);
}
void subtraction()
{
    int a, b, c = 0;
    printf("\nPlease enter first number  : ");
    scanf("%d", &a);
    printf("Please enter second number : ");
    scanf("%d", &b);
    c = a - b;
    printf("\n%d - %d = %d\n", a, b, c);
}
void multiplication()
{
    int a, b, mul=0;
    printf("\nPlease enter first numb  : ");
    scanf("%d", &a);
    printf("Please enter second number: ");
    scanf("%d", &b);
    mul=a*b;
    printf("\nMultiplication of entered numbers = %d\n",mul);
}
void division()
{
    int a, b, d=0;
    printf("\nPlease enter first number  : ");
    scanf("%d", &a);
    printf("Please enter second number : ");
    scanf("%d", &b);
    d=a/b;
    printf("\nDivision of entered numbers=%d\n",d);
}
void modulus()
{
    int a, b, d=0;
    printf("\nPlease enter first number  : ");
    scanf("%d", &a);
    printf("Please enter second number  : ");
    scanf("%d", &b);
    d=a%b;
    printf("\nModulus of entered numbers = %d\n",d);
}
void power()
{
    double a,num, p;
    printf("\nEnter two numbers to find the power \n");
    printf("number: ");
    scanf("%lf",&a);
    printf("power : ");
    scanf("%lf",&num);
    p=pow(a,num);
    printf("\n%lf to the power %lf = %lf \n",a,num,p);
}
int factorial()
{
    int i,fact=1,num;
    printf("\nEnter a number to find factorial : ");
    scanf("%d",&num);
    if (num<0)
    {
        printf("\nPlease enter a positive number to");
        printf(" find factorial and try again. \n");
        printf("\nFactorial can't be found for negative");
        printf(" values. It can be only positive or 0  \n");
        return 1;
    }             
    for(i=1;i<=num;i++)
    fact=fact*i;
    printf("\n");
    printf("Factorial of entered number %d is:%d\n",num,fact);
    return 0;
}
 
Last edited:


return_type function_name(parameter, list);

"void", in this case indicates that no data is returned from the function. "void" in the parameter list indicates no data is passed to the function. No, you should not remove the void keyword from the code.

What you should do is get yourself a good book on the C Programming Language before diving into code. There are many good books on C for the beginner. You can access one of them, Harbison & Steele online.

Every C Programmer starts with the most famous C program ever, hello.c:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello World!\n");

  return 0;
}

Always post your code in "Code" tags. Look for the "<>" in the icon at the top of the editor when posting a message on this site.
 
return_type function_name(parameter, list);

"void", in this case indicates that no data is returned from the function. "void" in the parameter list indicates no data is passed to the function. No, you should not remove the void keyword from the code.

What you should do is get yourself a good book on the C Programming Language before diving into code. There are many good books on C for the beginner. You can access one of them, Harbison & Steele online.

Every C Programmer starts with the most famous C program ever, hello.c:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello World!\n");

  return 0;
}

Always post your code in "Code" tags. Look for the "<>" in the icon at the top of the editor when posting a message on this site.


Oh, I see..... :D A book, eh.....? That may..... That may help.... XD
 
To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
Code:
# yum groupinstall 'Development Tools'
If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler:

Code:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev
 
To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
Code:
# yum groupinstall 'Development Tools'
If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler:

Code:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

Thanks for the reply..... :3 Except that I'm stuck with my ASUS now, since my Chromebook charger is broken (again)..... T_T Seriously, why does EVERY device I put Linux on break, yet a Surface RT that gets doused in liquid SURVIVE.....? D: I mean, I CAN get Kubuntu to boot up on the ASUS, after Shell Hell (LOTS of Commands), but even then, the Drivers for Wi-Fi and Sound, as well as the internal storage being detected are all non-existent..... :( Of course, I WOULD try OpenSUSE, but, well, the Chromebook charger..... T_T
 
Last edited:
The C code you have posted will not compile on Linux unless you remove the dependency on conio.h - See the #include at line 3:
Code:
#include <conio.h>

Conio.h is a non-standard header file. It is a header from the old Borland C compilers for Windows. It contains various non-standard extensions that provide additional console input/output functions and is not available for gcc.

To remove the dependency on conio, you should do this:
1. Remove the #include at line 3
2. Replace the call to getche at line 27 with a more standard C input function (scanf, getline etc)
3. Recompile

Other than the call to getche, I'm not sure if there are any other conio functions in there. I don't see any others offhand. But if there are more conio functions called in the code, the compiler or the linker will give you an error about an undefined function. If there are any more, you'll have to look up the function the compiler is complaining about and find a standard C function to use as an alternative.

Until you remove the dependency on conio.h, the code you have posted will only compile on a Borland compiler.

EDIT: Also there is the matter of the calls to system("cls") - just noticed these too. These should also be removed from the program for a number of reasons, chiefly for portability and security reasons. I don't have time to go into why system should not be used ATM, but avoid using it at all costs!

You can clear the screen in Linux using console escape codes like this:
Code:
printf("\E[H\E[2J");
This is not a portable solution, but it will clear the screen on a linux terminal!
 
Last edited:
The C code you have posted will not compile on Linux unless you remove the dependency on conio.h - See the #include at line 3:
Code:
#include <conio.h>

Conio.h is a non-standard header file. It is a header from the old Borland C compilers for Windows. It contains various non-standard extensions that provide additional console input/output functions and is not available for gcc.

To remove the dependency on conio, you should do this:
1. Remove the #include at line 3
2. Replace the call to getche at line 27 with a more standard C input function (scanf, getline etc)
3. Recompile

Other than the call to getche, I'm not sure if there are any other conio functions in there. I don't see any others offhand. But if there are more conio functions called in the code, the compiler or the linker will give you an error about an undefined function. If there are any more, you'll have to look up the function the compiler is complaining about and find a standard C function to use as an alternative.

Until you remove the dependency on conio.h, the code you have posted will only compile on a Borland compiler.

EDIT: Also there is the matter of the calls to system("cls") - just noticed these too. These should also be removed from the program for a number of reasons, chiefly for portability and security reasons. I don't have time to go into why system should not be used ATM, but avoid using it at all costs!

You can clear the screen in Linux using console escape codes like this:
Code:
printf("\E[H\E[2J");
This is not a portable solution, but it will clear the screen on a linux terminal!

Oh man..... That's all over my head....... D: I mean, I understand it, but..... I don't even know what the BASH code does..... :( Man, this is what makes me consider giving up Programming, but..... I just can't.......
 
Don't panic, and don't give up; stick with it!

The 'bash code' as you call it is just a sequence of ANSI escape codes that will be interpreted by the terminal when the program is ran. It's not something that anybody would expect someone starting out with C programming to know about.
\E[ denotes the start of an escape sequence, followed by the code.
\E[H will move the cursor to the top left and \E[2J clears the entire screen.

Here's a list of ANSI escape codes:
https://en.wikipedia.org/wiki/ANSI_escape_code

Again, this isn't a portable solution, but should allow the program to work on most, if not all Linux terminals. On Windows it will work in Cygwin, but it won't work in cmd or Powershell as they don't interpret ANSI escape sequences. So typically, escape sequences should not be used. Especially if cross-platform portability is a concern. Usually you'd use a cross-platform 'terminal aware' library like ncurses instead. My suggestion was just a quick and dirty hack to get the code you posted to compile on Linux, heh heh.
 
Last edited:
Don't panic, and don't give up; stick with it!

The 'bash code' as you call it is just a sequence of ANSI escape codes that will be interpreted by the terminal when the program is ran. It's not something that anybody would expect someone starting out with C programming to know about.
\E[ denotes the start of an escape sequence, followed by the code.
\E[H will move the cursor to the top left and \E[2J clears the entire screen.

Here's a list of ANSI escape codes:
https://en.wikipedia.org/wiki/ANSI_escape_code

Again, this isn't a portable solution, but should allow the program to work on most, if not all Linux terminals. On Windows it will work in Cygwin, but it won't work in cmd or Powershell as they don't interpret ANSI escape sequences. So typically, escape sequences should not be used. Especially if cross-platform portability is a concern. Usually you'd use a cross-platform 'terminal aware' library like ncurses instead. My suggestion was just a quick and dirty hack to get the code you posted to compile on Linux, heh heh.

I know..... I won't give up..... I guess the hardest PART is....... Being CREATIVE..... :( Like, what do I want to DO with what I've learned so far.....? What can I CREATE.....? :( Anyway..... I'll check out the Link, and I appreciate what you taught me about that Bash Code..... :) BTW, is that YOU at the Drums.....? :3 I like Drums..... Especially Drum Sticks..... :3 You can eat Sushi with them..... Do you like Sushi.....? :3 Also, I kind of figured it wouldn't work on Windows, except in Cygwin (or if you surreptitiously got your hand s on the NT Kernel..... :D)
 
The creative side of things is something that will come given time and experience. At this stage, you are still learning the basics of C (and programming in general), so it will take some time before things start falling into place and you know enough to be able to start getting properly creative. A lot of the advice I gave in your Python thread is also applicable here!

When first learning C, it took me a while before I became proficient enough to be able to get creative and productive using the language. As with learning Python, you need to find a book or online course/tutorial series that starts you with the basics and gradually builds your knowledge and experience of the language.

With the C family of languages, I started by learning ANSI C (C89/90, which was the most widespread version of the language when I was learning) and then moved on to C++ before eventually getting my first programming job (many years ago). But I haven't done any pure C development in a long while now. 99% of what I do at my current job is C++.

I use dwm as my default desktop environment on my laptop though. dwm is a C-based, tiling window manager for Linux which consists of approx 2000 lines of C code. Any customisations - even simple things like changing colours - requires you to edit the source code and recompile/reinstall it. But it takes no more than a few seconds to rebuild and reinstall. Then it's just a case of logging out and logging back in to see your changes in action! Kinda fun! I have made a few minor customisations to my install and have also tried some other experiments with it from time to time, with varying degrees of success/disaster! heh heh!

And yup, that's me in my 'other' office. I play drums for an unsigned, UK-based technical metal band called Kinasis (links to some vids are on my linux.org profile page if you're interested!). I do quite like sushi (and sashimi!), but it's not my favourite food! And I've never used my drum sticks as chopsticks either, or had the inclination to try. Being a metal drummer, most of my sticks are much larger and heavier than chopsticks. Far too impractical to try to eat with IMO! :)
 
BTW: Here's the code you originally posted with some minor edits to allow it to compile on Linux.
Code:
// Calculator example using C code
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

const char* Key = "Enter the calculator Operation you want to do:";

// Function prototype declaration
void addition();
void subtraction();
void multiplication();
void division();
void modulus();
void power();
int factorial();
void calculator_operations();

// Start of Main Program
int main()
{
  int ch;
  char Calc_oprn;
  // Function call
  calculator_operations();
  while(1)
  {
   printf("\n");
   printf("%s : ", Key);
   scanf("%c", &Calc_oprn);
   switch(Calc_oprn)
   {
    case '+': addition();
      break;
    case '-': subtraction();
      break;
    case '*': multiplication();
      break;
    case '/': division();
      break;
    case '?': modulus();
      break;
    case '!': factorial();
      break;
    case '^': power();
      break;
    case 'H':
    case 'h': calculator_operations();
      break;
    case 'Q':
    case 'q': exit(0);
      break;
    case 'c':
    case 'C':
      // clear the screen using a terminal escape sequence
      printf("\E[H\E[2J");
      calculator_operations();
      break;
    default :
      printf("\E[H\E[2J");
      printf("\n**********You have entered unavailable option");
      printf("***********\n");
      printf("\n*****Please Enter any one of below available ");
      printf("options****\n");
      calculator_operations();
   }

   // Ensure the input buffer has been flushed
   while ((ch = getchar()) != '\n' && ch != EOF);
  }
}
//Function Definitions
void calculator_operations()
{
  printf("\n  Welcome to C calculator \n\n");
  printf("******* Press 'Q' or 'q' to quit ");
  printf("the program ********\n");
  printf("***** Press 'H' or 'h' to display ");
  printf("below options *****\n\n");
  printf("Enter 'C' or 'c' to clear the screen and");
  printf(" display available option \n\n");
  printf("Enter + symbol for Addition \n");
  printf("Enter - symbol for Subtraction \n");
  printf("Enter * symbol for Multiplication \n");
  printf("Enter / symbol for Division \n");
  printf("Enter ? symbol for Modulus\n");
  printf("Enter ^ symbol for Power \n");
  printf("Enter ! symbol for Factorial \n\n");
}
void addition()
{
  int n, total=0, k=0, number;
  printf("\nEnter the number of elements you want to add:");
  scanf("%d",&n);
  printf("Please enter %d numbers one by one: \n",n);
  while(k<n)
  {
   scanf("%d",&number);
   total=total+number;
   k=k+1;
  }
  printf("Sum of %d numbers = %d \n",n,total);
}
void subtraction()
{
  int a, b, c = 0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number : ");
  scanf("%d", &b);
  c = a - b;
  printf("\n%d - %d = %d\n", a, b, c);
}
void multiplication()
{
  int a, b, mul=0;
  printf("\nPlease enter first numb  : ");
  scanf("%d", &a);
  printf("Please enter second number: ");
  scanf("%d", &b);
  mul=a*b;
  printf("\nMultiplication of entered numbers = %d\n",mul);
}
void division()
{
  int a, b, d=0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number : ");
  scanf("%d", &b);
  d=a/b;
  printf("\nDivision of entered numbers=%d\n",d);
}
void modulus()
{
  int a, b, d=0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number  : ");
  scanf("%d", &b);
  d=a%b;
  printf("\nModulus of entered numbers = %d\n",d);
}
void power()
{
  double a,num, p;
  printf("\nEnter two numbers to find the power \n");
  printf("number: ");
  scanf("%lf",&a);
  printf("power : ");
  scanf("%lf",&num);
  p=pow(a,num);
  printf("\n%lf to the power %lf = %lf \n",a,num,p);
}
int factorial()
{
  int i,fact=1,num;
  printf("\nEnter a number to find factorial : ");
  scanf("%d",&num);
  if (num<0)
  {
   printf("\nPlease enter a positive number to");
   printf(" find factorial and try again. \n");
   printf("\nFactorial can't be found for negative");
   printf(" values. It can be only positive or 0  \n");
   return 1;
  }  
  for(i=1;i<=num;i++)
   fact=fact*i;
  printf("\n");
  printf("Factorial of entered number %d is:%d\n",num,fact);
  return 0;
}

Compile and link using gcc like so:
Code:
gcc example.c -oexample -lm

Notes:
Because math.h was included, the -lm used as a parameter to gcc tells the compiler/linker to link with the math library - libmath. The -l flag is used to specify an additional library to link with. m is the name for libmath.

I haven't taken too long cleaning up the code, but it's the bare minimum to allow the code to compile on Linux, without changing the original logic too much. It uses the terminal escape-code hack I mentioned to clear the screen and there's an extra line of code I threw in there to flush the input stream.

It also contains its original bugs - As code examples go; it's not the cleanest, or most robust C code in the world. There is at least one very obvious, major, show-stopping bug left in the code by the original author. A very common mistake and very easily fixed, I'm very surprised the original author missed this! As an exercise, I'll leave you to work out exactly what it is and how to fix it. All I'll say is : Take a look at the division function... Does the term division by zero mean anything to you? Heh heh! KABOOOOM! XD

Should be a relatively simple fix for you to get started with!
 
Try https://www.edx.org/ and signup for a free account. Then take the introduction to computer science referred to as CS50 on the site. As part of the course you will learn to use C to solve problems. It costs nothing and is self paced until end of 2015 ;-) This course is offered by Harvard

Or you can go here and scroll down on the page. There is a ebook C for Linux that should be helpful. http://bookboon.com/en/c-cpp-csharp-ebooks
 

Attachments

  • C_Programming.zip
    1.5 MB · Views: 2,831
Last edited:
... When first learning C, it took me a while before I became proficient enough to be able to get creative and productive using the language. As with learning Python, you need to find a book or online course/tutorial series that starts you with the basics and gradually builds your knowledge and experience of the language. ...
Can I recommend "C A Reference Manual, by Harbison & Steele". There is a down-loadable PDF file. Of course with Linux, you have the "man" command. Even for those using a Windows compiler you can use an online version at die.net.
 
The creative side of things is something that will come given time and experience. At this stage, you are still learning the basics of C (and programming in general), so it will take some time before things start falling into place and you know enough to be able to start getting properly creative. A lot of the advice I gave in your Python thread is also applicable here!

When first learning C, it took me a while before I became proficient enough to be able to get creative and productive using the language. As with learning Python, you need to find a book or online course/tutorial series that starts you with the basics and gradually builds your knowledge and experience of the language.

With the C family of languages, I started by learning ANSI C (C89/90, which was the most widespread version of the language when I was learning) and then moved on to C++ before eventually getting my first programming job (many years ago). But I haven't done any pure C development in a long while now. 99% of what I do at my current job is C++.

I use dwm as my default desktop environment on my laptop though. dwm is a C-based, tiling window manager for Linux which consists of approx 2000 lines of C code. Any customisations - even simple things like changing colours - requires you to edit the source code and recompile/reinstall it. But it takes no more than a few seconds to rebuild and reinstall. Then it's just a case of logging out and logging back in to see your changes in action! Kinda fun! I have made a few minor customisations to my install and have also tried some other experiments with it from time to time, with varying degrees of success/disaster! heh heh!

And yup, that's me in my 'other' office. I play drums for an unsigned, UK-based technical metal band called Kinasis (links to some vids are on my linux.org profile page if you're interested!). I do quite like sushi (and sashimi!), but it's not my favourite food! And I've never used my drum sticks as chopsticks either, or had the inclination to try. Being a metal drummer, most of my sticks are much larger and heavier than chopsticks. Far too impractical to try to eat with IMO! :)

I see what you mean about getting fluent with it..... :) It just takes time..... I'll have to remember your advice when I see all those smart people who can Program an OS that NORAD uses..... :3 I used to live in N.Y. State, and visited the Canada side of Niagara Falls, and LOVED the restaurants there..... :3 Much wow, such atmosphere, max Canadian Quarters on the ground..... :3 Ever heard of ASMR.....? Some of my favorite ASMRtists are from the U.K..... :D Also, I hear you guys have AWESOME Chocolates and Curry..... :3 My stomach always hates me for eating Curry, but I don't care..... :D Also, I haven't heard of ANSI C before..... I'll have to look into that, as well..... :D Once, someone on here told me about Angular..... Maybe one day, since I'm a Red Head, I can make my OWN implementation of Python....... It'll be called Ginger-Python..... :D But yeah, I could eat Sushi all DAY..... :3 Also, isn't Sashimi similar.....? :) I'll check out your Music, too..... :) I can't buy it through iTunes though, since I got locked out of my Account..... :( How about CD's....? Do you have them.....? :) Maybe one day I could make an Open Source SONG....... :3 ^^
 
Can I recommend "C A Reference Manual, by Harbison & Steele". There is a down-loadable PDF file. Of course with Linux, you have the "man" command. Even for those using a Windows compiler you can use an online version at die.net.

Thanks!..... :3 I have Tiny Core in a VM, but that's all for Linux, since my Chromebook charger broke..... D: Hey, the Spell-Checker knows of VIM!..... :)
 
Try https://www.edx.org/ and signup for a free account. Then take the introduction to computer science referred to as CS50 on the site. As part of the course you will learn to use C to solve problems. It costs nothing and is self paced until end of 2015 ;-) This course is offered by Harvard

Or you can go here and scroll down on the page. There is a ebook C for Linux that should be helpful. http://bookboon.com/en/c-cpp-csharp-ebooks

Thank you,, good sir..... :3 Hey, once I get my Program for Hospitals worldwide rolling, wanna' help.....? :)
 
BTW: Here's the code you originally posted with some minor edits to allow it to compile on Linux.
Code:
// Calculator example using C code
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

const char* Key = "Enter the calculator Operation you want to do:";

// Function prototype declaration
void addition();
void subtraction();
void multiplication();
void division();
void modulus();
void power();
int factorial();
void calculator_operations();

// Start of Main Program
int main()
{
  int ch;
  char Calc_oprn;
  // Function call
  calculator_operations();
  while(1)
  {
   printf("\n");
   printf("%s : ", Key);
   scanf("%c", &Calc_oprn);
   switch(Calc_oprn)
   {
    case '+': addition();
      break;
    case '-': subtraction();
      break;
    case '*': multiplication();
      break;
    case '/': division();
      break;
    case '?': modulus();
      break;
    case '!': factorial();
      break;
    case '^': power();
      break;
    case 'H':
    case 'h': calculator_operations();
      break;
    case 'Q':
    case 'q': exit(0);
      break;
    case 'c':
    case 'C':
      // clear the screen using a terminal escape sequence
      printf("\E[H\E[2J");
      calculator_operations();
      break;
    default :
      printf("\E[H\E[2J");
      printf("\n**********You have entered unavailable option");
      printf("***********\n");
      printf("\n*****Please Enter any one of below available ");
      printf("options****\n");
      calculator_operations();
   }

   // Ensure the input buffer has been flushed
   while ((ch = getchar()) != '\n' && ch != EOF);
  }
}
//Function Definitions
void calculator_operations()
{
  printf("\n  Welcome to C calculator \n\n");
  printf("******* Press 'Q' or 'q' to quit ");
  printf("the program ********\n");
  printf("***** Press 'H' or 'h' to display ");
  printf("below options *****\n\n");
  printf("Enter 'C' or 'c' to clear the screen and");
  printf(" display available option \n\n");
  printf("Enter + symbol for Addition \n");
  printf("Enter - symbol for Subtraction \n");
  printf("Enter * symbol for Multiplication \n");
  printf("Enter / symbol for Division \n");
  printf("Enter ? symbol for Modulus\n");
  printf("Enter ^ symbol for Power \n");
  printf("Enter ! symbol for Factorial \n\n");
}
void addition()
{
  int n, total=0, k=0, number;
  printf("\nEnter the number of elements you want to add:");
  scanf("%d",&n);
  printf("Please enter %d numbers one by one: \n",n);
  while(k<n)
  {
   scanf("%d",&number);
   total=total+number;
   k=k+1;
  }
  printf("Sum of %d numbers = %d \n",n,total);
}
void subtraction()
{
  int a, b, c = 0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number : ");
  scanf("%d", &b);
  c = a - b;
  printf("\n%d - %d = %d\n", a, b, c);
}
void multiplication()
{
  int a, b, mul=0;
  printf("\nPlease enter first numb  : ");
  scanf("%d", &a);
  printf("Please enter second number: ");
  scanf("%d", &b);
  mul=a*b;
  printf("\nMultiplication of entered numbers = %d\n",mul);
}
void division()
{
  int a, b, d=0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number : ");
  scanf("%d", &b);
  d=a/b;
  printf("\nDivision of entered numbers=%d\n",d);
}
void modulus()
{
  int a, b, d=0;
  printf("\nPlease enter first number  : ");
  scanf("%d", &a);
  printf("Please enter second number  : ");
  scanf("%d", &b);
  d=a%b;
  printf("\nModulus of entered numbers = %d\n",d);
}
void power()
{
  double a,num, p;
  printf("\nEnter two numbers to find the power \n");
  printf("number: ");
  scanf("%lf",&a);
  printf("power : ");
  scanf("%lf",&num);
  p=pow(a,num);
  printf("\n%lf to the power %lf = %lf \n",a,num,p);
}
int factorial()
{
  int i,fact=1,num;
  printf("\nEnter a number to find factorial : ");
  scanf("%d",&num);
  if (num<0)
  {
   printf("\nPlease enter a positive number to");
   printf(" find factorial and try again. \n");
   printf("\nFactorial can't be found for negative");
   printf(" values. It can be only positive or 0  \n");
   return 1;
  } 
  for(i=1;i<=num;i++)
   fact=fact*i;
  printf("\n");
  printf("Factorial of entered number %d is:%d\n",num,fact);
  return 0;
}

Compile and link using gcc like so:
Code:
gcc example.c -oexample -lm

Notes:
Because math.h was included, the -lm used as a parameter to gcc tells the compiler/linker to link with the math library - libmath. The -l flag is used to specify an additional library to link with. m is the name for libmath.

I haven't taken too long cleaning up the code, but it's the bare minimum to allow the code to compile on Linux, without changing the original logic too much. It uses the terminal escape-code hack I mentioned to clear the screen and there's an extra line of code I threw in there to flush the input stream.

It also contains its original bugs - As code examples go; it's not the cleanest, or most robust C code in the world. There is at least one very obvious, major, show-stopping bug left in the code by the original author. A very common mistake and very easily fixed, I'm very surprised the original author missed this! As an exercise, I'll leave you to work out exactly what it is and how to fix it. All I'll say is : Take a look at the division function... Does the term division by zero mean anything to you? Heh heh! KABOOOOM! XD

Should be a relatively simple fix for you to get started with!

Thanks for that..... :D It's EXTREMELY helpful for a newbie like me to learn Code that's more modern..... :D Not that the old way is BAD....... ;) Divide by ZERO.....? You NEVER divide by Zero..... D: I must fix this, before the Big Rip becomes more than a THEORY.....!!! D:
 
Ever heard of ASMR.....? Some of my favorite ASMRtists are from the U.K..... :D Also, I hear you guys have AWESOME Chocolates and Curry..... :3 My stomach always hates me for eating Curry, but I don't care..... :D Also, I haven't heard of ANSI C before..... I'll have to look into that, as well..... :D Once, someone on here told me about Angular..... Maybe one day, since I'm a Red Head, I can make my OWN implementation of Python....... It'll be called Ginger-Python..... :D But yeah, I could eat Sushi all DAY..... :3 Also, isn't Sashimi similar.....? :) I'll check out your Music, too..... :) I can't buy it through iTunes though, since I got locked out of my Account..... :( How about CD's....? Do you have them.....? :) Maybe one day I could make an Open Source SONG....... :3 ^^

No, I've never heard of ASMR - I've googled the term though:
Autonomous Sensory Meridian Response - Is that what you're referring to?
Didn't really know that this was a thing! Heh heh!

A lot of Americans have told me that chocolate is better in the UK. No idea how true that is. The chocolate varies in quality here - depends on what you buy. Anywhere from 20-25% cocoa for the cheap, low quality stuff, most stuff averages at 40% cocoa, but there are some nice 75%-85% cocoa chocolates available. Personally I am a fan of dark chocolate with chilli!

And yes, we do love our curry over here in the UK. Indian restaurants here sell a wide range of curries, from mildly seasoned/spiced to insanely hot. As in inedibly hot! And there's probably at least one curry house in every single town/village in the country.

On the subject of hot food, there was a local Mexican restaurant a few years ago who sold an insanely hot chilli. I made the mistake of ordering it on a night out. The following morning, whilst extremely hung-over I found out why the restaurant called it 'The Ringer Stinger'. Heh heh, talk about a ring of fire. I was suffering all day after that one! XD

And yes, Sashimi is basically Sushi, just without the rice. Sashimi is just thinly cut raw fish/meat.

RE: Music - Sadly we don't have any CDs available ATM. We are due to self-release our debut album at some point soon. The release date still hasn't been confirmed yet, but there are two old demos available as free downloads from our pages on lastfm and reverbnation.
 
And also, it may be impractical for US to eat with Drum Sticks, but for noobs, they would be PERFECT..... :D Ahhh, I remember when I used to stab my food with Chopsticks, before I got good with them..... :3 Not sure how well I've got the FORM, but I think I do well..... ;) Next idea: Sushimi Linux!!..... :3
 

Members online


Top