Structure in C Programming

Structure in C programming language is a user defined data type that groups logically related information of different data types into a single unit. A structure variable can store multiple variables of different data types. Each variable declared in structure is called member.
Generally we want to store more than one information about any object. Suppose we want to store information about all employees of a company. We want to store following information for every employee:

Name (String)
Age (integer)
Salary (floating point number)
Department (String)

We can create a custom data type, which can store all data about a employee under single name.

C programming language provide support for defining a custom data type(structure) which can aggregate and store all related information about an entity. By using a structure, we can store all the details an employee into a structure variable, and we can define an array of structures to store details of all employees.

Structures in Details Click on the links below to know more about structures in C

  • Accessing Members of Structure
  • Pointers to Structures in C
  • Passing Structure to Function in C
  • Nesting of Structures in C
  • Array of Structure in C

Defining a Structure in C

Keyword struct is used to declare a structure. Declaration of a structure specifies the format/schema/template of the user defined data type. Like any other variables in C, a structure must be declared, before it’s first use in program.

Syntax of Declaring a Structure

struct structure_name
{
data_type variable_name1;
data_type variable_name2;
.
.
data_type variable_nameN;
};
  • struct keyword is used to declare a structure.
  • structure_name is the name of the structure.
  • data_type variable1; is member variable declaration statement. data_type is the type of member variable.
  • We can declare any number of member variables inside a structure.
  • Declaring a structure alone will not create any variable or allocate any memory. Memory is allocated first time when a variable of structure type is declared.

For Example

Structure declaration to store above mentioned employee details

struct employee
{
 char name[100];
 int age;
 float salary;
 char department[50];
};

Declaration of Structure Variable

We can declare variable of structure once we defined the format of structure. There are two ways of declaring a structure variable:

  • Using struct keyword after structure definition.
  • Declaring variables at the time of defining structure.

 

Declaring Variables during Structure Definition

struct employee
{
    char name[100];
 int age;
 float salary;
 char department[50];
} employee_one;

variable employee_one is an instance of structure employee.

We can declare multiple variables by separating them with comma.

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one, employee_two, employee_three;

Declare Variables using struct Keyword
he syntax of defining a structure variable is similar to the variable declaration syntax of any basic data type in C. it uses struct keyword followed by structure name as the data type.

struct structure_name variable_name;
For Example
struct employee employee_one;

Above statement declares a variable of structure type employee.

All member variables of a structure are stored in contiguous memory locations.

Initialization of Structure Variable

Like any other variable in C, we can initialize a structure variable at the time of declaration. We can specify initial values as a comma separated list of constants enclosed between curly braces.

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack", 30, 1234.5, "Sales"};

We can also initialize structure variable after structure declaration.

struct employee employee_one = {"Jack", 30, 1234.5, "Sales"};

It is not compulsory to initialize all the member variables of a structure. Following statement initializes only name member of structure employee and remaining members gets initialized with zero(for integer and float) and NULL(for pointers and char).

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack"};

String in C Programming

String in C Programming

String in C programming language is a one-dimensional array of characters which is terminated by a null character(‘\0’). A character array which is not null terminated is not a valid C string. In a C string, each character occupies one byte of memory including null character.

Null character marks the end of the string, it is the only way for the compiler to know where this string is ending. Strings are useful when we are dealing with text like Name of a person, address, error messages etc.

Examples of C Strings

  • “A”
  • “TechCrashCourse”
  • “Tech Crash Course”
  • ” “
  • “_18u,768JHJHd87”
  • “PROGRAM”

Below is the representation of string in memory.

C-Strings

Points to Remember about Strings in C

  • C Strings are single dimensional array of characters ending with a null character(‘\0’).
  • Null character marks the end of the string.
  • Strings constants are enclosed by double quotes and character are enclosed by single quotes
For Example

String constant : “TechCrashCourse”
Character constant: ‘T’

  • If the size of a C string is N, it means this string contains N-1 characters from index 0 to N-2 and last character at index N-1 is a null character.
  • Each character of string is stored in consecutive memory location and occupy 1 byte of memory.
  • The ASCII value of null character(‘\0’) is 0.

Declaration of Strings in C

