Thread: Execute a C program inside another C program in linux

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    33

    Execute a C program inside another C program in linux

    Lets say I want to run x.c from a.c

    here a.c

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    
    char x[5];
    system("x");
    return 0;
    
    }

    x.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int score;
    printf("please enter marks:");
    scanf("%d",&score);
    
    
    if(score>60)
    {
    printf("pass");
    }
    else 
    {
    printf("fail");
    }
    return 0;
    }

    So both files under the same directory.
    And if I compile them it turns into a.out
    When I execute the command ./a.out here is what I get in the terminal

    PHP Code:
     sh1xnot found 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    If a.c is compiled as a.out, what did you compile x.c as?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Quote Originally Posted by Salem View Post
    If a.c is compiled as a.out, what did you compile x.c as?
    Actually both compiles as a.out by default.
    PHP Code:
    GCC a.
    compiles as a.out
    Don't show error
    PHP Code:
    ./a.out 
    executes a.c
    PHP Code:
    GCC x.
    compiles as a.out replacing the previous a.out
    PHP Code:
    ./a.c a.out 
    then executes x.c

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well you have to give one of them another name.

    Then do something like
    System ("./subprogram")
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Done. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program won't execute
    By rayne117 in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2009, 07:16 AM
  2. Replies: 0
    Last Post: 04-08-2009, 04:23 PM
  3. Execute another program
    By Revan-th in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2006, 09:53 PM
  4. Can't execute my program
    By bahruddina in forum C Programming
    Replies: 4
    Last Post: 07-13-2004, 10:19 PM
  5. FAQ : running program inside program (spawn)
    By nipun in forum C Programming
    Replies: 3
    Last Post: 06-13-2004, 02:30 PM

Tags for this Thread