|
From: hhh h. <the...@ho...> - 2008-03-30 16:47:00
|
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 +1200From: dee...@gm...: dev...@li...: [Dev-C++] Char datatypehello 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 note.g. char = category;if (category == 'M') or (category == 'm')cheers..-- ---Deepesh Kapadia---
_________________________________________________________________
Instale a Barra de Ferramentas com Desktop Search e ganhe EMOTICONS para o Messenger! É GRÁTIS!
http://www.msn.com.br/emoticonpack |