[Dev-C++] SAME MESSAGE BUT 4k IN FILE SIZE NOT 14k Fw: Char datatype
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Eric <eri...@wo...> - 2008-03-30 18:18:03
|
deeps6ix
Well, even though other estimeed members have already answered
this, let me explain why char is case-sensitive; I kinda get the feeling, by
your question, you do not fully understand what a char is.
The datatype 'char' is a "virtual conception", in that a 'char' = 1
byte, and represents an entry of ASCII table.
The 'char' type does not exist in terms that it is really a
integer.
Every entry of the ASCII table features a value and a charater. the
first 31 entries consist of teletype control codes, like carriage return
legacy typewriter thing, that you had to do when you wanted to change
lines. )
The following 96 entries features common letters, numbers and
pontuaction stuff.
This, obviously represents the first 127 entries, that are, also
the maximum value a byte ( signed ) can achive...
but looking into ASCII table reveals Value('M') = 77 and Value('m')
= 109, therefore 'm' != 'M'
Check out this code, execution gives more info :
<CODE LANG="C">
#include <stdio.h>
int ReturnBytesInChar( unsigned char ch ) {
/* Right shhifts bits out, to AND with 0000 0001*/
int i;
for ( i=7*sizeof( char ) ; i >=0 ; i-- )
printf("%d", (ch >> i) & 1 );
}
int ReturnBytesInInt( unsigned int ch ) {
int i;
for ( i=7*sizeof(int) ; i >=0 ; i-- )
printf("%d", ( ch >> i ) & 1);
}
int main () {
char a = 'a';
unsigned int aIntegerValue = 97;
unsigned int Draw;
printf("Is a char = \'a\' equal a int = 97?\n\n");
if ( a == aIntegerValue )
printf("Wow, they do ? WTF!");
else
printf("Nah! this guys is full of bollogny"); // This will never
happen!!! heheh humor, hehe
printf ("\n\nBut.. numbers of bytes allocatted for a char variable are %d
and for a integer one %d\n...How can this be?", sizeof(char),sizeof(int));
printf("\n\nSimple dude :\n Check this table :\n");
for ( Draw^=Draw ; Draw != 9+sizeof(int)*7; Draw ++ ) putchar('-');
printf("\n\nChar bytes\t:\t%d\nVarDump:", sizeof( char ) );
ReturnBytesInChar( a );
printf("\n\nInt var bytes\t:\t%d\nVarDump:", sizeof( int ) );
ReturnBytesInInt( aIntegerValue );
putchar(10);
for ( Draw^=Draw ; Draw != 9+sizeof(int)*7; Draw ++ ) putchar('-');
printf("\nNotice the similarities ? from the %do byte to the %do of
integer the value equals \rchar's %d byte%c\n", sizeof(unsigned int)-1,
sizeof(unsigned int), sizeof(char), (sizeof(char)>1) ? 's':' ');
printf("What about diferent capitalizations? \'m\' and \'M\'?\nComputer
see them as their values, and not letters...\n");
printf("See it as :\n\n\tAs value(\'m\') != value(\'M\')
=>(implies)\n\t\t 'M' != 'm' \nAnd we can see it by the same functions
above...");
printf("mess with them, and learn... and \rsee functions like fgetc() and
try to understand why they are \'buggy\'");
scanf("%c",&a);
}
</CODE>
Date: Sat, 29 Mar 2008 11:20:28 +1200
From: dee...@gm...
To: dev...@li...
Subject: [Dev-C++] Char datatype
hello all,
i would like to know if the CHAR datatype is case-sensitive??
i.e. should i write code like this to recognise the character or not
e.g.
char = category;
if (category == 'M') or (category == 'm')
cheers..
--
---Deepesh Kapadia---
Veja mapas e encontre as melhores rotas para fugir do trânsito com o Live
Search Maps! Experimente já!
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
No virus found in this incoming message.
Checked by AVG.
Version: 7.5.519 / Virus Database: 269.22.1/1349 - Release Date: 29-Mar-08
5:02 PM
|