Hello to everyone! or should I say, "Hello World"?
I am new to C and new to this forum, and this is my first post.

I wrote a simple C program which I called: isitprime.c and generated isitprime.exe.
I can call this program isitprime.exe from the command line with an integer number and the program will tell me if the number is prime. For instance, if I do this in the command line:

isitprime 5

it will tell me that it is a prime number. Of course if the number is not prime it will say so.

Now, I am writing another C program which I will use to ask the user to enter an integer representing the maximum number of primes to check. Let me explain with an example since this is a bit confusing.

The new program is called: genprime.c and its corresponding genprime.exe.

I want to:

1 - Type: genprime 10000
2 - genprime sets a loop from 2 to 10000, skipping the even numbers.
3 - genprime needs to call isitprime n where n is the loop index. In other words, isitprime gets called from genprime.

The question is, how do I call from a C program, another C program passing a parameter from the caller to the target?

Thank you

Recommended Answers

All 7 Replies

Use IPC techniques. In particular, a pipe would be best suited. Of course you're totally over-engineering the problem and making it ten times harder than it should be. :icon_rolleyes:

How about this approach:

Project(Main Task)
                      | 
              -----------------
              |               |
   Code for task 1            Code for task 2

You can try this, instead of actually compiling two programs separately and trying to call one from the other.

exactly...he can simply use a common header, a isPrime.c, a genPrime.c and finally a main.c which calls the two. Inside genPrime.c he can simply call isPrime(n) which is inside isPrime.c. Offcourse he needs prototypes or a common header.


Edit: offcourse,you need to compile the files together. If you are using an IDE like code::blocks or Visual Studio, then make a project as was suggested above and the rest will be taken care of by the IDE.

Actually, isitprime works fine by itself. For example, I can do this on the command line:
isitprime 11 and it returns truth indicating that 11 is a prime number.

But now, instead of typing isitprime 11 and want to the same but from another C program genprime, which I am trying to get to work.

Some literature hinted at the use of either:

syscall
exec
spawnl

But I tried all 3 and can't figure out how to use them.

Thank you

if you were hinted stuffs like syscall, exec and spawnl, then the person giving you the hints is driving you towards IPC, as narue mentioned...

but since you said you are just a beginner, i think its not required (its also an overkill for your purpose)

Thanks to everyone who took their time to answer my post.
I learned couple things from this post:

1 - Although what I am trying to do appear to be very simple, it takes more than just a one line of code. I am used to simplicity in Ruby and thought it was the same in C, although Ruby is an interpreted language.

2 - I actually know how to do it within one single program, but as a learning exercise, I wanted to split it into two, since I wanted isitprime to worry just about the primality of a number.

3 - IPC can be used. However, I rather use IBM MQSeries, which is the actual reason why I am playing with C in the first place and I will have my inter program communication. Actual MQSeries I think might be simpler than IPC in C.

So, again, thank you all for your suggestions. I am sure I'll be back with other questions since I just started to play with C.

Kind regards

PS: It is now snowing here, in NY. Yes, I know, this has nothing to do with C programming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.