String is not a basic data type in C programming language. It is just a null terminated array of characters. Hence, the declaration of strings in C is similar to the declaration of arrays.

Like any other variables in C, strings must be declared before their first use in C program.
Syntax of String Declaration
char string_name[SIZE_OF_STRING];
In the above declaration, string_name is a valid C identifier which can be used later as a reference to this string. Remember, name of an array also specifies the base address of an array.

SIZE_OF_STRING is an integer value specifying the maximum number of characters which can be stored in this string including terminating null character. We must specify size of string while declaration if we are not initializing it.
Examples of String Declaration

  • char address[100];
  • char welcomeMessage[200];

C Does not provide support for boundary checking i.e if we store more characters than the specified size in string declaration then C compiler will not give you any error.

Initialization of Strings

In C programming language, a string can be initialized at the time of declarations like any other variable in C. If we don’t initialize an array then by default it will contain garbage value.
There are various ways to initialize a String Initialization of string using a character array

char name[16] = {'T','e','c','h','C','r','a','s','h','C','o','u','r','s','e','\0'};
or 
char name[] = {'T','e','c','h','C','r','a','s','h','C','o','u','r','s','e','\0'};

In the above declaration individual characters are written inside single quotes(”) separated by comma to form a list of characters which is wrapped in a pair or curly braces. This list must include a null character as last character of the list.

If we don’t specify the size of String then length is calculated automatically by the compiler.
Initialization of string using string literal

char name[16] = "TechCrashCourse";
or
char name[] = "TechCrashCourse";

In above declaration a string with identifier “name” is declared and initialized to “TechCrashCourse”. In this type of initialization , we don’t need to put terminating null character at the end of string constant. C compiler will automatically inserts a null character(‘\0’), after the last character of the string literal.

If we don’t specify the size of String then length is calculated automatically by the compiler. The length of the string will be one more than the number of characters in string literal/constant.
Initialization of string using character pointer

char *name = "TechCrashCourse";

In the above declaration, we are declaring a character pointer variable and initializing it with the base address of a string constant. A null terminating character is automatically appended by the compiler. The length of the string will be one more than the number of characters in string constant.

C Program showing String Initialization

String in C Programming

#include <stdio.h>
#include <conio.h>
  
void main(){  
   char nameOne[16] = {'T','e','c','h','C','r','a','s','h','C','o','u','r','s','e','\0'};
   char nameTwo[] = {'T','e','c','h','C','r','a','s','h','C','o','u','r','s','e','\0'};
   char nameThree[16] = "TechCrashCourse";
   char nameFour[] = "TechCrashCourse";
   char *nameFive = "TechCrashCourse";
   
   printf("%s\n", nameOne); 
   printf("%s\n", nameTwo); 
   printf("%s\n", nameThree); 
   printf("%s\n", nameFour);
   printf("%s\n", nameFive); 
    
   getch();
   return 0; 
}

Output

TechCrashCourse
TechCrashCourse
TechCrashCourse
TechCrashCourse
TechCrashCourse

String Input Output in C

Standard library functions for reading strings

  • gets() : Reads a line from stdin and stores it into given character array.
  • scanf() : Reads formatted data from stdin.
  • getchar() : Returns a character from stdin stream.
  • fscanf() : Read formatted data from given stream.

Standard library functions for printing strings

  • puts() : Writes a string to stdout stream excluding null terminating character.
  • printf() : Print formatted data to stdout.
  • putchar() : Writes a character to stdout stream.
  • fprintf() : Writes formatted output to a stream.

C Program to read and print strings

C Program to read and print strings

#include <stdio.h>
#include <conio.h>
 
int main(){
    char name[30], city[100], country[100];
    char c;
    int i=0;
     
    printf("Enter name: ");
    /* Reading string using gets function */
    gets(name);
     
    printf("Enter city: ");
    /* Reading string using getchar function */
    while((c = getchar()) != '\n'){
        city[i]=c;
        i++;
    }
    city[i]='\0';
     
    printf("Enter country: ");
    /* Reading string using scanf function */
    scanf("%s", country);
     
    /* Printing string using printf() */
    printf("%s\n", name);
    /* Printing string using puts() */
    puts(city);
    /* Printing string using putchar() */
    for(i=0; country[i] != '\0'; i++)
       putchar(country[i]);
     
    getch();
    return 0;
}

