hai ,
I am using turbo c compiler,
the system() function always return -1.
i can't run any dos command using system() function
i can't use system() properly
please help me !!!
ex:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i ;
i=system("dir");
printf("%d",i);
getch();
}

Short answer: system("cmd /c dir") Long answer: system() expects an executable. dir is not, it is a builtin.

Your code runs fine on my Dev C++. What OS you are on? Try running on some other compiler.

Comments
Turbo-C -- what OS do you think he's on?

Although one can run turboc on an OS like windows 7 too, using a software like Dosbox.
But I think it is more probable of him using XP or something, so may be my bad.

Some repairs.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i ;
  i=system("dir");
  printf("%d",i);
  getchar();
  return 0;
}

This is correct.

@nezachem
system() expects either an executable or a shell builtin.

The system() function can fail for a number of reasons. Be sure to read the man page (which is correct for DOS also).

You can see if the shell is available by calling i=system(NULL); . If it is available, check to make sure your shell is cmd.exe or command.com.

All that said, I'm not sure this is what you really want. Are you trying to get the names of files in the current working directory? You may want to google around the "readdir" family of functions -- they are fairly portable even if you have to #include different headers on DOS/Windows.

Hope this helps.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<errno.h>
void main()
{
int i;
i=system("dir");
perror("");
printf("%d",i);
getch();
}

i just used perror() to find the error ,it shows that "memory allocation error, cannot start command "
why?

ya these are diff.actually turbo c/c++ is no more upadated, as borland closed the project. so it don't support the ANSI C features, System() function is the part of ansi c. so use Dev C++, GCC or VS both of these supports ANSI C features.

actually turbo c/c++ is no more upadated, as borland closed the project. so it don't support the ANSI C features, System() function is the part of ansi c.

Turbo C/C++ is an ancient compiler for an outdated operating system, but it still supports C89 well enough that the system() function is available[1]. I'd certainly agree that unless you're supporting legacy code you should use a modern compiler, but claiming that system() "doesn't work" in Turbo C++ is just plain misinformation.

[1] Unless you go out of your way to find something like version Turbo C 1.0, where it was released early enough before C89 that I can't confidently talk about conformance. But I'm not aware of any way to acquire Turbo C version 1.0, so at the very least we're talking about Turbo C v2.0 or Turbo C++, both of which are reasonably close to conformant with C89.

This article has been dead for over six months. Start a new discussion instead.