LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2005, 05:14 PM   #16
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32

Unsigned char is useful if you are working with binary data. For instance, take the following C program:

Code:
#include <stdio.h>

int main()
{
#include <stdio.h>

int main()
{
    char ch = 130;
    unsigned char uch = 130;

    printf("char = %d; unsigned char = %d\n", ch, uch);

    if (ch > 128)
    {
        printf("ch > 128\n");
    }
    else
    {
        printf("ch <= 128\n");
    }

    if (uch > 128)
    {
        printf("uch > 128\n");
    }
    else
    {
        printf("uch <= 128\n");
    }

    return 0;
}

Compilation:
gcc  blah.c -o blah
blah.c: In function `main':
blah.c:10: warning: comparison is always false due to limited range of data type

Output:
./blah
char = -126; unsigned char = 130
ch <= 128
uch > 128
Note that if you are working with binary data and you need to compare the char value to a specific value >= 128, it is always going to be false, because as already stated, a signed char only stores the range -128 to 127.

Note also that when I tried to set it specifically to 130, the actual value printed when cast to an int is -126, this is due to it being stored as a two's complement signed value.

Last edited by deiussum; 03-20-2005 at 05:15 PM.
 
Old 03-21-2005, 10:24 AM   #17
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
just for curiosity,
is it possible to exploit all these problems for malicious uses?

C evolve from another typless language,rewrote unix over assembly,
triumph as a non-typesafe language.Is it hard to predict problems with types back in those days.Are there any other languages back then other than Algol68 and Pascal(does these two already exists?)
C++ evolve(complements?) from C with OOP and others,is it more tedious(bloat and still unsafe)?
if a safer language had been used back then,will linux be in a better position to "promote" itself?
why in the present days,many coders still insist on non-typesafe languages




:: needs to clarify all these though
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
unsigned char to wchar_t JurajPsycho Programming 6 03-19-2005 04:00 PM
Hex to unsigned char? george_mercury Programming 2 11-27-2004 01:27 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration