System calls for the Hello World program in Python vs. C
Calling C's hello world from Python - Stack Overflow
C++ user vs Python user
python - C++ "Hello World" program that calls hello.py to output the string? - Stack Overflow
Videos
Hi,
I was using the strace command to trace the system calls that print the "Hello, World!" string on the screen using a piece of code that I wrote in Python and I did the exact same thing for a piece of code that I wrote in C. The results are ridiculously different. My Python strace output has 732 lines whereas my C strace output has only 33 lines. Can someone explain why there's such a HUGE difference? I understand that my Python code gets interpreted and my C code gets compiled to the native machine code but does that justify this huge disparity in the strace outputs?
>>> import subprocess
>>> subprocess.check_output("./a.out")
'Hello World!\n'
Regarding your second question, you could also just call gcc using subprocess. In that case, call is probably the right thing to use, since you presumably want to check for failure before running a.out.
Here's a full script:
import subprocess
status = subprocess.call(["gcc","hello.c"])
if status == 0:
print(subprocess.check_output("./a.out").decode('utf-8'))
else:
print("Failed to compile")
hello.c:
#include "stdio.h"
int main() {
printf("Hello world!");
}
Output: Hello world!
If you compile the C script (say, to a.out):
#include <stdio.h>
int main(void) {
printf("Hello, World! \n");
return 0;
}
You can easily call it by using CTypes:
# hello.py
import ctypes
beach = "./a.out"
seashell = ctypes.cdll.LoadLibrary(beach)
seashell.main()
Calling the following Python script will call your main() in your C script:
$ python hello.py
Hello, World!
You can subprocess and capture the output using, for instance, communicate, but there's no need to spawn a shell to accomplish what you're after.
I am getting tired of people saying Python is so easy. That Python is the easiest, when I am actually unsure if that truly is the case
If one compares the amount of stuff needed in a code to write "Hello world" to a terminal, or even write a list to a terminal, Python is one of the easiest. For loops and while loops are also quite intuitive in Python
The thing is
I feel we are saying Python is easy because it is easy to use it to print Hello world, while the Python class functions are a nightmare
And then we say C++ is insanely difficult because there is so much content in C++ that is possible to learn and it takes a lifetime to master like all of C++. Yet, we call C easier than C++, yet like many C programs can work with a C++ compiler without too much work
Yes, C and C++ needs several lines for hello world in a terminal. But again, why are we judging a programming language for ease of saying "Hello world"?
The while loops are like really similar in Python and C++. About the same complexity and difficulty. I admit I don't yet know how complex and difficult classes are in C++, so maybe there is where this supposed super-difficulty lies?
tl;dr: If you go past the "Hello world" level, Python and C++ are about equally difficult imo
Edit and clarifications: So a few things I want to clarify. Yes, I am a beginner in C++. Well spotted. But I am finding it quite easy to learn, although that could be because of my experience with python. And my use of programming has often
Also I didn't mean to say that C++ is easier than Python. What I mean is that they have a similar level of difficulty. I actually do think C++ is slightly more difficult than Python, but only like 5-10% and not like 700% harder as some people seems to apply at times. I expected a super difficult time learning C++, but so far it seems pretty easy compared to my expectations
Last point is that I have always been a bit odd. Maybe my brain is simply well suited for learning C++. Everyone are unique
Edit 2:
I am beyond "hello world". I can write an windowed application using Pygame for Python and SDL for C++. I have some idea of what .h files are. In pythonYpygame I have made simple games (simple because classes are sometimes very much needed for more complex stuff) and cool illustrations based on mathematical functions. Because of the stupidly slow performance of these, I decided to learn C++. Are the windows in C++ empty, except a background colour? Yes. Is that going to change today? Hopefully. While I am very much a beginner and I won't hide that fact, I have written more than "hello world".