Output

Enter name: Jack
Enter city: New York
Enter country: USA
Jack
New York
USA

String Handling Standard Library Function
string.h header file contains a wide range of functions to manipulate null-terminated C strings.

Below are some commonly used string handling functions of string.h header file.

Function Description
strcat() It appends one string at the end of another string.
strchr() It searches for the first occurrence of a character in the string.
strcmp() It compares two strings character by character.
strcpy() It copies characters from one string into another string.
strlen() It returns the length of a string.
strstr() It searches for the first occurrence of a string in another string.
strtok() It breaks a string into smaller tokens.

C Program to show the use of string handling functions of string.h header file

C Program to show the use of string handling functions of string.h header file

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
int main(){
   char inputString[50];
   char copyString[60];
    
   printf("Enter a string: ");
   gets(inputString);
 
   /* Printing length of string using strlen() */
   printf("Length of %s : %d\n", inputString, strlen(inputString));
 
   /* copying inputString into copyString */
   strcpy(copyString, inputString);
   printf("Copied String : %s\n", copyString);
 
   /* comparing inputString and copyString string*/
   if(strcmp(copyString, inputString)){
       printf("Both Strings are Not Equal\n");
   } else {
       printf("Both Strings are Equal\n");
   }
 
   getch();
   return 0;
}

Program Output

Enter a string: TechCrashCourse
Length of TechCrashCourse : 15
Copied String : TechCrashCourse
Both Strings are Equal

C Program to Find Factorial of a Number using Recursion

The factorial of a integer N, denoted by N! is the product of all positive integers less than or equal to n. Factorial does not exist for negative numbers and factorial of 0 is 1.

N! = 1 x 2 x 3 x 4….x (N-2) x (N-1) x N

For Example
5! = 5 * 4 * 3 * 2 * 1 = 120.

We can use recursion to calculate factorial of a number because factorial calculation obeys recursive sub-structure property. Let factorial(N) is a function to calculate and return value of N!. To find factorial(N) we can first calculate factorial(N-1) then multiply it with N.
N! = 1 x 2 x 3 x 4….x (N-2) x (N-1) x N
N! = (N-1)! x N

factorial(N) = factorial(N-1) x N
Function factorial(N) reduces the problem of finding factorial of a number N into sub-problem of finding factorial on N-1. It keeps on reducing the domain of the problem until N becomes zero.

C program to calculate factorial of a number using recursion

Here we are using recursion to calculate factorial of a number. Below program contains a user defined recursive function getFactorial, which takes an integer(N) as input parameter and returns it factorial. To calculate factorial of N, getFactorial function first calls itself to find value of (N-1)! and then multiply it with N to get value of N!. Below c program cannot be used to calculate factorial of very big number because factorial of such numbers exceeds the range of int data type.

C Program to Find Factorial of a Number using Recursion 2

/*
* C Program to print factorial of a number 
* using recursion
*/
#include <stdio.h>
#include <conio.h>
 
int getFactorial(int N);
int main(){
    int N, nFactorial, counter;
    printf("Enter a number \n");
    scanf("%d",&N);
 
    printf("Factorial of %d is %d", N, getFactorial(N));
     
    getch();
    return 0;
}
 
/*
 * Recursive function to find factorial of a number
 */
int getFactorial(int N){
    /* Exit condition to break recursion */
    if(N <= 1){
         return 1;
    }
    /*  N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1  */ 
    return N * getFactorial(N - 1);
}

Program Output

Enter a number 
7
Factorial of 7 is 5040
Enter a number 
0
Factorial of 0 is 1

C Program to Delete Vowel Alphabets From a String

  • Write a C program to delete all vowels from a string.

There are five vowels alphabets in english A, E, I, O and U. We have to delete all vowel characters from a string. If Input string is “techcrashcourse” then output should string should be “tchcrshcrs” after removing all occurrences of vowels.

C program to remove or delete vowels from string using extra memory

