1
  1. Hi there, I am trying to make a C program that will print herself source code.

  2. I am assuming that the source code file and the execute file are in the same directory without any other .c files.

  3. Well I have tried many codes, and it didn't worked out.

  4. The error cause because the order of the commands (if it is windows command first it passed in windows OS and if it is a linux command first it passed in linux OS).

  5. I am trying to ignore fopen and other functions and to use only simple OS commands.

  6. Here is the code I have tried:

    code working on windows:


/* C library statement */

#include <stdlib.h>

/* main program */

int main()
{/* start of main */

    int windows = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
 * so if unix value is -1 we are not running unix system.
 * if we are not running unix system we will execute windows commands.
 */

    windows = system("type *.c"); /* windows command to print file text */
    if(windows == -1) /* if windows command failed (and the specific file is exists) then we are not running windows operation system */
        system("cat *.c"); /* unix command to print file text */
    return 0;

}/* end of main */

code working on linux:

/* this program will print herself source code by using simple linux or windows commands */

#include <stdlib.h>

/* main program */

int main()
{/* start of main */

    int unix = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
 * so if unix value is -1 we are not running unix system.
 * if we are not running unix system we will execute windows commands.
 */

    unix = system("cat *.c"); /* unix command to print file text */
    if(unix == -1) /* if unix command failed (and the specific file is exists) then we are not running unix operation system */
        system("type *.c"); /* windows command to print file text */
    return 0;

}/* end of main */

well the problem is that linux recognize type as a bash command and windows is not compiling the cat command

10
  • 1
    Use standard file IO functions to make the program portable (fopen, fread and friends) Commented Jun 7, 2021 at 14:27
  • 1
    Good luck: stackoverflow.com/questions/142508/… Commented Jun 7, 2021 at 14:27
  • 1
    The term for the type of program described in #1 is "quine. " Commented Jun 7, 2021 at 14:31
  • 1
    You should post code that works as an answer, not edit into the question post. Commented Jun 7, 2021 at 16:09
  • 1
    @Armali sorry I will fix that out, I am new in this forum. Commented Jun 7, 2021 at 16:18

2 Answers 2

2

I Made This Code Working, Can Be Compiled and Run in Both OS

#include <stdlib.h>

/* defining variables to check if what OS we are running in */

#if defined (_WIN32) || defined (_WIN64) /* if we are running win 32bit or win 64 bit */
    #define HAVEWIN 1 /* defined we are running at windows OS */
    #define HAVEUNIX 0 /* defined we are not running at unix OS */
#elif defined (__unix__) /* if we are running a unix OS */
    #define HAVEUNIX 1 /* defined we are running at unix OS */
    #define HAVEWIN 0 /* defined we are not running at unix OS */
#endif /* end of the OS checks */

/* main program */

int main()
{/* start of main */

    if(HAVEWIN == 1) /* if we are running on windows OS */
        system("type *.c"); /* use windows command to print file text */
    else if(HAVEUNIX == 1) /* if we are running on unix OS */
        system("cat *.c"); /* use unix command to print file text */
    return 0;

}/* end of main */
Sign up to request clarification or add additional context in comments.

1 Comment

You should use #if instead of if for platform specific code.
0

The symbol __FILE__ always expands to the name of the current file. The following code will print the source code location:

#include <stdio.h>

int main()
{
  printf("%s\n", __FILE__);
  return 0;
}

So you should just then just add code to dump its contents. But don't use system() calls, as this is not portable and very inefficient.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.