473,567 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare chars ASCII codes in C???

44 New Member
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 28233
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member

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
16023
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 non ascii chars... Can you help me please ? Thanks.
28
2240
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 "std::string". Thanks, -JKop
4
6136
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 rhsfile(rhs.c_str()); typedef std::istreambuf_iterator<char> istreambuf_iterator; return std::equal(
43
4756
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 character set specifies that each character such as 'b' or 'B' is an 8 bit character. So what's a multibyte character? Also how would you use the...
2
2111
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 my console by right-clicking the title, then edit, paste, and everything is fine. When I try to display them on the console programmatically by...
11
17393
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 internal string StringToAscii(string S) { byte strArray = Encoding.UTF7.GetBytes(S); string NewString = Encoding.UTF7.GetString(strArray);
9
8254
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
8628
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 chars. i'm assuming python has something like valid_str = strip(invalid_str)
14
6391
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 I go about doing it ? Thanks
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6272
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.