In this program, we first take a string as input from user using gets function. Here we are using another array to store the output string. This program uses a user defined function isVowel that takes a character as input and decide whether input character is vowel or not. Function isVowel converts upper case characters to lowercase and then perform vowel check. We traverse from first character to last character of input string and check whether current character is vowel or not. If it is not a vowel then we copy this character to output string otherwise skip this character. At last we add a null character at the end of output string, now output string contains all input string characters except vowels.

C program to remove or delete vowels from string using extra memory

/*
* C Program to remove vowels from string
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
 
int isVowel(char ch);
int main(){
    char inputString[100], outputString[100];
    int readIndex, writeIndex;
    printf("Enter a string \n");
    gets(inputString);
    for(readIndex = 0, writeIndex = 0; inputString[readIndex] != '\0'; readIndex++){
        if(!isVowel(inputString[readIndex])){
            /* If current character is not a vowel, copy it to outputString */
            outputString[writeIndex++] = inputString[readIndex];
        }
    }
    outputString[writeIndex] = '\0';
     
    printf("Input String: %s \n", inputString);
    printf("String without Vowels: %s \n", outputString);
     
    getch();
    return 0;
}
 
/*
 * Function to check whether a character is Vowel or not
 * Returns 1 if character is vowel Otherwise Returns 0 
 */
int isVowel(char ch){
    /* Check if character is lower case or upper case alphabet
    *  For any non-alphabet character return 0
    */
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        if(ch >= 'A' && ch <= 'Z'){
            ch = ch + ('a' - 'A');
        }
        /* Check if character(ch) is a vowel */
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
            return 1;
        }     
    } 
    return 0;
}

Program Output

Enter a string
delete vowels
Input String: delete vowels
String without Vowels: dlt vwls

C program to delete or remove vowels from string without using extra memory

In this program, we don’t use any extra character array to store the output string without vowels. We will modify input string and delete all vowels in single pass.

C program to delete or remove vowels from string without using extra memory

/*
* C Program to remove vowels from string without using extra memory
*/
#include <stdio.h>
#include <conio.h>
 
int isVowel(char ch);
int main(){
    char inputString[100];
    int readIndex, writeIndex;
    printf("Enter a string \n");
    gets(inputString);
    for(readIndex = 0, writeIndex = 0; inputString[readIndex] != '\0'; readIndex++){
        if(!isVowel(inputString[readIndex])){
            /* If current character is not a vowel, copy it to outputString */
            inputString[writeIndex++] = inputString[readIndex];
        }
    }
    inputString[writeIndex] = '\0';
    printf("String without Vowels: %s \n", inputString);
     
    getch();
    return 0;
}
 
/*
 * Function to check whether a character is Vowel or not
 * Returns 1 if character is vowel Otherwise Returns 0 
 */
int isVowel(char ch){
    /* Check if character is lower case or upper case alphabet
    *  For any non-alphabet character return 0
    */
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        if(ch >= 'A' && ch <= 'Z'){
            ch = ch + ('a' - 'A');
        }
        /* Check if character(ch) is a vowel */
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
            return 1;
        }     
    } 
    return 0;
}

Program Output

Enter a string
without extra memory
String without Vowels: wtht xtr mmry

C Program to Find Frequency of Characters in a String

C Program to Find Frequency of Characters in a String

We first take a string as input from user. Input string may contain any ASCII characters like lower and upper case alphabets, space characters etc. There are 256 ASCII characters and their corresponding integer values are from 0 to 255. We have to count the frequency of characters in input string.

For Example
Input String : Apple
A : 1 times
e : 1 times
l : 1 times
p : 2 times

C program to count frequency of characters of a string

In this program, we first take a string as input from user using gets function. We will use an integer array of length 256 to count the frequency of characters. We initialize frequency array element with zero, which means initially the count of all characters are zero. We scan input string from index 0 till null character and for every character we increment the array element corresponding to it’s ASCII value.

For Example
A’s ASCII value is 65
frequency[‘A’]++ is equal to frequency[65]++
Every index in frequency array corresponds to a character’s frequency whose ASCII value is equal to index.

Finally we scan frequency array form index 0 to 256 and prints the frequency of the characters whose corresponding value in frequency array is non-zero.

C Program to Find Frequency of Characters in a String

