0

I have two questions here.

Q1: What will the following program output(on a 32-bit little-endian machine):

int main()
{
    long long a = 0x1, b = 0x2, c = 0x3;
    printf("a = %d, b = %d, c = %d.\n", a, b, c);
    return 0;
}

and why?

Q2:

Why the output of a, b and c are different?

void func(int a, int b, int c)
{
    printf("a = %d, b = %d, c = %d.\n", a, b, c);
}
int main()
{
    long long a = 0x1, b = 0x2, c = 0x3;
    printf("a = %d, b = %d, c = %d.\n", a, b, c);
    func(a, b, c);
    return 0;
}
imsrch
  • 1,152
  • 2
  • 11
  • 24
  • 2
    If you set the first and third to defaults, what do you want it to do when I give it two arguments? As for everything else, simply undefined behaviour. – chris May 06 '13 at 04:44
  • @chris But why can't I only set the first parameter to default? – imsrch May 06 '13 at 04:46
  • Read about function overloading. You can do what you want with that. – Pete Fordham May 06 '13 at 04:48
  • You can have different functions with a same name but with different inputs. So if in your first function, c is constant, remove it from the input list and put c = 1 in the first line of the function. – AliBZ May 06 '13 at 04:49
  • http://stackoverflow.com/questions/10022078/will-default-arguments-at-an-arbitrary-point-in-the-argument-list-ever-be-possib for the first Q. – Mat May 06 '13 at 04:50
  • @Mat, Don't remind me lol. – chris May 06 '13 at 04:50
  • http://stackoverflow.com/questions/2270966/printing-unsigned-long-long-using-d and countless others for the second & third (undefined behavior) – Mat May 06 '13 at 04:52
  • The basic funda is that, While calling functions the Actual parameters are assigned from Left to Right directions to the Formal Parameters. So all the Default Parameters should be right hand side. – CodeCodeCode May 06 '13 at 05:11
  • 2
    If you have three questions, why did you not make three posts? One question per post. – Benjamin Lindley May 06 '13 at 05:25

2 Answers2

0

For Q1: As the thumb rule is that All the Default Pramaeters should be on the right handside, func(int a = 1, int b, int c) voilates the rule because of a =1. It will show error because if we write func(5,6) the compiler will not understand that 5 will be assigned to a or b. While calling functions the parameters are assigned from Left to Right directions. This is the basic funda.

Simlarily for func(int a = 1, int b, int c = 3) if we call func(7), compiler will try to assign 7 to a, but there is nothing for b. So that will also cause and error.

CodeCodeCode
  • 459
  • 1
  • 12
  • 21
0

This is in answer to Q3 only.

The simple answer is that printf is not typesafe. The format string is parsed and based on the specifiers that are encountered the printf function pulls data of the appropriate size from the variable argument list.

This all becomes clearer when you see the correct code. Because you are passing long long parameters you need to specify the correct length sub-specifier (int this case "ll"):

printf("a = %lld, b = %lld, c = %lld.\n", a, b, c);

Note the "ll" this tells printf how big the arguments are and in your case they are twice the size (on most current hardware) to the plan "%d" that you were passing.

Peter R
  • 2,865
  • 18
  • 19