Type Casting in C | W4 learn in hindi

Type Casting क्या होती है?

Type Casting in C 


Constant in C:

C language में type casting एक ऐसी process होती है जिसमें एक data type के variable की value का दूसरे data type में conversion होता है। कई बार ये variables automatically convert हो जाते है और कई बार आप इन्हें manually भी convert करते है।

1) C language में एक data type जब दूसरे data type में automatically बदलता है तो उसे type conversion कहा जाता है।

2) जब आप explicitly या manually type cast operator का use करते हुए किसी data type के variable को दूसरे data type में बदलते है तो उसे type casting कहा जाता है।

1) Type Conversion (Implicit/Automatic):

type conversion automatically compiler द्वारा perform किया जाता है। इसमें आपको कुछ भी करने की आवश्यकता नहीं होती है और ना ही इसमें किसी तरह के operator की आवश्यकता होती है। ये compatible type के बीच में ही perform होता है। Compatible types ऐसे data types होते है जिनमें type conversion possible है।

1) Example:



#include <stdio.h>
int main()
{
   int num = 2;
   float float_num;

/* Converting automatically from integer to float */

   float_num = num; 
   printf(“Number is :%f”,float_num);

   return 0;
}



Output:

Number is : 2.000000 



2) Type Casting (Explicit/Manually):

Type casting आप खुद perform करते है। इसके लिए आप type cast operator यूज़ करते है। C language में () को type cast operator कहा जाता है। इस operator को आप किसी expression से पहले use करते है। इन brackets में आप उस data type का नाम लिखते है जिसमें आप expression के result को convert करना चाहते है।

1) Example:



#include <stdio.h>
int main()
{
    int num = 2;
    char char_num;

    /* Manually casting integer type to char */

  char_num= (char) num;

    printf(“Number is : %d”,(int)char_num); /* Type Casting Again */
    return 0;
}



Output:

Number is : 2


Comments