/*
* C Program to count frequency of characters in string
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    char inputString[100];
    /* Declare a frequency counter array of size 256 
     * (for all ASCII characters) and initialize it with zero 
     */
    int index, frequency[256] = {0};
    printf("Enter a String\n");
    gets(inputString);
    for(index=0; inputString[index] != '\0'; index++){
        /* Populate frequency array */
        frequency[inputString[index]]++;
    }
    /* Print characters and their frequency */
    printf("\nCharacter   Frequency\n");
    for(index=0; index < 256; index++){
        if(frequency[index] != 0){
            printf("%5c%10d\n", index, frequency[index]);                    
        }
    }
 
    getch();
    return 0;
}

Program Output

Enter a String
Tech-Crash-Course

Character   Frequency
    -         2
    C         2
    T         1
    a         1
    c         1
    e         2
    h         2
    o         1
    r         2
    s         2
    u         1

C Program to Find Sum of All Even Numbers Between 1 to N using For Loop

C Program to Find Sum of All Even Numbers Between 1 to N using For Loop
  • Write a C program to find sum of all even numbers between 1 to N using for loop.
  • Wap in C to print sum of all even numbers between 1 to 100 using for loop.

Required Knowledge

C program to find sum of all even numbers between 1 to N using for loop

C Program to Find Sum of All Even Numbers Between 1 to N using For Loop

#include <stdio.h>  
   
int main() {  
    int counter, N, sum = 0;  
    /* 
     * Take a positive number as input form user 
     */
    printf("Enter a Positive Number\n");
 scanf("%d", &N);
   
    for(counter = 1; counter <= N; counter++) {  
        /* Even numbers are divisible by 2 */ 
        if(counter%2 == 0) { 
            /* counter is even, add it to sum */
            sum = sum + counter;  
        }  
    }
    printf("Sum of all Even numbers between 1 to %d is %d", N, sum);
 
    return 0;  
}

Output

Enter a Positive Number
9
Sum of all Even numbers between 1 to 9 is 20

C Program to Find Frequency of Each Element of Array

  • Write a C program to count frequency of all array elements using for loop.
  • How to find frequency of each elements in Array

Required Knowledge

Algorithm to count frequency of each element in an array
Let inputArray is an integer array having N elements.

  • We will declare another array countArray of same size as inputArray. We will use countArray to store the count of every array element and to keep track whether we had already counterd the frequency of current element or not(in case of duplicate elements in array).
  • If countArray[i] == -1, it means we have not counted frequency of inputArray[i] yet and If countArray[i] == 0, it means we have already counted frequency of inputArray[i]
  • Initialize each element of countArray to -1.
  • Using a for loop, we will traverse inputArray from 0 to N-1. and count the frequency of every of every element.
  • If countArray[i] == -1 for current element inputArray[i], then we store the frequency in countArray otherwise we don’t store as frequency for this element is already calculated.

C program to count frequency of each element of an array

C Program to Find Frequency of Each Element of Array

/* 
 * C Program to count frequency of each Array element  
 */ 
   
#include <stdio.h>  
   
int main() {  
    int inputArray[100], countArray[100];  
    int elementCount, i, j, count;  
   
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers\n", elementCount);
     
    /* Read array elements */
    for(i = 0; i < elementCount; i++){
        scanf("%d", &inputArray[i]);
        countArray[i] = -1;
    }
   
    /* 
     * for any element inputArray[i], If countArray[i] = -1, 
     * that means frequency is not counted for this number yet 
     * and countArray[i] = 0 means frequency is already 
     * counted for this number.
     */ 
    for(i = 0; i < elementCount; i++) {  
        count = 1;  
        for(j = i+1; j < elementCount; j++) {  
            if(inputArray[i]==inputArray[j]) {
                countArray[j] = 0;    
                count++;
            }  
        }  
        if(countArray[i]!=0) {  
            countArray[i] = count;  
        }  
    }  
   
    /* Print count of each element */   
    for(i = 0; i<elementCount; i++) {  
        if(countArray[i] != 0) {  
            printf("Element %d : Count %d\n", inputArray[i], countArray[i]);  
        }  
    }  
   
    return 0;  
}

Output

Enter Number of Elements in Array
6
Enter 6 numbers
1 3 4 2 3 1
Element 1 : Count 2
Element 3 : Count 2
Element 4 : Count 1
Element 2 : Count 1

