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 and minimum using functions

/* program to find maximum and minimum using functions Write a C program to input two or more numbers from user and find maximum and minimum of the given numbers using functions. How to find maximum and minimum of two or more numbers using functions in C programming. We already learned to find maximum using conditional operator and using many other approaches. Here, I will embed the logic to find maximum within a function. Let us define function to find maximum. First give a meaningful name to our function. Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of int type say, max(int number1, int number2). Finally, the function should return maximum among given two numbers. Hence, the return type of the function must be same as parameters type i.e. int in our case. After combining the above three points, function declaration to find maximum is int max(int number1, int number2);. */ #include <stdio.h> /* Function declarations */ int max(int number1, int number2); int min(int number1, int number2); int main() { int number1, number2, maximum, minimum; /* Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &number1, &number2); maximum = max(number1, number2); // Call maximum function minimum = min(number1, number2); // Call minimum function printf("\nMaximum = %d\n", maximum); printf("Minimum = %d", minimum); return 0; } /* Find maximum between two numbers. */ int max(int number1, int number2) { return (number1 > number2 ) ? number1 : number2; } /* Find minimum between two numbers. */ int min(int number1, int number2) { return (number1 > number2 ) ? number2 : number1; }

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,

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.

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.

#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:

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.

Program display 'every possible combination' of two words from the given 2 string without displaying "repeated combinations". Convert string in 2D array. Make the first string words


C Program calculate length of string without using strlen() function. You can use standard library function 'strlen()' to find the length of a string, this C program computes the length





C Program create a Linked List & display the elements in the list. Linked list is an ordered set of Data Elements, each containing a link to its successor. This Program is to create a



C program to input Octal number from user and convert to Binary number system. How to convert from Octal Number System to Binary number system in C. Octal number...