Author: admin

  • C++ Structures

    In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the different data type variables are represented by a single name.

    Unlike an array that holds the data of the same type, the structure holds the data of different types in a contiguous memory location. It begins with the keyword struct.

    Suppose, we want to keep the record of students with their name(string), roll_no(int), address(string), Score(float) and these are of different data types. So instead of creating a different variable, again and again, we can simply create members for each student with their respective data.

    Syntax of struct in C++:

    The keyword used to define the structure is struct.

    struct structure_name 
    {  
        data_type member1;  
        data_type member2;
        data_type member3; 
        ... 
        ...  
    };  

    Consider basic data of student: name, roll_no, score.
    student structure is declared in the following way:

    struct student
    {
      char name[30];
      int roll_no;
      float score;
     };

    Declaring Structure variable

    It means creating an instance of a structure in order to access the members of the structure. Once the structure student is declared, its variable is created by adding the following in the main function.

    struct student s1, s2;

    s1 and s2 are structure variables of the type Student. These are like the objects for the student just like an object in C++. When these variables are created, memory is allocated by the compiler.


    Accessing Struct Members

    The structure variable can be accessed by using a dot (.) operator.

    Suppose you want to access the score of an s1 student and assign it with a value of 80. This work can be performed in the following way.

    s1.score = 80;


    Example of C++ Structure

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct Student
    {
        char name[50];
        int roll;
        float score;
    };
    
    int main()
    {
        Student s1, s2;
        
        //Accessing Student members
        strcpy(s1.name, "John Mac");
        s1.roll = 101; 
        s1.score = 92.15;
        
        strcpy(s2.name, "Mickey Macguel");
        s2.roll = 102; 
        s2.score = 80;
        
        //Display student 1 Info   
       cout << "Student 1 Information" <<endl;    
       cout << "Name: " << s1.name <<endl;    
       cout << "Roll No: " << s1.roll <<endl;
       cout << "Score: " << s1.score <<endl <<endl;
       
       //Display student 2 Info
       cout << "Student 2 Information" <<endl; 
       cout << "Name: " << s2.name <<endl;    
       cout << "Roll No: " << s2.roll <<endl;
       cout << "Score: " << s2.score <<endl;
        
    
        return 0;
    }

    Output:

    Student 1 Information
    Name: John Mac
    Roll No: 101
    Score: 92.15

    Student 2 Information
    Name: Mickey Macguel
    Roll No: 102
    Score: 80


    After learning structure, go through the following topics in structure:


  • C++ Static Keyword

    Static is a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a function or class or with the static members of a class such as Class objects and Functions in a class.

    When is used with any of the mentioned above, it cannot be modified in the future. static elements are allocated storage only once in a program lifetime.

    Static Keyword can be used with the following in c++.

    1. Static variables in a functions
    2. Static Class Objects
    3. Static member Variable in class
    4. Static Methods in a class

    1. Static variables in a Function

    A variable declared static in a program is allocated to a static storage area. The static variables are only initialized only once in a program even if it is called multiple times in a program. It is useful when you do need to reset the counter even after multiple calls of the function.

    Example of a static variable inside a function in c++

    #include <iostream>
    using namespace std;
      
    void counter()
    { 
        // static variable
        static int count = 0;
        cout << count << " ";
        
        //updating counter but does not reset
        count++;
    }
      
    int main()
    {
        for (int i=0; i<=3; i++)    
            counter();
        return 0;
    }

    Output:

    //Output
    0 1 2 3

    2. Static class Objects

    The class objects are declared as static works the same in the same way as static variables. The objects declared static are allocated to a static storage area and have a scope till the lifetime of the program.

    Example of a static class object in c++

    #include <iostream>
    using namespace std;
      
    class Eg
    {
        int i;
        public:
            Eg()
            {
                i=0;
                cout << "CONSTRUCTOR" << endl;
            }
            ~Eg()
            {
                cout << "DESTRUCTOR" << endl;
            }
    };
    
    int main()
    {
        int x = 0;
        if(x == 0)
        {
            static Eg eg;
        }
        cout << "END" << endl;
    }

    Output:

    CONSTRUCTOR
    END
    DESTRUCTOR

    As you can see the destructor is called after the end of the main function because the scope of static object is throughout the lifetime of the program.


    3. Static Data Members inside a class:

    Static variables of a class are shared by all the objects and these members are initialized once and allocated a separate single static storage. Each object does not have a separate copy of those variables.

    Also, static data members are not initialized using the constructor as they are not dependent on variable initialization.

    Example of a Static Data Members inside a class in c++

    #include<iostream>
    using namespace std;
      
    class Abc
    {
       public:
         static int x; //static member
          
         Abc()
         {
            // Do nothing
         };
    };
      
    int Abc::x = 1;
    
    int main()
    {
      Abc obj;
    
      obj.x = 10;
        
      // display x
      cout << obj.x;   
    }

    Output:

    //Output
    10

    As you can see, it has to be explicitly initialized outside the class with a class name and scope resolution operator.


    4. Static function in a class

    Just like static data variables in a class, the static function also does not depend on the object of the class. We cannot call or invoke the static function of a class using object or ‘.‘ operator. But can be invoked using the class name and the scope resolution operator.

    However, these functions can only access the static data members and static member functions as it does not have ‘this‘ keyword.

    Example of a Static function in a class in c++

    #include<iostream>
    using namespace std;
      
    class Abc
    {
      public:
        // static member function
        static void display()
        {
            cout<<"This is simple2code.com";
        }
    };
      
    int main()
    {
        //static member function invoked
        Abc :: display();
    }

    Output:

    //Output
    This is simple2code.com

  • C++ Templates

    Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions).

    A single class or function is created to work with different data types using templates. We pass the data types in a function as a parameter so that repetition of the same code can be avoided for different data types.

    The passed parameter is called the template parameter. It is the same as the regular parameter but here instead of values we pass different data types.

    The templates can be represented in two different ways.

    • Function Templates
    • Class Templates
    templates in C++

    Function Templates

    We can create the function template in order to use it for different data types such as float, int, double, etc. For example, suppose we create a function called mul() for the multiplication, in normal function this function is used for specific data-types values but with templates we can pass any data types into the function.

    It makes a better approach because we can write less code and save time from the repetition of writing code that is used to do the same work.

    Syntax of Function Template:

    template <class T> T func_name(T parameter_list) 
    {
       // body of function
    } 
    • T is a template argument or placeholder name for a data type (int, float, etc). The compiler automatically replaces it with the actual data type.
    • class is a keyword used for generic type and it can replace with typename.

    Example of function templates in C++

    #include <iostream>  
    using namespace std; 
    
    template<class T> T mul(T &num1,T &num2)  
    {  
        T product = num1 + num2;  
        return product;  
    }  
    
    int main()  
    {  
      int a = 4, b = 2;  
      float x = 3.3, y = 2.3;  
      
      cout<<"Multiplication of a and b: "<< mul(a,b) << endl;
      cout<<"Multiplication of x and y: "<< mul(x,y);  
      return 0;  
    }  

    Output:

    Multiplication of a and b: 6
    Multiplication of x and y: 5.6

    Class Templates

    Just like a function template, we can create a template for the class for generic operations that is independent of any data type.

    It is required when we need the same implementation for many classes but for different data types and also avoided repetition of the same code.

    Syntax of class Template:

    template<class T> class class_name   
    {
       ....
       ....
       ......
    } 

    Creating class template object.

    The data type is mentioned inside <> while creating an object for the class template. For example:

    //Syntax
    class_name<dataType> classObject;
    
    //Example
    class_name<int> classObject;
    class_name<float> classObject;

    let us go through an example of a class template in C++ to understand more.

    Example of class templates in C++

    #include <iostream>
    using namespace std;
    
    template <class T>
    class Operation
    {
        public:
            T num1 = 10.5, num2 = 5.2;
    
        	void print()
        	{
        		cout << "Addition: " << add() << endl;
        		cout << "Subtraction: " << sub() << endl;
        		cout << "Product: " << mul() << endl;
        		cout << "Division: " << divide() << endl;
        	}
        	
        	T add() { 
        	    return num1 + num2; 
        	    
        	}
        	T sub() { 
        	    return num1 - num2; 
        	    
        	}
        	T mul() { 
        	    return num1 * num2; 
        	    
        	}
        	T divide() { 
        	    return num1 / num2; 
        	    
        	}
    };
    
    int main()
    {
    	Operation<int> intOb;
    	Operation<float> floatOb;
    	
    	cout << "INTEGER:" << endl;
    	intOb.print();
    	
    	cout << endl << "FLOAT:" << endl;
    	floatOb.print();
    	
    	return 0;
    }

    Output:

    INTEGER:
    Addition: 15
    Subtraction: 5
    Product: 50
    Division: 2
    
    FLOAT:
    Addition: 15.7
    Subtraction: 5.3
    Product: 54.6
    Division: 2.01923

    As you can see that the result is shown according to the data type passed while creating class objects (intOb, floatOb).


  • C++ Friend Function

    There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming i.e. data hiding. However, these rules can be broken by a friend function.

    A function defined with a friend keyword is called a friend function. It can access both the private and protected members from outside the class.

    Declaration of friend function:

    class class_name    
    {    
        .......
        friend data_type function_name(arguments);
    
        .......
    };   
    • A friend can be a member’s function, function template, or function, or a class or class template,  in which case the entire class and all of its members are friends.
    • Although the friend function appears inside the class, they are not membersfunction.
    • They can be declared anywhere in the class.

    Example: C++ program for a friend function

    #include <iostream>    
    using namespace std;    
    class Number    
    {    
        private:    
            int num;    
        public:    
            Number(): num(0) { }    
            friend int add(Number); //friend function    
    };    
    int add(Number n)    
    {    
       n.num += 20;    
        return n.num;    
    }    
    int main()    
    {    
        Number num;    
        cout<<"Number: "<< add(num)<<endl;    
        return 0;    
    }   

    Output:

    Number: 20

    C++ friend class

    friend keyword is also used to declare a friend class in C++. When a class is declared a friend class, all the member functions of the friend class become friend functions.

    class Two;
    
    class One {
       // class Two is a friend class of class One
       friend class Two;
       ... .. ...
    }
    
    class Two{
       ... .. ...
    }

    In the above example, class Two is a friend class of class One indicating that we can access all the members of class One from class Two. However, vice-versa is not granted.

    Example: C++ program for a friend class.

    #include <iostream>  
    using namespace std;  
      
    class One  
    {  
        int num = 10;  
        friend class Two; // friend class.  
    };  
    
    class Two  
    {  
      public:  
        void print(One &o)  
        {  
            cout<<"Num = "<<o.num;  
        }  
    };  
    int main()  
    {  
        One o;  
        Two t;  
        t.print(o);  
        return 0;  
    }  

    Output:

    Num = 10

  • this Pointer in C++

    In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the current object of the class. Also, only the member functions have this pointer.

    Members like friend functions do not have this pointer as they are not members of the class.

    Example of this pointer in C++

    Let us see an example of C++ this pointer that refers to the fields of the current class.

    #include <iostream>  
    using namespace std; 
    
    class Student 
    {  
       public:  
           int roll; //fields      
           string name;  
           float score;
           
           Student(int roll, string name, float score)    
           {    
               this->roll = roll;    
               this->name = name;    
               this->score = score;   
           }    
           void print()    
            {    
               cout << "Roll No: " << roll << endl;    
               cout << "Name: " << name << endl;    
               cout << "Score: " << score << endl << endl;    
            }    
    };  
    
    int main(void) 
    {  
        //creating Object
        Student s1 = Student(1101, "John", 80);  
        Student s2 = Student(1102, "Emma", 95); 
        
        s1.print();    
        s2.print();    
        
        return 0;  
    }  

    Output:

    Roll No: 1101
    Name: John
    Score: 80


    Roll No: 1102
    Name: Emma
    Score: 95


  • Inheritance in C++

    Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one class to another class. It is like a child and parent where the child possesses the properties of the parent.

    • The class which inherits the members of another class is called the derived class (child/sub).
    • The class whose members are inherited is called the base class (super/parent).

    Importance of Inheritance:

    • For Code Reusability.
    • For Method Overriding (to achieve runtime polymorphism).

    A basic example of Inheritance.

    class Animal 
    {
        // eat() function
        // sleep() function
    };
    
    class Cat: public Animal 
    {
        // sound() function
    };

    As you can see in the above example Cat class inherits from the Animal class which means that the Cat class has can access the eat() and sleep() methods of Animal and also has its own additional method sound().

    Modes of Inheritance.

    It is the visibility mode that is used while deriving a parent class. such as:

    class derived_class_name : visibility-mode parent_class_name  
    {  
        // derived class members.  
    }  

    We can derive the parent class publicly or privately.

    Public mode: When the derive class publicly inherits the base class then the public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

    Private mode: When the derive class privately inherits the base class then the public members of the base class become the private members of the derived class.


    Types Of Inheritance

    C++ supports the following five types of inheritance.

    1. Single inheritance
    2. Multiple inheritance
    3. Multilevel inheritance
    4. Hierarchical inheritance
    5. Hybrid inheritance

    1. Single Inheritance

    It refers to an inheritance where a child inherits a single-parent class.
    In the diagram below, class B inherits class A.

    single Inheritance

    Syntax:

    class subclass_name : access_mode base_class_name
    {
      //body of subclass
    };

    Example: C++ example for single inheritance.

    #include <iostream>  
    using namespace std; 
    
    class One  
    {  
        public:  
            int num1 = 10;  
            int num2 = 20; 
            
            int add()  
            {  
              int sum = num1 + num2;  
              return sum;  
            }     
    };  
      
    class Two : public One  
    {  
        public:  
            void print()  
            {  
              //Inheriting method
              int result = add();  
              cout <<"Result of addition: "<< result << endl;  
            }  
    };  
    
    int main()  
    {  
       Two two;  
       //Inheriting Field
       cout << two.num1 << endl;
       two.print();  
      
        return 0;  
    }  

    Output:

    10
    Result of addition: 30


    2. Multiple Inheritance

    Multiple inheritance is the process where the single class inherits the properties from two or more classes.

    In the diagram below, class C inherits from both Class A and Class B.

    multiple Inheritance

    Syntax:

    class subclass_name : specifier base_class1, specifier base_class2, ....
    {
      //body of subclass
    };

    Example: C++ example for multiple inheritance.

    #include <iostream>  
    using namespace std;  
    
    class One  
    {  
        protected:  
            int num1;  
        public:  
            void getNum1(int n)  
            {  
                num1 = n;  
            }  
    };  
      
    class Two  
    {  
        protected:  
            int num2;  
        public:  
            void getNum2(int n)  
            {  
                num2 = n;  
            }  
    };  
    
    //Inherits from One and Two
    class Three : public One, public Two  
    {  
       public:  
         void print()  
         {  
            int sum = num1 + num2;
            cout << "num1 : " << num1 << endl;  
            cout << "num2 : " << num2 << endl;  
            cout << "Result of sum: " << sum;  
         }  
    };  
    int main()  
    {  
       Three th;  
       th.getNum1(5);  
       th.getNum2(10);  
       th.print();  
      
        return 0;  
    }  

    Output:

    num1 : 5
    num2 : 10
    Result of sum: 15


    3. Multilevel Inheritance

    In this type inheritance derived class is created from another derived class. In multilevel inheritance, one class inherits from another class which is further inherited by another class. The last derived class possesses all the members of the above base classes.

    Multilevel Inheritance

    Example: C++ example for multilevel inheritance.

    #include <iostream>  
    using namespace std;  
    
    class Animal 
    {  
       public:  
          void sleep() 
          {   
            cout<<"Animal sleep"<<endl;   
          }    
       }; 
       
    class Dog: public Animal   
    {    
       public:  
          void bark()
          {  
            cout<<"Dog Barking"<<endl;   
          }    
    };
    
    class Puppy: public Dog   
    {    
       public:  
          void puppies() 
          {  
            cout<<"Puppies";   
          }    
     };  
     
    int main(void) 
    {  
        Puppy d;  
        d.sleep();  
        d.bark();  
        d.puppies();
         
         return 0;  
    }  

    Output:

    Animal sleep
    Dog Barking
    Puppies


    4. Hierarchical Inheritance

    The process of deriving more than one class from a base class is called hierarchical inheritance. In other words, we create more than one derived class from a single base class.

    hierarchical Inheritance

    Syntax:

    class One  
    {  
        // Class One members  
    }    
    class Two : public One   
    {  
        // Class Two members.  
    }  
    class Three : public One  
    {  
        // Class Three members  
    }   
    class Four : public One   
    {  
        // Class Four members  
    }  

    Example: C++ example for hierarchical Inheritance.

    #include <iostream>  
    using namespace std;  
    
    class Animal 
    {  
       public:  
          void sleep() 
          {   
            cout<<"Animal sleep"<<endl;   
          }    
       }; 
       
    class Dog: public Animal   
    {    
       public:  
          void dogSound()
          {  
            cout<< "Barks" <<endl;   
          }    
    };
    
    class Cat: public Animal   
    {    
       public:  
          void catSound() 
          {  
            cout<< "Meow" << endl;   
          }    
     };  
     
    int main(void) 
    {  
        Cat c;  
        c.sleep();  
        c.catSound();  
        Dog d;  
        d.sleep();  
        d.dogSound();
         
         return 0;  
    }  

    Output:

    Animal sleep
    Meow

    Animal sleep
    Barks


    5. Hybrid (Virtual) Inheritance

    Hybrid inheritance is the combination of more than one type of inheritance. In the diagram below is the combination of hierarchical Inheritanceand multiple inheritance.

    hybrid Inheritance

    Example: C++ example for hierarchical Inheritance.

    #include <iostream>  
    using namespace std; 
    
    class One 
    {  
        protected:  
            int a = 20;  
        public:  
            void getOne()  
            {  
               cout << "Value of a: " << a <<endl;   
            }  
    };  
    
    class Two   
    {  
        protected:  
            int b = 30;  
        public:  
            void getTwo()  
            {  
                cout << "Value of b: " << b <<endl;
            }  
    };
      
    class Three : public One   
    {  
        protected:  
            int c = 50;  
        public:  
            void getThree()  
            {  
                cout << "Value of c: " << c <<endl;  
            }  
    };  
      
      
    class Four : public Two, public Three  
    {  
        protected:  
           int d;  
           
        public:  
            void add()  
            {  
              getOne();  
              getTwo();  
              getThree();  
              cout << "Sum : " << a + b + c;  
            }  
    };  
    int main()  
    {  
        Four fr;  
        fr.add();  
        return 0;  
    }  

    Output:

    Value of a: 20
    Value of b: 30
    Value of c: 50
    Sum : 100


    Ambiguity in Inheritance

    When there is a function present with the same name in two or more base classes then during multiple inheritances or single inheritance an ambiguity can occur in such cases.

    Example: C++ example for ambiguity in multiple inheritance.

    #include <iostream>  
    using namespace std; 
    
    class One 
    {   
        public:  
            void display()  
            {  
               cout << "Class One" << endl;   
            }  
    };  
    
    class Two   
    {   
        public:  
            void display()  
            {  
                cout << "Class Two" << endl;
            }  
    };
      
    class Three : public One, public Two   
    {   
        public:  
            void print()  
            {  
                display();
            }  
    };  
      
    int main()  
    {  
        Three th;  
        th.print(); 
        
        return 0;  
    }  

    There are display() functions in both the classes of One and Two. The following error will be displayed after execution.

    error: reference to ‘display’ is ambiguous
             display();

    To overcome this problem we need to do the following changes in Class Three by using the class resolution operator (::).

    class Three : public One, public Two   
    {   
        public:  
            void print()  
            {  
                One :: display(); 
                Two :: display();
            }  
    }; 

  • C Program to calculate Simple Interest

    In this tutorial, we will write a program to find the Simple Interest in C. And the formula to calculate the Simple Interest (SI) is:

    SI = ((p * r * t) / 100))
    where,
    p = Principal
    r = Rate of Interest
    t = Time Period

    Explanation:
    The program takes the user input for Principal, Rate, and Time, calculates the result, and displays the result on the screen.


    C Program to calculate the Simple Interest

    //Program to calculate simple interest in C
    
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
      float p, r, t;
      float SI;
      
      printf("Enter the principal: ");
      scanf("%f", &p);
      
      printf("Enter the rate: ");
      scanf("%f", &r);
      
      printf("Enter the time: ");
      scanf("%f", &t);
      
      //calculating
      SI = (p * r * t) / 100;
      
      //Display the result
      printf("\nThe simple interest is: %.3f", SI);
    
      return 0;
    }

    Output:

    Enter the principal: 34000
    Enter the rate: 30
    Enter the time: 5

    The simple interest is: 51000.00

    The above source code to calculate the Simple Interest in C is compiled and ran successfully on a windows system. Browse through C Program to learn more.


  • C Program to Print Hello World

    This is the most basic program where the program prints the hello world. Through this, you can understand the basic structure of the C program.

    This the basic program for beginners in C. Let us go through an example.

    Program to Print “Hello World” in C

    //This is the header for standard I/O
    #include<stdio.h>
    
    //This is the main function
    int main() 
    {
      printf("Hello, World!");
    
      return 0;
    }

    Output:

    Hello, World!

    Run the above program in your C compiler and you will get the above output. You may go through the Program structure in C to understand the programming format better.


  • C Program to Multiply two numbers using Addition operator

    In this tutorial, we will write a C program to multiply using the plus operator (+). You may go through the following topics in C to understand the program better.

    Multiplication of two numbers using plus arithmetic operator:
    To find the result of the product of two numbers, we need to add the first number to the second number of times

    Example:

    Input:
    First number: 5
    Second number: 3
    
    Output:
    Multiplication by addition of 5 and 3 is: 15
    
    Solution:
    Multiplication is = 5 + 5 + 5 = 15

    C Program to Multiply number using Addition operator

    //C program to multiply two numbers using plus operator
     
    #include <stdio.h>
     
    int main()
    {
        int num1, num2;
        int result = 0, i;
         
        printf("Enter the first number: ");
        scanf("%d",&num1);
        printf("Enter the second number: ");
        scanf("%d",&num2);
         
        result=0;
    
        for(i = 1; i <= num2; i++)
            result += num1;
        
    
        printf("\nResult of Multiplication of %d and %d through Addition: %d\n", num1, num2, result);
    
        return 0;
    }

    Output:

    Enter the first number: 5
    Enter the second number: 3

    Result of Multiplication of 5 and 3 through Addition: 15


  • C Program to Find the Largest of three Numbers

    In this tutorial, we will write a basic C Program to find the greatest of three numbers. You may go through the following topics in order to understand the C program.

    Algorithm to find the largest among three numbers

    1.  Start.
    2. Read num1, num2 and num3 from user.
    3. Check num > num2, if true go to step 4 else go to step 5
    4. Check num1 > num3.
      • If true, print ‘num1 is the greatest number’.
      • If false, then print ‘num3 as the greatest number’.
      • go to step 6
    5. check if num2 > num3.
      1. If true, print ‘num2 as the greatest number’.
      2. If false, print ‘num3 as the greatest number’.
    6. Stop

    C Program to find the Largest among three Numbers

    We will find the greatest of three using if..else and if…else-if statement.

    //C program to find the biggest of three numbers using if else if statement
    
    #include <stdio.h>
    int main()
    {
        int num1, num2, num3;
        
        printf("Enter three numbers to compare.\n");
        
        printf("First Number: ");
        scanf("%d", &num1);
        printf("Second Number: ");
        scanf("%d", &num2);
        printf("Third Number: ");
        scanf("%d", &num3);
        
        if (num1 > num2)
        {
            if (num1 > num3)
            {
                printf("\nLargest number: %d \n",num1);
            }
            else
            {
                printf("\nLargest number: %d \n",num3);
            }
        }
        else if (num2 > num3)
        {
            printf("\nLargest number: %d \n",num2);
        }
        else
        {
            printf("\nLargest number: %d \n",num3);
        }
        return 0;
    }

    Output:

    Enter three numbers to compare.
    First Number: 32
    Second Number: 65
    Third Number: 19

    Largest number: 65


    Using Ternary operator

    The ternary operator works like an if..else statement in C. Syntax of the ternary operator. (?:)

    variable num1 = (expression) ? value if true : value if false

    //Using ternary Operator
    
    #include <stdio.h>
    int main()
    {
        int num1, num2, num3;
        
        printf("Enter three numbers to compare.\n");
        
        printf("First Number: ");
        scanf("%d", &num1);
        printf("First Number: ");
        scanf("%d", &num2);
        printf("First Number: ");
        scanf("%d", &num3);
        
        int greatestNum = ((num1>num2 && num1>num3) ? num1 : (num2>num3)?num2:num3);
        printf("\nLargest of three is: %d.", greatestNum);
        
        return 0;
    }

    Output:

    Enter three numbers to compare.
    First Number: 45
    Second Number: 98
    Third Number: 66

    Largest of three is: 98