Constant Pointer vs Pointer to Constant C Program

In previous post we discussed about pointers, how to define and use pointers. In this post we will learn what's the difference between constant pointer and pointer to constant. If we define a pointer as constant then we cannot modify its memory address. For example if we assigned the memory address of a variable 'a' to the pointer 'ptr'. Now if we try to assign the address of the variable 'b' to the pointer 'ptr' then compiler will throw an error message. This means that we cannot change/modify the address assigned to the constant pointer but we can modify/change the...
forward

Pointers in C Programming – Code Example

In previous posts we concluded the arrays topic. In next 2-3 posts we will discuss about pointers with some code examples. Pointers are used to store memory address of variables or arrays. In following simple program we created a pointer named as countPtr and a variable named as count. Then we assigned the address of the variable count to the pointer countPtr. Now by using that pointer we can get the memory address of the variable count as well as the value of the variable count. [js] #include <stdio.h> int main(void){ int count = 5; int *countPtr; countPtr = &count; printf("Address...
forward

Finding Mode Value From Array C Programming

In previous posts we learned how to find mean value from array and median value from array. In this post we will learn how to find mode value from array elements. Mode value is the most repeated value from the elements. In following simple program we will create a function which will accept an array as an input. Furthermore that function will return mode value from the array elements. [js] #include <stdio.h> int mode(int arr[], int size); // function prototype int main(void){ int arrayName[10] = {2,4,4,4,1,2,3,4,2,3}; // defined an array with 10 elements //output each element of array printf("Original values of...
forward

Finding Median Value From Array C Programming

In previous post we learned how to write a C program to find mean value from array elements. In this post we will learn how to find median value from array elements. Median value is the centered value in sorted (ascending order) elements. We also learned in our previous post about how to sort array elements in ascending order using bubble sort function. In following simple program we will create a function which will accept an array as an input. Furthermore that function will return median value from the array elements. [js] #include <stdio.h> int median(int arr[], int size); //...
forward

Finding Mean Value From Array C Programming

In previous post we discussed about how to sort array elements using bubble sort function. In following simple program we will create a function which will accept an array as an input. Furthermore that function will return mean value of the array elements. Mean value is the average of all array elements. Mean value is calculated by summing all values of the array and dividing by the total number of array elements. [js] #include <stdio.h> int mean(int arr[], int size); // function prototype int main(void){ int arrayName[10] = {90,44,33,83,49,34,51,84,56,44}; // defined an array with 10 elements //output each element of array...
forward

Sorting Array Elements Using Bubble Sort in C Programming

In previous post we discussed about how to pass an array into function. In following simple program we will create a function which will accept an array as an input. We will pass an array as input to a function called bubblesort. That function will then sort the elements of the array in ascending order. [js] #include <stdio.h> void bubbleSort(int arr[], int size); // function prototype int main(void){ int arrayName[10] = {90,44,33,83,49,34,51,84,56,44}; // defined an array with 10 elements //output each element of array printf("Original values of the arraynn"); printf("Array indextttValuen"); for(int j=0; j<10; j++){ printf("%11dttt%4dn", j, arrayName[j]); } bubbleSort(arrayName, 10);...
forward

Passing Arrays into Functions

In previous two posts we discussed about arrays and functions. In following simple program we will create a function which will accept an array as an input. Furthermore that function will multiply each element of the array with 20. At the and it outputs the elements of the array. It's to be noted that when we pass an array into function, then by default it's a function call by reference. Which means that any processing done by the called function will change/modify the values of the original array elements. [js] #include <stdio.h> void modifyArray(int arr[], int size); // function prototype int...
forward

Arrays in C Programming

Arrays in C Programming: Array is a type of variable which can store multiple items of the same data type. In following simple program we will define an array of 10 elements. After that we will multiply each element of array with 5 and then we will output the value of each element of the array. [js] #include <stdio.h> int main(void){ int arrayName[10] = {2,5,3,6,9,34,23,84,56,44}; // defined an array with 10 elements //output each element of array printf("Original values of the arraynn"); printf("Array indextttValuen"); for(int j=0; j<10; j++){ printf("%11dttt%4dn", j, arrayName[j]); } //multiplied each element of array with 5 for(int...
forward

Function Call by Value and Call by Reference in C Programming

In this simple C code example we tried to show the difference between function call by value and call by reference in C programming. In function call by value the original variables are not affected because the values are copied inside function definitions. Whereas in function call by reference the original variables are affected because we pass the memory addresses inside function definitions. [js] #include <stdio.h> void swapCallByValue(int x1, int y1); // this is prototype of our function (call by value) void swapCallByReference(int *x1, int *y1); // this is prototype of our function (call by reference) int main(void){ // in...
forward

Functions in C Programming – Code Example

For simplicity we divide our code into small pieces / modules known as functions. To use functions in C programming we need to define function prototype, function definition and finally we call the function to perform that functionality. Here we will define a simple sum function, which will accept 2 numbers as an input and will return sum of these numbers. [js] #include <stdio.h> int sum(int x, int y); // this is prototype of our sum function. int main(void){ // for simplicity we divide our code into small pieces / modules known as functions // to use functions in C programming...
forward