Char Array and Integer

Posted on 2007-10-08
Last Modified: 2010-05-18
In following code I don't understand how can I assign integer to char ?

# include<stdio.h>

void main(void)
      char setOne[10];
      char setTwo[10];

      setOne[0] = 65;
      setTwo[0] = 66;

      setOne[1] = '\0';
      setTwo[1] = '\0';

      setOne[0] = (setOne[0] + setTwo[0]) % 128;      



Question by:s_more
    LVL 53

    Expert Comment

    A char is basically a signed integer value of size one byte (8 bits usually). It can contain all values between -128 and 127.

    An assignment like this :

            char c = 65;

    is thus entirely valid. But it would have been a lot clearer if this was used instead :

            char c = 'A';

    This does exactly the same thing since the ASCII value for the character 'A' is 65.
    LVL 84

    Expert Comment

    Char's are one byte integer values.
    If your character set is ASCII
    '\0'  == 0
    'A' == 65
    LVL 7

    Expert Comment

    char type is a numerical type, like int or long.
    of course it has smaller length and value range.

    signed char:
    len: 8b
    range: -128 -> 127

    len: 8b
    range: -128 -> 127 or 0 -> 255 (not defined by standard)

    unsigned char:
    len: 8b
    range 0 -> 255

    So you can write

    char c = 50;

    but If you write

    char c = 1000;

    you will get an warning from compiler.

    Author Comment


    What if I assign:

    char test  =  3;

    and output it to a text file ( I'll get a not readable character ), will this cause any issues
    with any other applications trying to read this text file. I've a application that complains
    "binary data encountered in text file". I'm not sure why is this considered as a binary data.

    LVL 53

    Accepted Solution

    Take a look at the ASCII table. You can find one here for example :


    You'll see that the character with ASCII code 3 is the ETX (end of text) character. This is a control character, rather than a printable character (just like the other characters with an ASCII code under 32).

    In principle, there is no problem to write that character to a file, however the program that will read the file will have to take that into account - it will not be able to print it.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now