Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Char Array and Integer

Posted on 2007-10-08
5
Medium Priority
?
548 Views
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;      

      printf("%s\n",setOne);

      getchar();

}
0
Comment
Question by:s_more
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20036919
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.
0
 
LVL 85

Expert Comment

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

Expert Comment

by:UrosVidojevic
ID: 20036931
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

char:
=====
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.
0
 

Author Comment

by:s_more
ID: 20037241
Infinity08,

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.

slm
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 20037292
Take a look at the ASCII table. You can find one here for example :

        http://www.asciitable.com/

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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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