-1

I am using visual studio 2017 .

First I wrote the following code :

void main()
{


printf("abcdefgh %d hjhjh %d", 5, 6);

getch();



}

It ran perfectly fine .

But after that I modified the code to the following :

void main()
{
char abc[100];

strcpy_S(abc, "premraj");
    printf("%s", abc);

printf("abcdefgh %d hjhjh %d", 5, 6);

getch();



}

But now I am getting an error with getch stating that "'getch' undefined, assuming extern returning int"

But this new code has been built on the existing code which recognized getch perfectly , how can it not recognize getch the second time ?

I checked out the following question :

getch() is working without conio.h - how is that possible?

which also carried a similar problem but here with modifications only I got this error .

There is an informative answer there by user named "Fatal Error" but still I would like to know about this intriguing phenomenon that is coming in after modifications . What can be the reason behind this ?

P.S : The following was my header file declarations for the first time :

#include <stdio.h>

and the following for the second time :

#include <stdio.h>
#include <string.h>
  • 2
    Both [the Windows `strcpy_s` function](https://msdn.microsoft.com/en-us/library/td1esda9.aspx) and the [standard C `strcpy_s`](http://en.cppreference.com/w/c/string/byte/strcpy) (both with a *lower-case* `s` at the end), have one more argument than you provide. That means you have [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior) as you don't pass enough arguments to the function. With UB all speculation of the behavior of your program becomes useless. – Some programmer dude Apr 29 '18 at 16:58
  • 2
    However, do you include the correct header file for [`_getch`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch)? *Always* include the header files of the functions, structures, etc. that you use. – Some programmer dude Apr 29 '18 at 17:02
  • Are you asking what the error means, or are you asking why you got the error under circumstance A but yet not, with seemingly identical code, under circumstance B? – Steve Summit Apr 29 '18 at 17:04
  • @Someprogrammerdude : So the first time run can also be explained as an undefined behavior ? – Kanishk Viman Apr 29 '18 at 17:05
  • 1
    @SteveSummit : Yes I am asking why there was no error in the first circumstances but there were in the second – Kanishk Viman Apr 29 '18 at 17:06
  • OT: It's `int main(void)` , at least. – alk Apr 29 '18 at 17:14
  • @someprogrammerdude -- OP is asking about a compile error, not a runtime error. UB at runtime does not seem relevant. – MFisherKDX Apr 29 '18 at 17:17
  • I don't understand why the compiler did not complain about `strcpy_S(abc, "premraj");` particularly because you `#include ` so the compiler *knows* the argument is missing. Are you sure you don't have "finger trouble" with your versions? Also the function is `strcpy_s` (lowercase) so have you posted *actual* code or *romantic* code? – Weather Vane Apr 29 '18 at 17:35
  • @WeatherVane : What does finger trouble mean ? – Kanishk Viman Apr 29 '18 at 17:36
  • It means "muddled" as we can all be at times for no apparent reason. Like typing the wrong keys or command, using the wrong file - finger trouble. – Weather Vane Apr 29 '18 at 17:37

1 Answers1

1

Once upon a time, if you called a function which the compiler had never heard of, like this:

#include <stdio.h>
int main()
{
    int x = foo();
    printf("%d\n", foo);
}

Anyway, if you did that, the compiler quietly assumed that foo() was a function returning int. That is, the compiler behaved just as if you had typed

extern int foo();

somewhere before you called foo.

But in, I think, C99, the language was changed. It was no longer legal to call a function you had not explicitly declared. Because there was lots and lots of code out there that was written under the previous set of rules, most compilers did not immediately begin rejecting the old-style code. Some continued to quietly assume that unrecognized functions returned int. Others -- like yours -- began noisily assuming that unrecognized functions returned int, emitting warnings along the lines of "'foo' undefined, assuming extern returning int".

It sounds like your question is that some time ago, your code containing calls to getch() was accepted without warning, but today, you're getting the warning "'getch' undefined, assuming extern returning int". What changed?

One possibility is that your code changed slightly. If your code used to contain the line

#include <conio.h>

somewhere, that file would have contained a declaration along the lines of

extern int getch();

and this would have goven the compiler the declaration that it needed, and you would not have gotten the warning. But if today your code does not contain that #include line, that explain why the warning started cropping up.

It's also possible that your compiler has changed somehow. It's possible you're using a new version of the compiler, that's more fussy, that has gone from quietly assuming, to normally assuming. Or, it's possible that your compiler options have changed. Many compilers can be configured to accept different variants of the language, corresponding to the different versions of the language standards that have been released over the years. For example, if some time ago your compiler was compiling for language standard "C89", but today, it's doing "C99" or "C11", that would explain why it's now being noisy with this warning.

The change in the compiler could be a change in the defaults as configured by a system administrator, or a change in the way you're invoking the compiler, or a change in your project's Makefile, or a change in the language settings in your IDE, or something like that.

A few more points:

  • getch is not a Standard C function; it's specific to Windows. Your program would be more portable, in general, if you didn't use it. Are you sure you need it? (I know what it's for; what I don't know if there's some other way of keeping your program's output window on the screen after if exits.)
  • You should get in the habit of declaring main() as int, not void. (void will work well enough, but it's not correct, and if nothing else, you'll get lots of negative comments about it.)
  • I think there's something wrong with your call to strcpy_S, too,
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Thanks a lot for this awesome answer . But I haven't used conio.h anywhere and this changes were done in an interval of two -three minutes and I used the same key combinations to compile and run and did not click on any other options . So can we still speculate a compiler change ? – Kanishk Viman Apr 29 '18 at 17:35
  • @KanishkViman you have, or should have, because `getch();` is declared in `conio.h`. Moreover VC compiler will complain that you didn't use `_getch()` unless you take steps to prevent it. Are you looking at compiler warnings? – Weather Vane Apr 29 '18 at 17:41
  • @WeatherVane : The point is I have not used conio.h in any of these two attempts . But everything ran fine for the first time . I need to understand how this modifications can lead to compiler change ? – Kanishk Viman Apr 29 '18 at 17:45
  • @KanishkViman It's impossible for me to say exactly what happened. In my answer, I focused on facts I know, and facts you should know, with a limited amount of speculation about what might have happened. The answer you're seeking may be interesting, but it may be so obscure it'll be impossible to get to the bottom of in this forum -- and in the end it might be banal, not interesting. Going forward, either add the `#include ` line, or eliminate the call to `getch()`. Either change should make the warning go away for good, and make your program that much more correct. – Steve Summit Apr 29 '18 at 17:45
  • @SteveSummit : Yes , conio.h works . Placing getchar() also works. Thanks a lot – Kanishk Viman Apr 29 '18 at 18:53