Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #169015

Re: Interesting case that I can't workout!!!!

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Interesting case that I can't workout!!!!
Date 2023-01-24 13:32 -0800
Organization None to speak of
Message-ID <874jsfei0e.fsf@nosuchdomain.example.com> (permalink)
References <tqnotu$2b1mt$1@paganini.bofh.team> <87lelsea8j.fsf@nosuchdomain.example.com> <tqpd6s$2gdku$1@paganini.bofh.team> <878rhrelsd.fsf@nosuchdomain.example.com> <tqpfhe$2glei$1@paganini.bofh.team>

Show all headers | View raw


Jack <invalid@invalid.net> writes:
> On 24/01/2023 20:10, Keith Thompson wrote:
>> Jack <invalid@invalid.net> writes:
>>> On 24/01/2023 06:07, Keith Thompson wrote:
>>>> If the executable is in your current directory (and you're running it
>>>> from a command prompt, or from PowerShell, or something similar), it's
>>>> best to explicitly specify the directory: `.\program` or `.\program.exe`
>>>> (on a Unix-like system it would be `./program`).
>>> Yes it works by typing .\program but what amazes me is that why other
>>> programs in the same folder runs just by typing "program"? What is so
>>> special about running shift operator?
>>>
>>> The same program compiled using gcc or clang runs like before. It must
>>> be something in Visual Studio that stops running the program. I will see
>>> if Microsoft developers can verify this. I will post the question on
>>> their forum so somebody can try it.
>>>
>>> Thanks for the suggestion.
>> Is the name of the program literally "program"?  When asking for help
>> with a problem, it's important to show *exactly* what you did.  The
>> specific name of the executable matters.  For example, if it's
>> "dir.exe", then typing "dir" will probably run the system's directory
>> listing command rather than your program; ".\dir" would execute your
>> program.  If it's "btfsplk.exe", then there probably isn't a system
>> command with the same name.
>>
>> When you type "foo" at a command prompt, the system searches your path
>> (%PATH% on Windows, $PATH on Unix-like systems) for an executable name
>> "foo" (or "foo.exe" or "foo.bat" et all on Windows).  The current
>> directory is likely at the *end* of your search path.
>>
>> It's very likely that your program has a name that happens to match an
>> existing command, and the system executes that existing command rather
>> than your program.  You should get into the habit of typing ".\foo"
>> ("./foo" on Unix-like systems) when running a program "foo" in the
>> current directory.
>>
>> And you haven't told us how you're executing the program.  cmd.exe?
>> PowerShell?
>
> I am running from the same directory as the executable file. The name of 
> the program is "shift.exe". I used "program.exe" and "program" to 
> simplify the matter but obviously it created more questions.

You didn't answer my question, but you appear to be running cmd.exe, not
PowerShell.

> On a windows system the OS first looks for the executable file in the 
> same folder as from where the cmd is run.

That does not appear to be correct.  On my Windowss 10 system, %PATH%
ends with a semicolon, which if I understand correctly means that it
searchs the current directory *last*.

>                                           In my case, I have a folder 
> called "E:\CmdLine\C_cpp" and the output is:
>
> E:\CmdLine\C_Cpp>.\shift
> Value 15 = 15
> doubled num1 = 30
> ShiftFour num1 = 240
>
> When the same program is compiled in GCC or clang then I can run them 
> like so:
>
> E:\CmdLine\C_Cpp>clang -o shift3.exe shift.c
>
> E:\CmdLine\C_Cpp>shift3
> Value 15 = 15
> doubled num1 = 30
> ShiftFour num1 = 240
>
> shift3 is the executable file I created to avoid over writing the file 
> created in Visual Studio. The clang command is self explanatory, I hope. 
> GCC is almost the same except the file is called shift2.exe. I hope this 
> helps.

This has nothing to do with which compiler you're using, or even which
language you're using.  It's entirely about *the name of the
executable file*.

"shift" is a built-in command in cmd.exe (and not in PowerShell).  If
you type "shift" at a prompt, it will execute that built-in command
without searching %PATH%.  There is no system-provided "shift.exe".
(Type "shift /?" for more information on the built-in "shift" command.)

Typing "shift.exe" without the leading ".\" should also run your
program, since the built-in "shift" command doesn't have a ".exe"
suffix.  But I don't recommend that solution, since you'll still have
the same problem if you have an executable whose name matches the name
of a system executable in your path.

When you compiled with gcc or clang *and used a different name for the
executable*, the behavior changed, because there is no built-in command
or system-provided executable named "shift3" or "shift3.exe".

Again, this has nothing to do with C; it's only about the name of your
executable.  And you can completely avoid this issue by cultivating the
habit of preceding the name of an executable in the current directory
with ".\".

(On Unix-like systems, it's generally recommended to remove ".", the
current directory, from your search path, precisely to avoid this kind
of problem.  It's probably a good idea to do the same on Windows, but
the system default is to have the current directory at the end of
%PATH%.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Interesting case that I can't workout!!!! Jack <invalid@invalid.net> - 2023-01-24 05:00 +0000
  Re: Interesting case that I can't workout!!!! Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-23 22:07 -0800
    Re: Interesting case that I can't workout!!!! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-23 22:13 -0800
    Re: Interesting case that I can't workout!!!! Jack <invalid@invalid.net> - 2023-01-24 20:00 +0000
      Re: Interesting case that I can't workout!!!! Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-24 12:10 -0800
        Re: Interesting case that I can't workout!!!! Jack <invalid@invalid.net> - 2023-01-24 20:34 +0000
          Re: Interesting case that I can't workout!!!! David Brown <david.brown@hesbynett.no> - 2023-01-24 21:51 +0100
            Re: Interesting case that I can't workout!!!! Jack <invalid@invalid.net> - 2023-01-24 21:21 +0000
              Re: Interesting case that I can't workout!!!! David Brown <david.brown@hesbynett.no> - 2023-01-25 00:09 +0100
          Re: Interesting case that I can't workout!!!! Ike Naar <ike@sdf.org> - 2023-01-24 21:12 +0000
            Re: Interesting case that I can't workout!!!! Jack <invalid@invalid.net> - 2023-01-24 21:28 +0000
              Re: Interesting case that I can't workout!!!! Vir Campestris <vir.campestris@invalid.invalid> - 2023-02-03 20:56 +0000
                Re: Interesting case that I can't workout!!!! gazelle@shell.xmission.com (Kenny McCormack) - 2023-02-03 21:06 +0000
                Re: Interesting case that I can't workout!!!! Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2023-02-03 22:18 +0000
                Andy or Vi{r,c} (Was: Interesting case that I can't workout!!!!) gazelle@shell.xmission.com (Kenny McCormack) - 2023-02-04 00:27 +0000
                Re: Interesting case that I can't workout!!!! Vir Campestris <vir.campestris@invalid.invalid> - 2023-02-06 12:04 +0000
          Re: Interesting case that I can't workout!!!! Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-24 13:32 -0800
      Re: Interesting case that I can't workout!!!! Phil Carmody <pc+usenet@asdf.org> - 2023-01-25 13:06 +0200
        Re: Interesting case that I can't workout!!!! Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-25 11:01 -0800

csiph-web