Predict the Output of the Below C Programs

Program 1:

C




#include <stdio.h>
 
void fun(int arr[], unsigned int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
}
 
// Driver program
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    unsigned int n = sizeof(arr) / sizeof(arr[0]);
    fun(arr, n);
    return 0;
}


Program 2:

C




#include <stdio.h>
void fun(int* arr)
{
    int i;
    unsigned int n = sizeof(arr) / sizeof(arr[0]);
    for (i = 0; i < n; i++)
        printf("%d  ", arr[i]);
}
 
// Driver program
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    fun(arr);
    return 0;
}


Hint: For the 64-bit processor, the size of the pointer is 8 bytes and the size of the integer is 4 bytes.

Program 3:

C




#include <stdio.h>
#include <string.h>
 
void fun(char* arr)
{
    int i;
    unsigned int n = strlen(arr);
    printf("n = %d\n", n);
    for (i = 0; i < n; i++)
        printf("%c  ", arr[i]);
}
 
// Driver program
int main()
{
    char arr[] = "geeksquiz";
    fun(arr);
    return 0;
}


Program 4:

C




#include <stdio.h>
#include <string.h>
 
void fun(char* arr)
{
    int i;
    unsigned int n = strlen(arr);
    printf("n = %d\n", n);
    for (i = 0; i < n; i++)
        printf("%c  ", arr[i]);
}
 
// Driver program
int main()
{
    char arr[]
        = { 'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z' };
    fun(arr);
    return 0;
}


NOTE: The character array in the above program is not ‘\0’ terminated. (See this for details)

Pass Array to Functions in C

In C, the whole array cannot be passed as an argument to a function. However, you can pass a pointer to an array without an index by specifying the array’s name.

Arrays in C are always passed to the function as pointers pointing to the first element of the array.

Similar Reads

Syntax

In C, we have three ways to pass an array as a parameter to the function. In the function definition, use the following syntax:...

Examples of Passing Array to Function in C

Example 1: Checking Size After Passing Array as Parameter...

Predict the Output of the Below C Programs

...

Frequently Asked Questions (FAQs)

...