0 Members and 1 Guest are viewing this topic.
#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 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.
Yup, it's available in C as well. I just forget how...
// This is C++ code extern "C" { //this is C code }
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 }