Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a question that may tax you, but it may also embarrass me at the same time, but here goes.

Let's have some programming examples with which to work with first:
C
   Declaration of a "C" routine
        int RetIntDayNum(char * datestrg, int * intdaynum, int * iostat);

   Declaring the actual simple "C" routine.
        int RetIntDayNum(char * datestrg, int * intdaynum, int * iostat) {
////
            ... 
            ... 
            ... 
            return(1);
        } 

   Declaring a "C" call:   Routine 1. 
          iretv = RetIntDayNum(cdatestring, &idaynum, &iostat);

   Declaring a "C" call:   Routine 2. 
          iretv = RetIntDayNum(cdatestring, &idaynum);

Ok, here's the question...
In calling a "C" routine, you should use "Routine 1". But in putting my programming project together, I later found out and
realized that I've been using the "Routine 2" approach, and every time I compiled this program;
It compiled OK and ran OK.

Currently, I'm using Enterprise MSVC 2019 environment, upon a Windows 10 Platform.

I know using "Routine 1" is the right way of doing things, but why does the use of "Routine 2" not make the program
blow up?. Why. Or is it a waiting explosion ready to happen?
Currently, I've got to go back into this big program and fix all my program mistakes. But it bothers
me why doesn't the "Routine 2" approach cause the program to blow up( or abend ) now.

Or will this waiting explosion happen, once this
program runs on a completely different windows platform??

My curious mind wants to know...
And again, Thanks!

What I have tried:

what's shown in the question area, and in my programming project.
Posted
Updated 19-Mar-24 2:56am
v3

It depends on the compiler: most will give you an error "too few arguments to function" when you try to call it with two parameters.

Other than that, it will probably blow up when you try to use iostat within the function as it's value will be undefined and you could be reading or writing to any part of memory.

I'd check what you are using - MSVC was MicroSoft Visual C and that was a standalone product that became part of Visual Studio about 20 years ago and which never had a 2019 version.
And I'm pretty sure the VS 2019 would throw an error on too few parameters for a C program (it is possible to define functions in C that have a variable number of arguments - called a variadic function - but that takes a special syntax and how you use them is different).
 
Share this answer
 
First of all, a distinction must be made between the declaration in the implementation and the call of a function. The following line is not a declaration but an assignment:
C
iretv = RetIntDayNum(cdatestring, &idaynum, &iostat);

VS2019 usually always generates an error if it cannot find a suitable implementation for the call.

The call of
C
iretv = RetIntDayNum(cdatestring, &idaynum);

causes error C2198: "RetIntDayNum": Not enough arguments for call.
or error C2660: "RetIntDayNum": Function does not accept 2 arguments

As VS2019 is a C++ compiler, several questions may arise.
Is the code compiled as C or C++ code and with which compiler version? VS2019 can basically compile with the following standards: C++14, C++17, C++20, Legacy MSVC, std:c11, std:c17.

The only explanation I can think of for the described behavior is that somewhere there is another function with the same name and only 2 parameters.
The question of whether the program can "explode" after (error-free) compilation due to the missing parameter cannot be answered, as it is not known what the functions actually do.
In any case, you could only access variables that are also visible to the compiler. If there are global variables with the same name, this could be unpleasant.
 
Share this answer
 
v3
I could not believe that. So I tried the following piece of code
C
#include <stdio.h>
  

int RetIntDayNum(char * datestrg, int * intdaynum, int * iostat);

int main()
{

  char foo[] = "foo";
  int daynum = 42;
  int iostat = 10;

  RetIntDayNum(foo, &daynum, &iostat);
  RetIntDayNum(foo, &daynum);

  return 0;
}


int RetIntDayNum(char * datestrg, int * intdaynum, int * iostat)
{
  printf("datestrg %s, intdaynum %d, iostat  %d\n", datestrg, *intdaynum, *iostat);
  return 0;
}

on two different platforms:
  • Linux box, gcc 9.4
  • Windows 10, VS2022, CL 19.34

Both compilers gave an error:
  • error: too few arguments to function ‘RetIntDayNum’ (gcc)
  • error C2198: 'RetIntDayNum': too few arguments for call (CL)
 
Share this answer
 
v2
Comments
Richard MacCutchan 19-Mar-24 5:08am    
Interesting that you omitted the three values corresponding to the % items in the printf format string. :)
CPallini 19-Mar-24 7:04am    
Fixed now, thank you Richard.
I got the warnings from the compilers and ignored them resolutely. :-D
Richard MacCutchan 19-Mar-24 7:05am    
Warnings are for wimps. :))
Pete O'Hanlon 19-Mar-24 9:20am    
Sounds like something that should be on a range of bumper stickers. "Warnings are for wimps". "Exceptions are for a**holes".
Richard MacCutchan 19-Mar-24 9:29am    
And on T-shirts.
Your function declared as taking 3 arguments. But you call it passing only 2 arguments. Hence you get an error during compilation. 3rd argument is not optional as declared.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900