473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to compare chars ASCII codes in C???

44
Hi everybody! The following comparison of two chars does not work in a C program:

if( 'a' < 'z' ){

//do something...
}

This comparison works, for example, in Java, and my question is: how to do the same or in a similar way in C?

Gratefully yours
Aug 3 '06 #1
2 28190
Banfa
9,065 Expert Mod 8TB
Yes it does, that precisely works in a C program. However was is true for C and I suspect Java is that the C standard does not specify the character set (e.g. ASCII) it specifically states that the characters '0', '1', '2' ... '9' should have consecutive values but does not specify the same for the letters.

What this means is that ('a' < 'z') is actually dependent on the character set you are using, it is TRUE in ASCII and to be quite honest many C/C++ programmers rely on this fact which makes there code slightly non-portable however many many platforms use ASCII so I think we can probably forgive them this.

Here is a working program

Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     if( 'a' < 'z' ){
  7.  
  8.         printf( "a is less than z\n" );
  9.     }
  10.  
  11.     return EXIT_SUCCESS;
  12. }
  13.  
Aug 4 '06 #2
zeny
44

Here is a working program

Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     if( 'a' < 'z' ){
  7.  
  8.         printf( "a is less than z\n" );
  9.     }
  10.  
  11.     return EXIT_SUCCESS;
  12. }
  13.  

Thanks a lot, indeed the above code works. I tried to do exactly the same but the compiler reported errors. I´m using Dev C++ 4 IDE and the compiler totally sucks, and most of the times i have to guess where are the errors because the errors reported by the compiler dont make any sense and are unspecific. So when the program gave errors i assumed that the comparison was invalid because i wasnt seeing any syntactic errors.
Aug 4 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Nicolas Bouillon | last post by:
Hi I would like to replace accentuel chars (like "é", "è" or "à") with non accetued ones ("é" -> "e", "è" -> "e", "à" -> "a"). I have tried string.replace method, but it seems dislike...
28
by: JKop | last post by:
Haven't been able to find such a thing. Can anyone please inform me of a Standard C++ function for comparing two strings without regard to case. Both for working with "char*", and with...
4
by: Siemel Naran | last post by:
How to compare if two files are identical? I wrote the following: bool comparefiles(const std::string& lhs, const std::string& rhs) { std::ifstream lhsfile(lhs.c_str()); std::ifstream...
43
by: Bill Cunningham | last post by:
I've been reading the C standard online and I'm puzzled as to what multibyte chars are. Wide chars I believe would be characters for languages such as cantonese or Japanese. I know the ASCII...
2
by: glean | last post by:
My console in WinXP is using Lucida Console for the font. My problem is, I can go to character map and select a string of special characters such as the following: ®¾Öþ¬«§¯ I can paste them into...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
9
by: simchajoy2000 | last post by:
Hi, I know what the ASCII Character Codes are for the 2nd and 3rd powers in VB.NET but I can't find the 6th power anywhere - does anyone know what it might be or if it even exists? Joy
6
by: bruce | last post by:
hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.