retagged by
369 views
1 votes
1 votes
int fun(int arr[]); int fun(int arr[2]); Both the statements are same??? HELP
retagged by

1 Answer

1 votes
1 votes

Declaration is all about just knowing the data type.
It is enough to know that, it is an array of int and it will return int .

So both statements will work same.

Run the below code:




#include <stdio.h>

// Function declaration: int fun(int arr[])
int fun(int arr[])
{
    int sum = 0;
    int i;

    // Calculate the sum of array elements
    for (i = 0; arr[i] != '\0'; i++)
    {
        sum += arr[i];
    }

    return sum;
}

// Function declaration: int fun(int arr[2])
int fun2(int arr[2])
{
    int sum = 0;
    int i;

    // Calculate the sum of array elements
    for (i = 0; arr[i] != '\0'; i++)
    {
        sum += arr[i];
    }

    return sum;
}

int main()
{
    int arr1[] = {1, 2, 3, 4, 5};  // Array of any size
    
    // Call the first function
    int result1 = fun(arr1);
    printf("Result 1: %d\n", result1);

    // Call the second function
    int result2 = fun2(arr1);
    printf("Result 2: %d\n", result2);

    return 0;
}

OUTPUT:

Result1: 15 

Result2: 15

Related questions

884
views
3 answers
5 votes
lovish_bhatia asked Jun 11, 2023
884 views
Consider the following program, what will be the output?#include<stdio.h void main() { int a; a = printf("2>5") printf("2<5") ? printf("2")!=printf("20")?printf("GA"):pr...
630
views
0 answers
0 votes
Pranavpurkar asked Sep 27, 2022
630 views
Consider a string is stored in a word addressable memory where each word is of 4 Bytes. If the string starts at 100 and ends at 104 then which of the following can be the...
571
views
0 answers
0 votes
Pranavpurkar asked Sep 27, 2022
571 views
Which of the following is/are true regarding the following code snippet?1:int *p;2:p = &q; A)*p gives faster retrieval of information than q.B)The size of p is same as si...
1.2k
views
3 answers
1 votes
Sunnidhya Roy asked Dec 14, 2022
1,193 views
Given the following piece of code :Main(){Int child = fork();Int c = 5;if (child == 5){C + = 5;}Else{Child = fork();C + = 5;}}What are the different values of the variabl...