Functions


  1. What will be the output of the following C code?
    #include <stdio.h>
    int *fun();
    void main()
    {
    int *ptr = fun();
    printf("Welcome ");
    printf("%d", ptr[0]);
    }
    int *fun()
    {
    int n[5] = {15, 18, 10};
    return n;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Segmentation fault

    main.c: In function ‘fun’:
    main.c:12:16: warning: function returns address of local variable [-Wreturn-local-addr]
    return n;
    ^
    $main
    timeout: the monitored command dumped core
    sh: line 1: 179205 Segmentation fault timeout 10s main


  1. What will be the output of the following C code?
    #include <stdio.h>
    void main()
    {
    int n = fun();
    printf("%d", n);
    }
    void fun()
    {
    printf("Welcome ");
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Welcome 8



  1. What will be the output of the following C code?
     #include <stdio.h>
    int *fun()
    {
    int *ptr1 = 10;
    return ptr1;
    }
    void main()
    {
    int *ptr2 = fun();
    printf("%d", ptr2);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    10


  1. What is the problem in the following C declarations?
    int function(int);
    double function(int);
    int function(float);











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    *A function with same name cannot have different return types.
    *A function with same name cannot have different number of parameters.
    *A function with same name cannot have different signatures.



  1. What will be the output of the following C code?
    #include <stdio.h>
    int *fun();
    void main()
    {
    int n = A();
    printf("%d", n);
    }
    int *fun()
    {
    int num[5] = {51, 18, 25};
    return num;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Compilation Error

    main.c: In function ‘main’:
    main.c:5:17: warning: implicit declaration of function ‘A’ [-Wimplicit-function-declaration]
    int n = A();
    ^
    main.c: In function ‘fun’:
    main.c:11:16: warning: function returns address of local variable [-Wreturn-local-addr]
    return num;
    ^~~
    /tmp/ccNdhoL1.o: In function `main':
    main.c:(.text+0xe): undefined reference to `A'
    collect2: error: ld returned 1 exit status