LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multiple Errors in C program (beginner) (https://www.linuxquestions.org/questions/programming-9/multiple-errors-in-c-program-beginner-484810/)

circuit_girl 09-18-2006 09:22 PM

Multiple Errors in C program (beginner)
 
I am just learning how to use unix and program in unix. I have programmed before but it has been awhile.
The first thing I am trying to do is run a c program that will excecute unix commands and will display some comments on the screen.

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

main()
{
printf ("Getting something to print out.'\n'");
system ("ls");
system ("pwd"); /*prints working directory*/
system ("history"); /*prints the history of commands*/
return 0;
}

I have figured out that you have to compile and run it with 2 diffent commands.
I can comile it but it will not run I get an error

/TT_DB: No match.
/TT_DB: No match.
/TT_DB: Permission denied.
Badly placed ()'s.
%
What am I doing wrong? PLEASE HELP

Leisy 09-18-2006 10:11 PM

don't close \n in quotes, then it seems to work besides system("history");

btw. the error message Badly placed ()'s gives good clue

Dark_Helmet 09-19-2006 12:18 AM

The code works as-is on my system, albit probably not as intended (referring to the single quotes and the \n Leisy mentioned). I'm willing to bet there's a problem with the commands used to compile and/or run the program. Here's hat I think is going on:
  • You have a script on your system with the same name as the executable you're trying to make
  • The script is located somewhere in your PATH environent variable
  • When you try to run the program (after compiling), you aren't giving the path to the newly compiled executable

To be certain, you need to post what commands you are using to compile the program and the command you're using to run the program.

In case I'm right, here's the customary way to compile and run a program (assuming the source file is named new_prog.c):
Code:

gcc -Wall -o new_prog new_prog.c
./new_prog


circuit_girl 09-19-2006 01:23 AM

I tried using that code, I got the same error messages. I also do not get a little of what you said. the second line that you entered, ".\new_prog" do I put that at the end of the command?

circuit_girl 09-19-2006 01:42 AM

I put all of the command in and it said
gcc: ./file_name: No such file or Directory

thank you for all the help so far

Nylex 09-19-2006 01:53 AM

You use ./filename to run the program, i.e. it's not part of the line you use to compile.

circuit_girl 09-19-2006 03:08 AM

what does the -Wall stand for. I did not understand the man for it

circuit_girl 09-19-2006 03:35 AM

It is working Thanks to everyones help I just have a couple of questions:

1. How come it will not execute with out typing that wall command and what does the -o stand for?

2. What does the ./ stand for when I try to execute it.

3. Why do I have to do it everytime I enter Unix?

Thanks Again

Nylex 09-19-2006 03:43 AM

-Wall gives you all warnings about things that could be wrong with your code. In gcc's man page, there's quite a long list of things that you can be warned about. I couldn't be bothered to read it all, to be honest.

You need to use ./filename if . (i.e. the current directory) is not in the $PATH environment variable. When you run an executable, the shell searches all the directories listed in $PATH and if that executable isn't in those directories, you'll get a "command not found" error. "./filename" just tells the shell to run the executable filename that's in directory . (again, the . stands for the current directory). A similar example would be if you had a directory called Programs in your current directory. If you wanted to run an executable in that directory, you'd use Programs/filename.

jschiwal 09-19-2006 03:47 AM

1. The -Wall options enables warnings. It should compile and run without it however you may miss problems in your code if you don't use it. The -W part is for Warning, and "all" is to enable all warnings. This is easier than manually enabling dozens of warnings.
2. The current directory isn't in your path, so to execute code in the current directory, precede it with ./. to indicate that the program is in the current directory. You don't want to add the current directory "." to your path. This is to prevent executing a hackers version of a common command such as "ls" or "dir" in a world writable directory such as /tmp.


All times are GMT -5. The time now is 10:10 AM.