Author: admin

  • Features of C

    Features and advantages of C

    • Simple and easy to learn.
    • Fast, Powerful & Efficient.
    • Procedural Language
    • Portable language.
    • Structured Programming Language
    • Rich Library.
    • Mid-Level Programming Language.
    • Pointers/Recursion.
    • Extensible

    1. Simple and easy to learn.
    C is simple and easy to learn language as it provides a structured Language approach (i.e. to break the problem into modules), its rich set of library functions, a simple set of keywords, data types, etc.

    2. Fast, Powerful & Efficient.
    The compilation and execution time of C language is fast as it provides powerful operators, variety of data types and it has a lesser inbuilt function and access to direct manipulation with the computer hardware hence makes it more efficient and the first choice for the programmers..

    3. Procedural Language
    As a procedural language means the languagethat follows, in order, a set of commands. And in C there are step by step, ordered predefined instructions that are carried out tat makes it procedural.

    4. Portable language.
    It is also known as Machine Independent Language that can be executed on different machines with particular changes, hence making it portable and Independent Programming Language.

    5. Structured Programming Language.
    C language is a structured programming language that makes the program debugging, testing, and maintenance easier. This makes users to think of a problem in terms of function modules or blocks. The collection of these modules makes a complete program.

    6. Rich Library.
    C is rich in inbuilt function and Operators that make easier for the programmer to write a complex program easily and help in faster development.

    7. Mid-Level Programming Language.
    As we understand, that C is a low-level language but however it also supports the high level language. Therefore, it binds the gap between these low and high level language, hence known as Mid-Level Language.

    A user can use C programming Language for System Programming that is writing Operating System and can be used as well as in  Application Programming that is for creating a menu-driven customer billing system.

    8. Pointers/Recursion.
    Pointers and Recursion provides a certain functionality to the program.

    • Pointers allow us to directly interact with the memory and is used for function, array, structures, etc.
    • Recursion allows us to call the function within the function, providing the re-usability of every function.

    9. Extensible.
    Last but not the least feature/advantage of C is its ability to extend itself. C Language is easy to extend due to its ability to extend or add new features in already created applications.


    Disadvantage of C

    C does not have a concept of OOPs(Object-oriented programming), namespace, constructor or destructor.

  • C – nested if statements

    This statement allows the user to use if block inside the other if block. And the inner if statement is executed only if the outer if statement’s condition is true.

    Syntax

    Syntax of nested if statement in C:

    if(condition1)
     {    
        //block of code to be executed    
        if(condition2)
        {  
          //block of code to be executed    
        }    
     } 

    nested if Flowchart:

    nested if

    Example of nested if statement in C:

    #include <stdio.h>
    
    int main()
    {
      // local variable
      int num1 = 60;
      int num2 = 90;
    
      // first if statement
      if (num1 > 50)
      {
        // if inside if statement
        if (num2 > 80)
        {
          printf("Actual value of num1 is : %d\n", num1);
          printf("Actual value of num2 is : %d\n", num2);
        }
      }
    
      return 0;
    }

    The output of nested if statement in C.

    Actual value of num1 is : 60
    Actual value of num2 is : 90


  • Java – this Keyword

    In Java, this is a reference variable, this can be used inside any method or constructor to refer to the current object.

    Following demonstrating the use of this

    volume(double w, double h, double d) 
     {  
      this.width = w; 
      this.height = h; 
      this.depth = d;
     }  

    Use of this Keyword in constructor:

    class Student
     {
      String name;
      int marks;
    
      Student(String name, int marks)
      {
       this.name = name;   //use of this
       this.marks = marks; // use of this
      }
      void display()
      {
       System.out.println("Name: " + name + " marks:" + marks);
      }
     }
     public class main
     {
      public static void main (String[] args) 
      {
       Student st = new Student("Daniel", 90);
       st.display();
      }
     } 

    Output:

    Name: Daniel marks:90

    Notethat, Java does not allow to declare two local variables with the same name inside the same or enclosing scopes, so the use of the same parameter name and data-members(instance variable) is possible because of this operator.
    this keyword used within the constructor refers to the instance variable of that constructor or class.

    In the above example, this.name = name, this.name refers to the instance variable whereas the name on the right side refers to the parameter value. and equal(=) sign assign parameter value to an instance variable. Also called Instance Variable Hiding.

    If we do not use this keyword violating the Java declaration process, we get the following result:

    class Student
     {
      String name;
      int marks;
    
      Student(String name, int marks)
      {
       name = name;   //not using of this
       marks = marks; //not using of this
      }
      void display()
      {
       System.out.println("Name: " + name + " marks:" + marks);
      }
     }
     public class main
     {
      public static void main (String[] args) 
      {
       Student st = new Student("Daniel", 90);
       st.display();
      }
     }

    Output:

    Name: null marks:0


    this Keyword: to pass as an argument in the method:

    class Method
     {
      void m1(Method obj)
      {
       System.out.println("This is method m1");
      }
      void m2()
      {
       m1(this);
      }
     }
     class Main
     {
      public static void main (String[] args) 
      {
       Method m = new Method();
       m.m2();
      }
     }

    Output:

    This is method m1


    this: Invoke current class method:

    The method of the current class can be invoked by using this keyword. If this keyword is not used then the compiler automatically adds this keyword while invoking the method.

    source code:

     class Method
     {
      void m1()
      {
       System.out.println("This is method m1");
      }
      void m2()
      {
       System.out.println("This is method m2");
       this.m1();
      }
     }
     class Main
     {
      public static void main (String[] args) 
      {
       Method m = new Method();
       m.m2();
      }
     }

    Output:

    This is method m2
    This is method m1


  • Java – StringBuffer

    StringBuffer and StringBuilder in java is a class that provides the mutable(changeable) ability.
    Since a String is immutable that is its value cannot be changed once the object is created. So to modify the String, StringBuffer and StringBuilder class is used.

    Both these classes work fine but the difference is that the StringBuffer is thread-safe whereas StringBuilder is not.

    • Far a faster process, consider using StringBuilder because it is faster than StringBuffer.
    • For safety, purposes consider using StringBuilder.

    StringBuffer Methods:

    1. StringBuffer append() method:

    This method concatenates the two string and updates the result. The method takes boolean, char, int, long, Strings, etc.

    Example for StringBuffer append() in Java:

     public class StringBufferTest
      {  
       public static void main(String args[])
       {  
        StringBuffer s =  new StringBuffer("Long ");  
        s.append("Day");//changed with Day  
        System.out.println(s);//prints the concatenated result  
       }  
      }

    Output:

     Long Day

    2. StringBuffer insert() method:

    This String inserts the new string in a specified position. We need to offset the position before the value as shown in an example.

    Example for StringBuffer insert() in Java:

     public class StringBufferTest
      {  
      public static void main(String args[])
      {  
        StringBuffer s = new StringBuffer("Long ");  
        s.insert(1,"Day");//now original string is changed  
    
        System.out.println(s);//prints HJavaello  
      }  
      }

    Output:

     LDayong 

    3. StringBuffer delete() method:

    As the name suggest, this method is used to remove/delete the string by index number. We need to specify the beginIndex and endIndex.

    Example for StringBuffer delete() in Java:

     public class StringBufferTest 
      {
      public static void main(String args[]) 
      {
        StringBuffer s = new StringBuffer("ABCDEFGHIJK");
        s.delete(4, 8); 
    
        System.out.println(s); 
      }  
      }

    Output:

    ABCDIJK

    4. StringBuffer replace() method:

    This method is used to replace a String with another String within a specified index.

    This content three values one to remove from beginIndex to endIndex and then the String value for replacement.

    Example for StringBuffer replace() in Java:

     public class StringBufferTest 
      {
       public static void main(String args[]) 
       {
         StringBuffer s = new StringBuffer("ABCDEFGHIJKJ");
         s.replace(4, 9, "xyz");
    
         System.out.println(s); 
       }  
      }

    Output:

     ABCDxyzJKJ 

    5. StringBuffer reverse() method:

    This method reverses the value of the string that is invoked.

    Example for StringBuffer reverse() in Java:

     public class StringBufferTest 
     {
      public static void main(String args[])
      {
       StringBuffer s = new StringBuffer("Sting Buffer");
       s.reverse();
    
       System.out.println(s);
      }  
     }

    Output:

     reffuB gnirtS 

    List of some other methods that are similar to string class:

    1. void ensureCapacity(int minimumCapacity):
      This method ensures that the buffer capacity is at least to minimum capacity.
    2. int capacity():
      It returns the current capacity of the string buffer.
    3. int indexOf(String str):
      The index of the first occurrence of the specified substring is returned within this string.
    4. int indexOf(String str, int fromIndex):
      The index of the first occurrence of the specified substring is returned within this string and it starts from the index specified.
    5. int lastIndexOf(String str, int fromIndex):
      The index of the last occurrence of the specified substring is returned within this string and from the specified index.
    6. void setLength(int newLength):
      This method sets the length of the string buffer.
    7. int length():
      It is used to return the length of the string buffer.
    8. String toString():
      The data represented to this string buffer is converted to a string through this method
    9. char charAt(int index):
      It returns the sequence currently represented by the string buffer that is specified the index argument.
    10. CharSequence subSequence(int start, int end):
      This method returns a new character sequence that is a sub-part of this sequence.

    There are more String methods such as, String substring(int start, int end),  String substring(int start),  void setCharAt(int index, char ch), int lastIndexOf(String str), void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin).


  • C – Variables

    What are variables in C?

    The variable is the basic unit of storage that holds the value while the program is executed. We can also say that it is a name given to the memory location. A variable is defined by data-types provided by C.

    It may belong to any data-types in C that can be char, int, float, double, and void.

    Rules for naming C Variables:

    • A variable name must begin with a letter or underscore.
    • They are case sensitive that is Score and score are not the same.
    • They can be constructed with digits, letters and underscore.
    • No special symbols are allowed other than underscores.
    • A variable name cannot be a keyword. Example, int cannot be a variable name as it is an in-built keyword.

    Variable Definition, Declaration, and Initialization in C

    Defining a variable:

    Defining a variable means the compiler has to now assign storage to the variable because it will be used in the program. It is not necessary to declare a variable using an extern keyword, if you want to use it in your program. You can directly define a variable inside the main() function and use it.

    To define a function we must provide the data type and the variable name. We can even define multiple variables of the same data type in a single line by using a comma to separate them.

    type variable_list_name, where type must be the valid C data-type.

    Example:

    int i, j, k; 
    char c, ch; 
    float f, amount, salary; 
    double db;

    Declaration of Variables:

    The declaring variable is useful for multiple files and the variable is declared in one of the files and is used during linking the file.

    Declaration of variables must be done before they are used in the program. Declaration does the following things.

    • It tells the compiler what the variable name is.
    • It specifies what type of data the variable will hold.

    A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program.

    The user needs to use the keyword extern to declare a variable at any place. Though you can declare variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.

    extern int a;
    extern float b;
    extern double c,d;

    Initialization of Variables:

    initialization is simply giving the variable a certain initial value which may be changed during the program.

    A variable can be initialized and defined in a single statement, like:

     int a = 20; 

    Let see an example for declaring, defining and initializing in C:

    #include <stdio.h>
    
    // Variable declaration:
    extern int x, y;
    extern int z;
    extern float f;
    
    int main () {
    
       /* variable definition */
       int x, y;
       int z;
       float f;
     
       /* actual initialization */
       x = 30;
       y = 20;
      
       z = x + y;
       printf("value of z: %d \n", z);
    
       f = 70.0/3.0;
       printf("value of f: %f \n", f);
     
       return 0;
    }

    The output:

    value of z : 30
    value of z : 23.333334

  • Java – What is Static and Dynamic Binding?

    The Association of the method call to the method body is known as binding. There are two types of binding in java:

    • Static binding.
    • Dynamic bindinng.

    1. Static Binding or Early Binding in Java:

    The binding that resolved at compile time is known as Static Binding or Early Binding. There are three methods that cannot be overridden and the type of the class is determined at the compile time.

    They are static, private and final methods. They are bind at compile time. In this type, the compiler knows the type of object or class to which object belongs to during the execution.

    Example of Static Binding or Early Binding in Java:

    class SuperClass 
     { 
        static void display() 
        { 
            System.out.println("Displaying superclass."); 
        } 
     } 
     class SubClass extends SuperClass 
     { 
        static void display() 
        { 
            System.out.println("Displaying subclass."); 
        } 
     } 
     public class StaticTest 
     {
      public static void main(String[] args) 
      { 
        //Reference is of SuperClass type and object is SuperClass type
        SuperClass sup = new SuperClass();
        
        //Reference is of SuperClass type and object is SubClass type
        SuperClass sub = new SubClass(); 
        
        sup.display(); 
        sub.display(); 
      } 
     }

    Output of Static Binding:

     Displaying superclass.
     Displaying superclass.

    In this above example, two objects are created (sup, sub) of different types referring to the same class type(SuperClass type). Here the display() method of SuperClass is static, and therefore compiler knows that it will not be overridden in subclasses and also knows which display() method to call and hence no ambiguity.


    2. Dynamic Binding or Late Binding in Java:

    The binding that resolved at run time is known as Static Binding or Late Binding. The perfect example of this would be Overriding. Consider a method overriding where parent class and child class has the same method.

    Example Dynamic Binding in Java:
    In the following example, the overriding of a method is possible because no methods are declared static, private, and final.

    class SuperClass 
     { 
      void display() 
      { 
        System.out.println("Displaying superclass."); 
      }  
     } 
     class SubClass extends SuperClass 
     { 
      void display() 
      {  
        System.out.println("Displaying subclass."); 
      } 
     } 
     public class StaticTest 
     {
      public static void main(String[] args) 
      { 
       //Reference is of SuperClass typ and object is SuperClass type
       SuperClass sup = new SuperClass();
    
       //Reference is of SuperClass typ and object is SubClass type
       SuperClass sub = new SubClass(); 
    
       sup.display(); 
       sub.display(); 
      } 
     }

    Output of Dynamic Binding:

     Displaying superclass.
     Displaying subclass.

  • C – Program Structure

    Before we start the basic building blocks of the C programming language, we must know the Program Structure of C for the building of programs.

    A C program basically consists of the following parts:

    • Preprocessor Commands.
    • Functions.
    • Variables.
    • Statement & Expressions.
    • Comments.

    Let see the basic structure below to print Hello World:

     #include <stdio.h>
    
     int main() 
     {
      /* This is my First Program */
      // First Program
        printf("Hello, World! \n");
        return 0;
     }

    Now, let’s know the various parts of the above example:

    1. #include <stdio.h> is a preprocessor command, the first line of the program that adds the stdio.h file before the program compilations.
    2. Next is int main() which is the main function where the program execution begins.
    3. /*...*/ or // is a comment box that will not be executed by the compiler.
    4. printf(...)is a function in C that displays the message “Hello, World!” on the screen. The message needs to be under " ".
    5. return 0; in a program terminates the main() function and returns the value 0.
  • Java – Strings

    A string is defined as a series of characters or an array of characters in java. They are present within the double quote.

    Create a String:

    One way to create a String is to directly declare it within the double quote that is by String Literal:

    String s = "SimpleToCode";

    Like any other object, it is possible to create a string object with the help of a new keyword.
    such as:

    char[] s = {'S','i','m','p','l','e','T','o','C','o','d','e'};  
    String obj = new String(s);

    Example:

     public class StringTest 
     {
    
      public static void main(String args[]) 
      {
        String s = "Simple To Code";
        System.out.println( s );
      }
     }

    Output:

     Simple To Code

    Example of string with object in Java:

     public class StringTest 
     {
    
      public static void main(String args[]) 
      {
        char[] s = {'S','i','m','p','l','e',' ','T','o',' ','C','o','d','e'};  
        String obj = new String(s);
        System.out.println( obj );
      }
     }

    Output:

    Simple To Code

    Note that: it also takes blank space as a character.
    Also, string objects are immutable that is their values are unchangeable once the object is created
    .


    String Methods:

    Java provides Various String Methods for String Operation such as compare(), concat(), equals(), length(), compareTo() and amany more.

    List of some string Methods supported by Java are:

    1. char charAt(int index):
      This method returns the character at the specified index.
    2. int compareTo(String secondString):
      The method compares the two strings depending upon their Unicode Value.
    3. int compareTo(Object o):
      Compares the present String to another object.
    4. String concat(String str):
      This method concatenates the String ‘str’ at the end of the string.
    5. boolean equals(Object obj):
      This method compares the string to the specified object.
    6. boolean endsWith(String suffix):
      This method checks or tests whether the string ends with the specified suffix or not.
    7. boolean startsWith(String prefix):
      It checks for the string to have a specified prefix and depending on that it returns a true or false boolean value.
    8. int compareToIgnoreCase(String str):
      It compares the string the same as compareTo() but here it ignores the case of the string.
    9. int hashCode():
      This method returns the hash code of the string.
    10. int indexOf(int ch):
      The index of the first occurrence of the specified character ‘ch’ is returned in the string.
    11. int lastIndexOf(int ch):
      This one returns the last occurrence ‘ch’ in the string.
    12. int indexOf(String str):
      The index of the first occurrence of the specified substring ‘str’ is returned through this method.
    13. int lastindexOf(String str):
      similarly, this method returns the last occurrence of the string ‘str’.
    14. String intern():
      This method first searches for the particular string and returns the reference of it otherwise it allocates the memory to the particular string and assigns the reference to it.
    15. int length():
      This method returns the length of the string.
    16. char[] toCharArray():
      It is used to convert the string to a new array.
    17. String toString():
      This already present string is itself return.
    18. String toLowerCase():
      It is used to all the characters of the string to lower case by the default locale.
    19. String toUpperCase():
      It is used to all the characters of the string to upper case by the default locale.
    20. String trim():
      It returns the substring of the original string after omitting leading and trailing white spaces.

    There are many more String methods such as, static String copyValueOf(char[] data),  static String valueOf(),  byte[] getBytes(),  boolean matches(String regex),  static String copyValueOf(char[] data, int offset, int count),  string[] split(String regex) and so on.


  • Java – Decision Making Statements

    The decision-making statement in Java is used when the user wants a specific block of code to be executed when the given condition is fulfilled.

    Java programming language provides the following types of decision-making statements:

    • if statement 
    • if-else statement 
    • nested-if statement 
    • if-else-if ladder statement 
    • switch-case statement 

    if statement

    Among the decision making statements, it is the most simple one. It checks if the condition is true or not, and if it is true then it executes the block of code inside if otherwise, it will not. The block that is executed is placed within the curly braces({}).

    Syntax of if statement in Java:

      if (boolean expression)
      {
       /* if expression is true */
       statements... ; /* Execute statements */
      }

    Example: Click here


    if-else statement

    There are two-part in this statement. First is the if statement, here it checks for the condition to be true and if it is true then it will execute the code present inside the if. But if the condition is false then the code inside the else statement will be executed.

    Syntax of if-else statement in Java:

     if (condition) 
     {
      //code executed if condition true 
     }
     else
     {
      //code executed if condition false 
     }

    Example: Click here


    nested-if statement

    This statement allows the user to use if block inside the other if block. And the inner if is executed only if the outer if is true in condition.

    Syntax of nested if statement in java:

     if(condition1)
     {    
        //block of code to be executed    
        if(condition2)
        {  
          //block of code to be executed    
        }    
     } 

    Example: Click here


    if-else-if ladder statement

    This statement allows the user to have multiple options to check for different conditions. Here, if one of the if or else-if condition is true then that part of the code will be executed and the rest will be bypassed. if none of the conditions is true then the final else statement at the end will be executed.

    Syntax of if-else-if ladder statement in java:

     if(condition1)
     {  
       //code to be executed if condition1 is true  
     }
      else if(condition2){  
       //code to be executed if condition2 is true  
       }  
      else if(condition3){  
       //code to be executed if condition3 is true  
       }  
        ...  
       else{  
        //final else if all the above condition are false 
        }

    Example: Click here


    switch-case statement

    A switch statement allows a variable to be tested for equality against multiple values. It provides the case for the different blocks of code to be executed. Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values.

    In the end, it can have a default case which is optional that is executed if no cases are matched. It can also have an optional break that is useful to exit the switch otherwise it continues to the next case.

    Syntax of switch statements in Java:

      {
        case value1:    
        //code to be executed;    
        break;  //optional  
        case value2:    
        //code to be executed;    
        break;  //optional  
        .
        .
        .
        .    
        case valueN:    
        //code to be executed;    
        break;  //optional 
            
        default:     
        code to be executed if all cases are not matched;    
      }

    Example: Click here


  • C – Operator and Expression

    Operators are the mathematical symbols that are used to perform a mathematical operation on operands. These symbols tell the compiler to perform respective operations. An expression is formed by joining constants and variables in C programming.

    Example:

    int a = 10 + 5;

    In the above example, 10 and 5 are operands and + is the operation performed between these two operands.

    Types of Operator in C:

    • Arithmetic Operator
    • Increment and Decrement Operators
    • Relational Operator
    • Assignment Operators
    • Logical Operators
    • Bitwise Operators
    • sizeof Operator
    • Other Operators

    C Arithmetic Operator

    These are used to perform mathematical operations on operands such as addition, subtraction, multiplication, division etc.

    • Addition: ‘+’ operator adds two operands. eg, a+b.
    • Subtraction: ‘-‘ operator subtracts two operands. eg, a-b.
    • Multiplication: ‘*’ operator multiplies two operands. eg, a*b.
    • Division: ’/’ operator divides the operand by the second. eg, a/b.
    • Modulus: ’%’ operator returns the remainder when the first operand is divided by the second. For example, a%b.

    Example of Arithmetic Operator

    #include <stdio.h>
    
    int main()
    {
      int a = 24;
      int b = 12;
      int result;
    
      result = a + b;
      printf("Addition:  %d\n", result);
    
      result = a - b;
      printf("Subtraction: %d\n", result);
    
      result = a * b;
      printf("Multiplication: %d\n", result);
    
      result = a / b;
      printf("Division: %d\n", result);
    
      result = a % b;
      printf("Modulus  %d\n", result);
    
      return 0;
    }

    The output of Arithmetic Operator:

    Addition:  36
    Subtraction: 12
    Multiplication: 288
    Division: 2
    Modulus  0

    C Increment and Decrement Operators

    These are the Unary Operators as they operate on a single operand. They are ++ and -- operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.

    • Post-Increment or Post-Decrement:
      First, the value is used for operation and then incremented or decremented. Represented as a++ or a–.
    • Pre-Increment Pre-Decrement:
      Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.

    Example of Increment and Decrement Operator

    #include <stdio.h>
    
    void main()
    {
      int x = 5;
    
      //post increment
      printf("%d\n", x++);
      printf("%d\n\n", x);
    
      x = 10;
    
      //preincrement
      printf("%d\n", ++x);
      printf("%d\n", x);
    }

    The output of Increment and Decrement Operator.

    5
    6
    
    11
    11

    C Relational Operator

    Relational operators are used to compare two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values. They are used with a loop statement and also with if-else statements.

    List of relational operators in C:

    OperatorNameExample
    ==Equal to.A == B
    !=Not equal.A != B
    >Greater than.A > B
    <Less than.A < B
    >=Greater than or equal to.A >= B
    <=Less than or equal to.A <= B

    Example of Relational Operator

    #include <stdio.h>
    
    int main()
    {
      int a = 10, b = 10, c = 20;
    
      printf("%d == %d is %d \n", a, b, a == b);
      printf("%d == %d is %d \n", a, c, a == c);
      printf("%d > %d is %d \n", a, b, a> b);
      printf("%d > %d is %d \n", a, c, a> c);
      printf("%d<%d is %d \n", a, b, a < b);
      printf("%d<%d is %d \n", a, c, a < c);
      printf("%d != %d is %d \n", a, b, a != b);
      printf("%d != %d is %d \n", a, c, a != c);
      printf("%d >= %d is %d \n", a, b, a>= b);
      printf("%d >= %d is %d \n", a, c, a>= c);
      printf("%d <= %d is %d \n", a, b, a <= b);
      printf("%d <= %d is %d \n", a, c, a <= c);
    
      return 0;
    }

    The output of Relational Operator.

    10 == 10 is 1 
    10 == 20 is 0 
    10 > 10 is 0 
    10 > 20 is 0 
    10 < 10 is 0 
    10 < 20 is 1 
    10 != 10 is 0 
    10 != 20 is 1 
    10 >= 10 is 1 
    10 >= 20 is 0 
    10 <= 10 is 1 
    10 <= 20 is 1 

    C Assignment Operators

    Assignment operator is used to assigning a value to any variable. This operator assigns the value of the right-hand side of an operator to the left-hand side.
    These are the short version formed by combining the two operators.

    For example Instead of writing, int a = a+3, we can write a += 3.

    • Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
      a = 20; B = 30; Ch = 'cha';
    • Add AND assignment operator: ‘+=’ This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
      c+=7; a+=b;
    • Subtract AND assignment operator: ‘-=’ This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.
      c-=7; a-=b;
    • Multiply AND assignment operator: ‘*=’ This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
      c*=7; a*=b;
    • Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
      c/=7; a/=b;
    • Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
      C %= A is equivalent to C = C % A ;

    Example of Assignment Operators

    #include <stdio.h>
    
    int main()
    {
      int x = 10, y;
    
      y = x;	// y is 10
      printf("Value of y: %d\n", y);
    
      y += x;	// y is 20
      printf("Value of y: %d\n", y);
    
      y -= x;	// y is 10
      printf("Value of y: %d\n", y);
    
      y *= x;	// y is 100
      printf("Value of y: %d\n", y);
    
      y /= x;	// y is 10
      printf("Value of y: %d\n", y);
    
      y %= x;	// y = 0
      printf("Value of y: %d\n", y);
    
      return 0;
    }

    The output of Assignment Operators:

    Value of y: 10
    Value of y: 20
    Value of y: 10
    Value of y: 100
    Value of y: 10
    Value of y: 0

    C Logical Operators

    Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. All of the binary logical operators return two boolean values that are true and false depending upon 0 and 1.

    OperatorDescriptionExample
    && (logical and)If both the operands are non-zero, then the condition becomes true.(X && Y) is false
    || (logical or)If any of the two operands are non-zero, then the condition becomes true.(X || Y) is true
    ! (logical not)Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(X && Y) is true

    Example of Logical Operators

    #include <stdio.h>
    
    int main()
    {
       int x = 5;
       int y = 20;
       int z;
    
       if (x && y)
       {
          printf("Line 1 - Condition is true\n");
       }
    
       if (x || y)
       {
          printf("Line 2 - Condition is true\n");
       }
    
       /*let's change the value of  a and b */
       x = 0;
       y = 10;
       if (x && y)
       {
          printf("Line 3 - Condition is true\n");
       }
       else
       {
          printf("Line 3 - Condition is not true\n");
       }
    
       if (!(x && y))
       {
          printf("Line 4 - Condition is true\n");
       }
    
       return 0;
    }

    The output of Logical Operator.

    1st Display: true
    2nd Display: true
    =====Values are changed======
    3rd Display: false
    4th Display: true

    C Bitwise Operators

    In C programming, Bitwise operators are used to perform a bit-level operation. All the mathematical operations are converted to bit-level which makes processing faster and also saves power.

    OperatorsName of operators
    &Bitwise AND
    |Bitwise OR
    ^Bitwise XOR
    ~Bitwise complement
    <<Shift left
    >>Shift right

    Example of Bitwise Operators.

    #include <stdio.h>
    
    int main()
    {
      int a = 15, b = 28;
    
      printf("Output = %d\n", a &b);
      printf("Output = %d\n", a | b);
      printf("Output = %d\n", a ^ b);
    
      printf("Output = %d\n", ~19);
      printf("Output = %d\n", ~-22);
    
      printf("Left shift by %d: %d\n", b>> 1);
      printf("Left shift by %d: %d\n", b << 1);
    
      return 0;
    }

    The output of Bitwise operator.

    Output = 12
    Output = 31
    Output = 19
    Output = -20
    Output = 21
    Left shift by 14: 0
    Left shift by 56: 0

    sizeof Operator

    sizeof operator is a unary operator that returns the size of constants, variables, array, etc.
    Example: sizeof(x), return size of the variable x.

    Example of sizeof Operator

    #include <stdio.h>
    
    int main()
    {
      int x;
      float y;
      double z;
      char ch;
    
      printf("Size of int = %lu bytes\n", sizeof(z));
      printf("Size of float = %lu bytes\n", sizeof(y));
      printf("Size of double = %lu bytes\n", sizeof(z));
      printf("Size of char = %lu byte\n", sizeof(ch));
    
      return 0;
    }

    The output of sizeof Operator.

    Size of int = 8 bytes
    Size of float = 4 bytes
    Size of double = 8 bytes
    Size of char = 1 byte

    Other Operators:

    Comma Operator (,)This operator is used to link related operators together. We use a comma operator when we declare many variables in a single line as shown below.
    Example: a, c, d = 0, z;
    & operatorThis is used to get the address of the variable.
    Example: pt = &x
    * operatorThis is used as a pointer to a variable.
    Example: int *pt;

    C Program to demonstrate & and * operator

    In this C program, & is used to get the address of a variable and * is used to get the value of variable.

    #include <stdio.h>
    
    int main()
    {
       int *ptr, x;
       x = 10;
    
       ptr = &x;
    
       printf("%d", *ptr);
    
       return 0;
    }

    Output: 10