Connect with us

Can integer variable store char value?

Discussion in 'Microcontrollers, Programming and IoT' started by champ1, Dec 25, 2019.

Scroll to continue with content
  1. champ1


    Jul 17, 2018
    integer variable store integer value like 10, 100, -6

    Can integer variable store char value? my program shows int type variable can be store char value.
    void main ()
        int Code = 'A';
        printf("Code %d \n", Code);
        printf("Code %c \n", Code);
    if I write int Code = 'A'; in embedded program. Is it valid statement in embedded program.
  2. Nanren888


    Nov 8, 2015
    What result did you get when you ran it?
    Seems as if you are doing the right experiment to test the idea.
    Most languages use ASCII which means that a character is 8 bits long. It's a convention of representing characters as numbers, since in computers we really only have bits and numbers and have to decide how to interpret them as other things..
    ASCII is not the only way, but the most common, along with extentions for such as non-English characters. e.g. abcdef. персонажи, 人物
    (Your browser needs to also know how to draw each number as a character for it to come out right)

    C is not a "strongly-typed" language, which basically means that if the data fits in a variable you can store it there.

    Googling (c language character representation) I got this as my first hit

    C Character Type

    Summary: in this tutorial, you will learn what C character type is and how to declare, use and print character variables in C.

    C uses char type to store characters and letters. However, the char type is integer type because underneath C stores integer numbers instead of characters.
    There's more if you'd like to read further.
    Just next on that page is a table of ASCII. It's the convention about which integer value means which character. If everyone uses the same standard, or a compatible standard, everything comes out right.
    When I used to programme in C, I kept a copy or K&R handy. You might want to look for one, if for some reason google doesn't work for you. :)
  3. Wollowstone


    Mar 26, 2018
    Since, in theory, a character code can be ANSI 7 bits, EBDIC, HTML, ... for PORTABILITY, it is strongly suggested to use a char when a char is supposed to be used, and an int for an int. But sure, a sequence of 64 bits ... is readable in many possible ways, among other, when using an Assembler like language. Some micro controllers even give you the possibility to read and write individual bits (and not just bytes). Adding even more possible meanings.
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    Nov 17, 2011
    Valid but not good practice. The side effects can be disastrous. Compare this to storing gasoline in a bottle for drinking water (something you should never do!): It can be done and it does no harm as long as it is treated as gasoline. But consider the fatality if someone takes the content of the bottle for water :(:eek:.
    Always use the correct data types. This improves legibility of the program and allows static checking tools like e.g. lint (and similar, more modern tools) to better find errors in your code.
    If you need to change the data type within your code, use explicit type casting.

    Note that the char data type is often of 1 byte size, whereas an integer even on 8 bit embedded systems often is of 2 byte size. Using integers to store characters is then a waste of memory -- not something you want to do in an embedded system with usually very limited resources (which is not to imply that it would be good practice on a system with lots of available memory).
    If you need to use characters outside the classic ASCII character set, consider using unicode. Look up the documentation and/or the respective header files of your development system (C compiler and libraries) to find out which options are supported.
    hevans1944 likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day