Author Topic: C/C++ Bad Habit  (Read 3761 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
C/C++ Bad Habit
« on: February 18, 2011, 05:28:18 pm »
Hey guys,

I just had this doubt while learning C++:

Code: [Select]
#include <iostream>
#include <stdio.h> // C Library
#include <string>

using namespace std;

int fibonacci(int x)
{
if (x==0)
{
return 0;
}
else if (x==1)
{
return 1;
}
else
{
return fibonacci(x-1)+fibonacci(x-2);
}

}

void myfunc() //void functions have no 'return'
{
cout << "Hello multiple functions" << endl;
}

string dispString(string x)
{
return x;
}

int add(int a,int b)
{
return a+b;
}

int main ()
{
  cout << fibonacci(4) << endl; //Gets 4th number of fibonacci sequence
  myfunc(); //This executes myfunc()
  printf("Enter first number: ");
  int firstNumber;
  cin >> firstNumber;
  printf("Enter second number: ");
  int secondNumber;
  cin >> secondNumber;
  printf("%i",add(firstNumber,secondNumber)); //This returns the sum of the two input numbers before
  cout << dispString("\nWoah Woah\n"); // This prints dispString(string)
  printf("Hello World\n"); // 'Hello World' in C

  string s; //Declares string
  cout << "\nEnter your name: "; //Asks for name input
  cin >> s; //Gets input and stores it in variable s
  cout << "\nYour name is: " << s; //Displays variable s

  system("pause>nul"); // system("pause"); but without the text (null)
  return 0; //main returns 0
}

I started C++ half an hour ago and was wondering... Is using C in the middle of C++ a bad habit? Notice what I'm doing above.

Thanks.

Offline z80man

  • Casio Traitor
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 977
  • Rating: +85/-3
    • View Profile
Re: C/C++ Bad Habit
« Reply #1 on: February 18, 2011, 06:09:16 pm »
I usually stick to only the C++ STL, but sometimes a function might be easier to use in the C library, and for sections that require absolute speed I will write that in asm. It does make your code a little unclear when you have both iostream and stdio, but it will not cause errors when you compile.

List of stuff I need to do before September:
1. Finish the Emulator of the Casio Prizm (in active development)
2. Finish the the SH3 asm IDE/assembler/linker program (in active development)
3. Create a partial Java virtual machine  for the Prizm (not started)
4. Create Axe for the Prizm with an Axe legacy mode (in planning phase)
5. Develop a large set of C and asm libraries for the Prizm (some progress)
6. Create an emulator of the 83+ for the Prizm (not started)
7. Create a well polished game that showcases the ability of the Casio Prizm (not started)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C/C++ Bad Habit
« Reply #2 on: February 19, 2011, 05:28:10 am »
I usually stick to only the C++ STL, but sometimes a function might be easier to use in the C library, and for sections that require absolute speed I will write that in asm. It does make your code a little unclear when you have both iostream and stdio, but it will not cause errors when you compile.

Assembly in the middle of C++? I gotta learn that now!

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: C/C++ Bad Habit
« Reply #3 on: February 19, 2011, 10:18:48 am »
Yup, it's available in C as well. I just forget how...
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C/C++ Bad Habit
« Reply #4 on: February 19, 2011, 12:37:40 pm »
Yup, it's available in C as well. I just forget how...

I just googled and found out. Looks pretty decent ;D

Ashbad

  • Guest
Re: C/C++ Bad Habit
« Reply #5 on: February 20, 2011, 06:09:49 pm »
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:

Code: [Select]
// This is C++ code
 extern "C" {
          //this is C code
 }
« Last Edit: February 20, 2011, 06:10:12 pm by Ashbad »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C/C++ Bad Habit
« Reply #6 on: February 21, 2011, 07:33:38 am »
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:

Code: [Select]
// This is C++ code
 extern "C" {
          //this is C code
 }

Thanks much :D