1

An interview question arise a strong confusion in my mind i.e Lets see this program

#include "stdio.h"

int main()
{
    static int a=25;
    void cdecl conv1();
    void pascal conv2();
    conv1(a);
    conv2(a);
    return 0;
}

void cdecl conv1(int a,int b)
{
    printf("%d%d", a, b);
}

void pascal conv2(int a,int b)
{
    printf("\n%d%d", a, b);
}

Output is

25 0

0 25

But why? And how?

Can you briefly explain to me because I don't understand this program mentioned in UGC book.

Please help me to understand this concept more finely, so that I can better prepare for my interview.

Thanks for your precious time.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Bhavya
  • 53
  • 1
  • 8
  • 4
    Your question is unclear. This program is not valid C OR Pascal. – StoryTeller - Unslander Monica Aug 01 '17 at 09:59
  • 9
    `cdecl` and `pascal` are not part of the C language, but compiler extensions for different calling conventions. Apparently they affect the way parameters are passed to functions. However, what happens when you call a function with the wrong number of parameters isn't too interesting. I would seriously not want to work for a company using this to screen the applicants. – Bo Persson Aug 01 '17 at 09:59
  • You're using a Microsoft Compiler on Windows? You should mention platform and compiler in your question... – Scheff's Cat Aug 01 '17 at 10:05
  • It's a little odd that you haven't passed in the argument for `b`. Standard C doesn't have "default parameter" support. Have you looked at assembly calling conventions for C versus Pascal? Each language has a choice as to how to pass arguments to function. When pushing arguments on the stack, they also have a choice of what order to push them in (first to last, versus last to first). In particular, C pushes arguments onto the stack last to first, and I believe the Pascal convention is first to last. – lurker Aug 01 '17 at 14:36
  • I've removed your Pascal tag. That tag is described as being for questions about the Pascal programming language, and your question has absolutely nothing to do with the Pascal language. Please read tag descriptions instead of just adding them because they seem to sound proper. Tags have specific meanings and relevance here. Thanks. – Ken White Aug 03 '17 at 12:35

1 Answers1

4

(As already hinted by Bo Persson, this has (probably) to do with the so-called calling convention.

There is a nice explanation in Wikipedia x86 calling conventions.

Short summary: Different languages (resp. the compilers) may have different conventions in which order the arguments of a function are passed (e.g. on stack).

If you want to link object files where code is written in different languages, this can become an issue. Thus, some compilers have extensions to change the calling convention for function calls. (If nothing denoted the native is used, of course.)


Story Teller pointed out that (beside of the calling convention issue) there is something else in your sample code which is really suspicious.

The prototypes conv1() and conv2() in main have unspecified argument lists. This is allowed in C (elaborated e.g. in SO: C: Unspecified number of parameters - void foo()). Unfortunately, it prevents detection of wrong calling.

conv1() and conv2() have two parameters each. However, both are called in main() with one argument. This is undefined behavior.

(Thank you Story Teller to let me recognize this. The calling convention thing let me totally oversee this as well as the hint in Bo Perssons comment.)

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • @StoryTeller I thought in C, an empty argument list means the function can take any amount of arguments. If you want to declare a function that takes no arguments, you need to use `void`? like: `void myfunc(void)`. – Nikos C. Aug 01 '17 at 10:33
  • 1
    @NikosC. Not really. It simply means "unspecified" (vs. variable/variadic). – Scheff's Cat Aug 01 '17 at 10:34
  • This looks like a comment, not an answer to OP's question. – melpomene Aug 01 '17 at 10:35
  • @NikosC. In C, the empty parentheses means that the function is not variadic (does not have `, ...` at the end of the argument list), but the arguments are otherwise unspecified — in type and number. It means that the compiler cannot use the declaration to check whether calls are correct. – Jonathan Leffler Aug 01 '17 at 14:09
  • 1
    @Scheff: not *probably*. **pascal** is a calling convention: Callee clears the stack, passing from left to right (unlike **cdecl**, where the caller clears the stack and passing is from right to left). Calling conventions do not necessarily differ from language to language. Most languages on Windows know several, the most important are **cdecl** and **stdcall**. **pascal** is a rather old convention, hardly ever used in Win32. – Rudy Velthuis Aug 01 '17 at 16:51
  • Of course only passing one argument when the function expects two is weird, and I would not want to get such an interview question. Or I would complain that this doesn't make sense. – Rudy Velthuis Aug 01 '17 at 17:20
  • @RudyVelthuis Well, not "probably" but "(probably)". I asked for platform and compiler but got no response. I remember that calling convention was an issue for me when doing Windows 3.1 programming (probably with Borland C++) but this is _many_ years ago (may be 20 years or more)... – Scheff's Cat Aug 02 '17 at 05:30
  • @Scheff: AFAIK, calling conventions like `pascal` are only available on Windows. – Rudy Velthuis Aug 02 '17 at 15:17
  • @RudyVelthuis You are probably right but I don't know so many (exotic) C compilers and didn't dare to make such statement... – Scheff's Cat Aug 02 '17 at 15:20
  • @Scheff: hence my comment. – Rudy Velthuis Aug 02 '17 at 15:21