Friday, 3 April 2020

Typedef in C with examples


Consider following example:
#include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };

int main(){
   struct student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   struct student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

As you can see in above example, every time, a structure variable is being declared, we have to write struct student varName.
To avoid writing struct all over the place, we can use typedef with structure declaration. Using typedef avoids having to write struct every time you declare a variable of that type.
 Example:
#include<stdio.h>
   typedef struct student{
       int rollno;
       float marks;
   };
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   typedef struct {
       int rollno;
       float marks;
   }student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of same program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student s;
int main(){
   s s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   s s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Summary:
(i). Typedef keyword is used to give a new symbolic name(i.e alias) to another existing type.
(ii). It saves some typing.
(iii). It makes code cleaner.




Please comment and share if you find it useful.

Thursday, 26 March 2020

How to read deleted WhatsApp messages

WhatsApp allows users to delete a sent message within a stipulated time. However, there is a way to read deleted messages. But one must remember that there is no such official feature which let you read deleted messages.
To read deleted WhatsApp messages, one will need to download third-party applications. Kindly note that these application might collect personal data stored in the phone. So download such applications at your own risk. One such application is WhatsRemoved+.  You can download this app from Google Play store. It is a free app.

Steps
Connect your phone to a WiFi network.
Download WhatsRemoved+ from Google Play store.
       Install it and accept the terms and conditions.
      
        Provide access to phone’s notification
  

App will ask to choose the applications you want it to save all messages from. Select WhatsApp option and then continue.


WhatsRemoved+ will then ask whether it should save files or not. Choose the option that you want.
Then you will be taken to a page that will show all deleted WhatsApp messages. Just click on WhatsApp option on top of the screen.

Hope it helps. Kindly like and share this article.

Friday, 20 March 2020

How to block /unblock USB Ports in Windows


USB ports are ports are used for different purposes like data transfer between your computer and external hard disk or pen drive.
Disabling USB is one of the best way to stop unauthorized access to your data. Also it stops your computer from viruses which may come from infected flash drives.
In this article, you will learn different ways to block/unblock USB ports in Windows:

Method 1: Using Registry Editor

Step 1: Go to Registry Editor- Windows key + R and type regedit and hit Enter.

Step 2: Go to HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> services -> USBSTOR

Step 3: In the USBSTOR, find the Start DWORD.

Step 4:Double click Start to edit the value.

Change the value data to 4 and the click OK button.

Close Registry Edit.

You are done.

Sunday, 25 August 2019

How to reverse a string in Java

Java is one of the most widely used programming languages in the world and hence a lot of questions are asked in interview from Java.

In this article, you will learn simple but most commonly asked Java Interview Question- How to reverse a string.
You will learn three method to reverse a string.
/*
  There is an API for reversing a string in Java. StringBuffer and StringBuilder provides reverse()
  method to reverse a string.
*/
package programs;

public class ReverseString {

    public static void main(String[] args) {
        /*************************method 1- Manual Method********************/
        String str = "India";
        String revStr = "";

        //Convert String to char array
        char[] strArray = str.toCharArray();

        //find length of char array
        int len = strArray.length;

        //iterate over char array from last index to first index
        for (int i = len - 1; i >= 0; i--) {
            revStr = revStr + str.charAt(i);
        }
        System.out.println("Method 1 : Manually reversing a string");
         System.out.println(revStr);
       
         /*************************method 2 - Using StringBuffer********************/
        StringBuffer sbf=new StringBuffer(str);
        System.out.println("Method 2 : Reverse string using StringBuffer");
        System.out.println(sbf.reverse());
       
        /*************************method 3 - Using StringBuilder********************/
        StringBuilder sbl=new StringBuilder(str);
        System.out.println("Method 3 : Reverse string using StringBuilder");
        System.out.println(sbl.reverse());
    }

}

Output:
Method 1 : Manually reversing a string
aidnI
Method 2 : Reverse string using StringBuffer
aidnI
Method 3 : Reverse string using StringBuilder
aidnI

Wednesday, 22 May 2019

How to setup auto login in Windows


There may be several reasons for you to setup auto login in your computer. Like when you are away from your computer and for some reason you computer restarts and you want to start a program without login into it. But before setting up auto login into your computer, you must that somebody might get physical access to your important data.

If you are sure that nobody has physical access to your computer or data in your computer is not sensitive, then following the procedure below to configure automatic log on to windows.

Step 1: Type netplwiz in Run program.
It will open Advanced User Accounts program.

Step 2: In User Accounts popup, go to Users tab and Uncheck the box next to “Users must enter a username and password to use this computer”.

Step 3 : Click Apply button. Another popup will come up. Enter username and password of user account which you want to setup for automatic log in.

Step 4 : Click Ok button.

You are done. Restart your computer and see whether windows logs in automatically.


Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 7 May 2019

Type Conversion and Typecasting In C


Introduction
In our C programs, an expression may contain data values of different data types. When the expression contains values of similar type values then no conversion is required. But if the expression contains values of different types then they must be converted to desired type. In a c programming language, the data conversion is performed in two different methods as follows:
    (i).Type Conversion
    (ii).Type Casting

 Type Conversion
  • In this procedure, a data value from one data type to another data type is converted automatically by the compiler.
  • Also known as implicit type conversion.
  • The implicit type conversion is automatically performed by the compiler.
  • To evaluate the expression, the data type is promoted from lower to higher level where the hierarchy of data types can be given as: double, float, long, int, short, and char.
Example:
#include<stdio.h>
  int main(){
     int x=3,u;
     float y,v=4.59;
     y= x;
     u=v;
     printf("y=%f\n",y);
     printf("u=%d\n",u);
 return 0;
}
Output:
 y=3.000000
 u=4  //data loss


Typecasting
  • also called as explicit type conversion or forced conversion.
  • Implicit type conversion may lead to data loss as in above example. That’s why we need type casting.
  • To convert data from one type to another type we specify the target data type in paranthesis as a prefix to the data value that has to be converted.
  • Type casting can be done by placing the destination data type in parentheses followed by the variable name that has to be converted.
The general syntax of type casting is as follows:
   (data/Type) variableName

Example:
#include<stdio.h>
   int main(){
     int x=3;
     float y;
     y= (float)x;
     printf("y=%f\n",y);
 return 0;
}
Output:
  Y=3.000000


Kindly like and share this post.

Sunday, 5 May 2019

Relational Operators in C


Relational operators are used to compare two values. There are six relational operators in C. These are called relational operator because they establish relation between its operands. These operators are used in decision-making. These are binary operators because they need two operands.
Following table lists the six relational operators and their meaning-
Sl No
Operator
Syntax
Meaning
1
< 
Op1<Op2
Is Op1 less than Op2
2
<=
Op1<=Op2
Is Op1 less than or equal to Op2
3
> 
Op1>Op2
Is Op1 greater than Op2
4
>=
Op1>=Op2
Is Op1 greater than or equal to  Op2
5
==
Op1==Op2
Is Op1 equal to Op2
6
!=
Op1!=Op2
Is Op1 not equal to than Op2

Each of these operations results in a value of type int. The result of each operation is 1 if the comparison is true and 0 if the comparison is false.

Following C program demonstrates the working of above relational operators-
#include<stdio.h>
int main(){
   int a=1,b=2;
   if(a<b){
      printf(“a is less than b.\n”);
    }

   a=3,b=2;
   if(a<=b){
      printf(“a is less than or equal to b.\n”);
    }
  a=2,b=1;
   if(a>b){
      printf(“a is grater than b.\n”);
    }

  a=2,b=3;
   if(a>=b){
     printf(“a is greater than or equal to b.\n”);
    }

a=4,b=4;
if(a==b){
     printf(“a is equal to b.\n”);
    }
if(a!=b){
     printf(“a is not equal to b.\n”);
    }

  return 0;
}
Output:
a is less than b.
a is grater than b.
a is equal to b.

Please comment and share if you find it useful.

Thursday, 2 May 2019

The Conditional Operator in C


Introduction
  • It involves three operands- the logical expression plus two other expressions—this operator is also referred to as the ternary operator.
  • The conditional operator evaluates to one of two expressions, depending on whether a logical expression evaluates true or false.

Syntax
Condition ? expression1 : expression2

The ? character follows the logical expression, condition. On the right of ? are two operands, expression1 and expression2, that represent choices. The value that results from the operation will be the value of expression1 if condition evaluates to true, or the value of expression2 if condition evaluates to false. Remember that only one, either expression1 or expression2, will be evaluated.

Example
The conditional operator can be used in place of if-else statements.

Following program find the greater number between two numbers using if-else statements.
#include<stdio.h>
int main(){
  int num1=20,num2=30;
  if(num1>num2)
     printf(“%d is greater than %d.”,num1,num2);
 else
       printf(“%d is greater than %d.”,num2,num1);

  return 0;
}
Output:
  30 is greater than 20.

Now we will find out the greater number using conditional operator.
#include<stdio.h>
int main(){
  int num1=20,num2=30;
     num1>num2?printf(“%d is greater than %d.”,num1,num2):printf(“%d is greater than %d.”,num2,num1);

  return 0;
}
Output:
  30 is greater than 20.


Please comment and share if you find it useful.

Logical Operators in C


Introduction
Sometimes you want to perform more than one test for a decision. You may want to combine two or more checks on values and perform a certain action only when they’re all true. For example, you may only want to go to watch a movie if you’re feeling well and it’s a weekend. These sorts of situations demand use of logical operators.
There are three logical operators available in C - &&,|| and !.

1. Logical AND && Operator
  • binary operator that combines two logical expressions—that is, two expressions that evaluate to true or false.
  • Returns true when both the conditions are true; return false when any of the conditions or both are false.

Syntax :
    Expression1 && Expression2

This expression evaluates to true if both expressions Expression1 and Expression2 evaluate to true. If either or both of the expressions are false, the result of the operation is false.

Example:
      if(marks >=60 && age < =80)
           printf("Grade : B\n");

2. Logical OR || Operator
  • binary operator that combines two logical expressions—that is, two expressions that evaluate to true or false.
  • If either or both expressions of the || operator are true, the result is true. The result is false only when both operands are false.


Syntax :
    Expression1 || Expression2

This expression evaluates to true if either or both expressions Expression1 and Expression2 evaluate to true. If both expressions are false, the result of the operation is false.

Example:
      if(marks >=60 || age < =80)
           printf("Grade : B\n");

3. Logical NOT ! Operator
  • The ! operator is a unary operator, because it applies to just one operand.
  • The logical NOT operator reverses the value of a logical expression: true becomes false, and false becomes true

Syntax :
!(expression)
   If expression is true, it becomes false; if it is false, it becomes true.

Example:
  int marks=100;
      if(!marks)
           printf("You didn’t received 100% marks.\n");


Please comment and share if you find it useful.