- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
Passing an array to a function in C
In this article, we will learn how to pass an individual array element or whole array to a function in C. First, we have created a program that passes an entire array to a function. And then I created a program that passes individual array elements to a function.
Passing the entire array to function
Let's learn, with the program given below, how to pass a whole array to any function in C. The passed array can be used in functions just like it can be used in the main function.
#include<stdio.h> #include<conio.h> void func(int arr[]); int main() { int arr[10], i; printf("Enter 10 array elements: "); for(i=0; i<10; i++) scanf("%d", &arr[i]); printf("\nPassing array to the function...\n"); func(arr); getch(); return 0; } void func(int arr[]) { int i; printf("\nThe array is:\n"); for(i=0; i<10; i++) printf("%d ", arr[i]); }
As you can see from the above program, the whole array (everything inside the array) gets passed into a function named func(), which performs the operation.
The above program was written inside the Code::Blocks IDE; therefore, after a successful build and run, here is the sample output:
Now provide any 10 array elements (numbers) and then press the ENTER key. After this, you will get a message saying "Passing array to the function," and then the complete array gets passed to the function named func(). Inside that function, all the 10 entered array elements get printed back on the output screen; here is the second screenshot of the sample run:
Program Explained
- Receive any 10 numbers as 10 array elements and store them inside the array, saying arr[], one by one.
- That is, the first number is stored in arr[0], the second number in arr[1], the third number in arr[2],... and the tenth number in arr[9].
- Now pass the array to the function named func().
- To pass an array to a function, just write the name of the array as an argument of the function, that is, func(arr), where func() is the name of the function and arr is the name of the array.
- Never forget to declare the function before the main() function or before calling the function.
- With the func(arr); statement, the array arr gets passed to the function definition portion of the program.
- Inside the function definition, we have printed back all 10 elements of the array arr on the output screen.
Passing the individual array elements to a function in C
Now let's create another program that passes individual array elements to a function one by one.
#include<stdio.h> #include<conio.h> void check(int num); int main() { int arr[10], i; printf("Enter 10 Array elements:\n"); for(i=0; i<10; i++) scanf("%d", &arr[i]); printf("\nChecking each elements for Even/Odd..\n"); for(i=0; i<10; i++) check(arr[i]); getch(); return 0; } void check(int num) { if(num%2==0) printf("%d is Even\n", num); else printf("%d is Odd\n",num); }
As you can see here, all 10 elements of the array get passed to the function check(), which checks whether the element is an even number or an odd number. Here is its sample run:
Now enter all 10 numbers (array elements) and then press ENTER. Here is the sample output:
Program Explained
- Receive any 10 array elements.
- Assume the user has supplied array elements such as 1, 2, 3,..., 10.
- Therefore, the first number, which is 1, gets stored at arr[0], the second number, which is 2, gets stored at arr[1], the third number, which is 3, gets stored at arr[2],... and the tenth element, which is 10, gets stored at arr[9].
- Now we have printed a message: checking each element to see if it is even or odd.
- Following this message, we created a for loop that runs from 0 to 9 in order to check all 10 elements of the array that are present at index numbers 0, 1, 2,..., and 9.
- On the first run of the for loop, check(arr[i]) or check(arr[0]) or check(1) is passed to the function check(). That is, the number 1 (the first number or number present at the 0th index of the array) gets passed to the function named check().
- Inside the function, we have used an if-else statement to check for even and odd.
- That is, if num%2 (the num variable holds the number passed through the function that is 1 this time) or 1%2 is equal to 0 or not.
- If it is, then print the number as even; otherwise, print the number as odd.
- After printing "even" or "odd," the program goes back to the main() function, where the for loop variable i is increased by 1, and if 1 is less than 10, the program goes back to the for loop a second time.
- The function check(arr[i]), check(arr[1], or check(2) is called again. That is the number that gets passed to the function check().
- Inside the function, the num variable is set to 2, and it is checked to see if 2 can be divided by 2 without leaving a remainder.
- If it is, then print the number as even; otherwise, print the number as odd.
- Do the same thing until the for loop's condition evaluates to false, that is, until the value of i (the loop variable) becomes 10 and the condition i<10 or 10<10 evaluates to false, and program flow exits the loop.
- Therefore, we have passed all the given 10 array elements one by one to the function to check and print as even or odd.
Here is the modified version of the above program. The user is allowed to define the size of the array before entering elements for it:
#include<stdio.h> #include<conio.h> void checkEven(int e); void checkOdd(int o); static int n1=0, n2=0; int main() { int arr[100], i, size; printf("How many elements you want to store: "); scanf("%d", &size); printf("Enter all %d Array elements:\n", size); for(i=0; i<size; i++) scanf("%d", &arr[i]); printf("\nChecking each elements for Even/Odd..\n"); printf("\nList of all Even numbers: "); for(i=0; i<size; i++) checkEven(arr[i]); printf("\nList of all Odd numbers: "); for(i=0; i<size; i++) checkOdd(arr[i]); getch(); return 0; } void checkEven(int e) { if(e%2==0) { if(n1==0) printf("%d", e); else printf(", %d", e); n1++; } } void checkOdd(int o) { if(o%2 != 0) { if(n2==0) printf("%d", o); else printf(", %d", o); n2++; } }
This program is designed to ask the user at run-time how many elements he/she wants to store, say 10, and then prompt the user to enter all elements (say 10). Finally, the program will list out all the even and odd numbers. Let's take a look at its output with the sample run given below:
Now enter 10 as the size of the array, then provide all the 10 elements, and then press ENTER to see all the even and odd numbers from those entered 10 numbers:
Note: A variable of the static type holds or remembers its previous value throughout the program.
« Previous Program Next Program »