Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Functions Code Examples

Program to find maximum in array elements using recursion and conditional operator

/* Program to find maximum in array elements using recursion and conditional operator Write a C program to find maximum and minimum elements in an array using recursion. How to find maximum and minimum element in an array using recursion in C programming. */ #include <stdio.h> #define maxsize 100 //Maximum size of the array /* Function declarations */ int maximum(int array[], int index, int len); int minimum(int array[], int index, int len); int main() { int array[maxsize], N, max, min; int i; /* Input size and elements of array */ printf("Enter size of the array: "); scanf("%d", &N); printf("Enter %d elements in array: ", N); for(i=0; i<N; i++) { scanf("%d", &array[i]); } max = maximum(array, 0, N); min = minimum(array, 0, N); printf("Minimum element in array = %d\n", min); printf("Maximum element in array = %d\n", max); return 0; } int maximum(int array[], int index, int len) { int max; if(index >= len-2) return (array[index] > array[index + 1]) ? array[index] : array[index + 1]; max = maximum(array, index + 1, len); return (array[index] > max) ? array[index] : max; } /* Recursive function to find minimum element in the array. */ int minimum(int array[], int index, int len) { int min; if(index >= len-2) { return (array[index] < array[index + 1]) ? array[index] : array[index + 1]; } min = minimum(array, index + 1, len); return (array[index] < min) ? array[index] : min; }

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides many types of operators. Except for Arithmetic and Logical Operator, there are some special Operators provided by C language which performs special operations and description of them summarized in the following table.

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions. A function can also be referred as a method or a sub-routine or a procedure, etc.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.



Code count the Number of Occurrences of an element in the linked list using recursion. This C Program uses recursive function & finds the occurrence for an element in an unsorted list.

C Program finds the sum of each row & each column of a MxN matrix. The c program code accepts an MxN matrix. Then adds each row of the matrix and also adds each column of...

Program code to 'convert' uppercase string to lowercase using for loop. First check whether the given character is 'uppercase' alphabet or not. If it is Uppercase Alphabet just add 32 to


Program implements doubly linked list using singly Linked List. It makes use of 2 pointers, one points at the current node, other points at the head and when user requests to move



C Language program code add two Complex numbers by passing structure to a function. In this program code, structures j1 and j2 are passed as an argument of function add(). The

C Programming code implements two Stacks using a Single Array & Check for Overflow & Underflow. A Stack is a linear data structure in which a data item is inserted and deleted...