Parameters passing conventions in c



There are two types of parameters passing conventions in c:


1. pascal: In this style function name should (not necessary ) in the uppercase .First parameter of function call is passed to the first parameter of function definition and so on.

2. cdecl: In this style function name can be both in the upper case or lower case. First parameter of function call is passed to the last parameter of function definition. It is default parameter passing convention.
Examples:

1.What will be output of following 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: 25 0
0 25



(2) What will be output of following program?


#include<stdio.h>
void cdecl fun1(int,int);
void pascal fun2(int,int);
int main(){
    int a=5,b=5;
    fun1(a,++a);
    fun2(b,++b);
    return 0;
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}

Output:
cdecl:  6 6
pascal: 5 6

(3) What will be output of following program?

#include<stdio.h>
void cdecl fun1(int,int);
void pascal fun2(int,int);
int main(){
    int a=5,b=5;
    fun1(a,++a);
    fun2(b,++b);
    return 0;
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}

Output:
cdecl:  6 6
pascal: 5 6

(4) What will be output of following program?

#include<stdio.h>
void convention(int,int,int);
int main(){
    int a=5;
    convention(a,++a,a++);
    return 0;
}
void  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}

Output: 7 7 5

(5) What will be output of following program?

#include<stdio.h>
void pascal convention(int,int,int);
int main(){
    int a=5;
    convention(a,++a,a++);
    return 0;
}
void pascal  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}

Output: 5 6 6

(6) What will be output of following program?

#include<stdio.h>
void pascal convention(int,int);
int main(){
    int a=1;
    convention(a,++a);
    return 0;
}

void pascal  convention(int a,int b){
    printf("%d %d",a,b);
}

Output: 1 2

(7) What will be output of following program?

#include<stdio.h>
void  convention(int,int);
int main(){
    int a=1;
    convention(a,++a);
    return 0;
}
void  convention(int a,int b){
    printf("%d %d",a,b);
}

Output: 2 2

Post a Comment