Can I mix C and C++ in a single program [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
can i write code with mixing of c and c++ header file in a programming language?
SUPPOSE
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout <<"Hello world"; << endl;
printf("",);
}

What you have is not a mix of C and C++ code. It's C++ code with some elements written using C-style coding, but not actual C language.
You cannot mix C and C++ in a single source file. You can combine C and C++ files in the same program, but each source file must be in one language.
A header file can be written such that the code it contains can be used as both C and C++ code. When included in a C fie it is treated as C and when included in C++ file it is treated as C++. Most headers of C libraries are written this way today. One must do some (small) extra work on a C header towards C++ compatibility. A C header that ignores existence of C++ is likely to be incompatible with C++.

Yes, you can
/* code.cc */
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
printf("Hello world\n");
}
However, you need to use c++ to compile the code
c++ -o code code.cc
./code
Hello world
Hello world
In case described by François you need to go slightly different way
/* code_c.h */
extern "C" {
void printMessage();
}
Code that needs to be compiled using C
/* code_c.c */
#include <stdio.h>
void printMessage() {
printf("Hello world!\n");
}
C++ code
/* code.cc */
#include <iostream>
#include "code_c.h"
using namespace std;
int main()
{
cout << "Hello world" << endl;
printMessage();
}
Gluing all together
cc -c code_c.c
c++ -c code.cc
c++ -o code code.o code_c.o
./code
Hello world
Hello world!
Crazy mode [on]
In fact, you can even "mix" C++ with "bash" ;)
code_c.c
echo =1/*"beat that ;)" | tail -c13
#*/;int fun(){return printf("beat that ;)\n");}
code.cc
extern "C" int fun();
int main() { return fun(); }
compilation and execution
> c++ -c code.cc
> cc -c code_c.c
> c++ -o code code.o code_c.o
> ./code
beat that ;)
> bash ./code_c.c
beat that ;)
Crazy mode [off]

Related

Compile C++ Code Using The Terminal Directly Without Save File.cpp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I need to compile C++ code directly in the terminal or CLI without saving the file
When is use the below way, It shows me an error.
gcc -x c - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
You are trying to compile a C++ program using a C compiler.
This works:
g++ '-xc++' - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
If you are trying to run a C++ program from a source,
this works:
g++ -xc++ - && ./a out <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof

Problem with the most simple program in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've encountered an error with the hello world program in c++ vscode, it doesn't print out Hello World also warns me the i don't have gcc
#include <iostream.h>
using namespace stl;
integer main()
{
output << "Hello World"
return 0;
}
The source code won't compile, here is a working hello world program:
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
In addition to this, please make sure you have a compiler.
Looks like u don't have a compiler to compile the code, consider installing gcc or any other c++ compiler first. Your source code also seems to have some syntax errors, try this hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;
}

Following code is not working as expected in DEV C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following code is not working in DEV C++:
#include <iostream>
using namespace std;
void main()
{
cout << "Hello world";
}
How can I do this?
Also, tell me if I can get almost all the functionality of Turbo C++ in Dev C++. Also, tell me if I can easily switch to DEV C++ if I know a moderate amount in Turbo C++. I just want to use C++ with Python 3.x for console applications including GUI.
From what I see in your initial question (printf works while ostream doesn't) the problem may be in buffering. Try that:
std::cout << "Hello world" << std::endl;
or, if you wish to avoid a newline:
std::cout << "Hello world";
std::cout.flush();
There is a difference between streams and printf: the streams are not outputing the data immediately but buffer that to optimize the performance. This however means that in some environments for the small outputs like yours it may stuck forever waiting for more output.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world";
return 0;
}
I used int main instead of void main.

c++ : invalid operands to binary expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}

Can This Code Compile? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ void return type of main()
What is the proper declaration of main?
Simple question, really.
My friend and I are perusing the Powerpoint slides of a professor we are supposed to be hearing next semester. It will be a Java course. For some reason, he has this C++ code snippet
#include <iostream.h>
main ()
{ cout << "Hello, World\n"; }
I have told my friend, "No, this won't work with any modern C++ compiler."
My question is now, can this compile at all?
It could, sure.
Consider, for example, if <iostream.h> was a header with the following contents:
#include <iostream>
using std::cout;
#define main int main
That is not standard C++. The list of issues is quite long for a piece of code that short... probably because it comes from ages ago and an old non-conforming compiler.
The proper name of the include header is #include <iostream>, the .h was dropped during the ANSI standarization.
Types must be explicitly stated in C++. You cannot declare a function without return type and get a default int (that is C, not C++).
The standard signatures for main are: int main(), and int main( int argc, char** ) (implementations can provide extra arguments, but the return type must be int)
cout is defined inside the std namespace and cannot be used without qualification unless you add a using.
The equivalent code in proper C++ would be
#include <iostream>
int main() {
std::cout << "Hello world\n";
}
This will compile, though any decent compiler should raise a warning. Since you're using #include <iostream.h>, the compiler assumes that this is old code and compiles it in backwards-compatible mode. On my machine, gcc says:
In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
from oldcpp.cpp:1:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header for C++
includes, or <iostream> instead of the deprecated header <iostream.h>.
To disable this warning use -Wno-deprecated.
But it still compiles it fine. On running the code, I get exactly what is expected: i.e.:
Hello, World
Is this even worth questioning? Why write corner-case C/C++? What's wrong with keeping with the standards? main() is the entry point of the program, and the OS expects a return code from the program. Now there are arguments whether void main() is acceptable, but why even defend it? Just use int main() and be a happy dude that writes good code:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!\n";
return 0;
}
I start every program with this (without the HW! then) and never ever had any issues.