C Program to Find Sum of All Odd Numbers Between 1 to N using For Loop

C program to find sum of all odd numbers between 1 to N using for loop
  • Write a C program to find sum of all odd numbers between 1 to N using for loop.
  • Wap in C to print sum of all odd numbers between 1 to 100 using for loop.

Required Knowledge

C program to find sum of all odd numbers between 1 to N using for loop

#include <stdio.h>  
   
int main() {  
    int counter, N, sum = 0;  
    /* 
     * Take a positive number as input form user 
     */
    printf("Enter a Positive Number\n");
    scanf("%d", &N);
   
    for(counter = 1; counter <= N; counter++) {  
        /* Odd numbers are not divisible by 2 */ 
        if(counter%2 == 1) { 
            /* counter is odd, add it to sum */
            sum = sum + counter;  
        }  
    }
    printf("Sum of all Odd numbers between 1 to %d is %d", N, sum);
 
    return 0;  
}

Output:

Enter a Positive Number
9
Sum of all Odd numbers between 1 to 9 is 25

C Program to Check a Number is Odd or Even Using Conditional Operator

C Program to Check a Number is Odd or Even Using Conditional Operator
  • Write a C program to check whether a number is odd or even using conditional operator.
  • Wap in C to check whether a number is odd or even using ternary operator

Required Knowledge

In this program, we will use conditional operator to check a number is odd or even.

C program to check whether a number is odd or even using conditional operator

C program to check whether a number is odd or even using conditional operator

#include <stdio.h>  
   
int main() {  
    int num, isEven;  
   
    /* Take a number as input from user
  using scanf function */
    printf("Enter an Integer\n");  
    scanf("%d", &num); 
     
    /* Finds input number is Odd or Even 
 using Ternary Operator */ 
    isEven =  (num%2 == 1) ? 0 : 1;  
     
    if(isEven == 1)
        printf("%d is Even\n", num);
    else
        printf("%d is Odd\n", num);
   
    return 0;  
}

Output

Enter an Integer
3
3 is Odd
Enter an Integer
4
4 is Even

C Program to Find Maximum of Three Numbers

C program to find maximum of three numbers using If Else statement
  • Write a C program to read three numbers and find maximum of three numbers using if else statement.
  • Wap in C to find largest of three numbers using function.

Required Knowledge

We will first take three numbers as input from user using scanf function. Then we print the maximum of three numbers on screen.

C program to find maximum of three numbers using If Else statement

It first finds largest of first two numbers and then compares it with third number.

C program to find maximum of three numbers using If Else statement

/** 
 * C program to find maximum of three numbers using 
 * if else statement
 */ 
#include <stdio.h>  
   
int main()  {  
    int a, b, c, max;  
    /* 
     * Take three numbers as input from user 
     */ 
    printf("Enter Three Integers\n");  
    scanf("%d %d %d", &a, &b, &c);  
     
    if(a > b){
        // compare a and c
        if(a > c)
            max = a;
        else
            max = c;
    } else {
 // compare b and c
        if(b > c)
            max = b;
        else
            max = c;
    }
   
    /* Print Maximum Number */ 
    printf("Maximum Number is = %d\n", max);  
   
    return 0;  
}

Output

Enter Three Integers
2 8 4
Maximum Number is = 8

C program to find largest of three numbers using function

Function getMax takes two numbers as input and returns the largest of two numbers. We will use this function to find largest of three numbers as follows:

C program to find largest of three numbers using function

/** 
 * C program to find maximum of three numbers using
 * function operator 
 */ 
#include <stdio.h>  
 
/*
 *It returns Maximum of two numbers
 */
int getMax(int num1, int num2) {
    if (num1 > num2){
        return num1;
    } else {
        return num2;
    }
}
 
int main()  {  
    int a, b, c, max;  
    /* 
     * Take three numbers as input from user 
     */ 
    printf("Enter Three Integers\n");  
    scanf("%d %d %d", &a, &b, &c);  
     
    max = getMax(getMax(a, b), c);
   
    /* Print Maximum Number */ 
    printf("Maximum Number is = %d\n", max);  
   
    return 0;  
}

Output

Enter Three Integers
32 45 87
Maximum Number is = 87