Thread: Type casting in C

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    Type casting in C

    If I understand, converting one type to another type is type casting in c laguage

    Code:
     #include<stdio.h>
    
    int main()
    {
        char a = 'A';
    
    
    
    
        void *ptr = &a;
    
    
    
    
        printf("%c \n", *(char *)ptr);
    	
        *((char *)ptr) = 'B';
    	 
    	printf("%c", *(char *)ptr);
    
    
    
    
        return 0;
    }
    Is the type casting happeing in the code ? in which line type casting is happning ?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Dadu@ View Post
    If I understand, converting one type to another type is type casting in c laguage

    Code:
     #include<stdio.h>
    
    int main()
    {
        char a = 'A';
    
        void *ptr = &a;
    
        printf("%c \n", *(char *)ptr);
    	
        *((char *)ptr) = 'B';
    	 
    	printf("%c", *(char *)ptr);
    
        return 0;
    }
    Is the type casting happeing in the code ? in which line type casting is happning ?
    Anytime you see "(Type)" or "(Type *)" these are a cast to the type in the parentheses. In lines 15, 17, and 19 you are converting a "void *" to a "char *" pointer.

    Also, please remove most of the blank lines. One is sufficient.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Is this a homework assignment? If not, where is this code from? If it came from a book or web site, you'd think it would explain type casting and show code like that as an example.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Dadu@ View Post
    If I understand, converting one type to another type is type casting in c laguage.
    If you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type casting
    By chucky in forum C Programming
    Replies: 2
    Last Post: 06-21-2018, 05:52 PM
  2. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  3. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  4. type casting or not?
    By siavoshkc in forum C++ Programming
    Replies: 8
    Last Post: 03-19-2006, 08:20 PM
  5. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM

Tags for this Thread