473,700 Members | 2,371 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 28268
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
16078
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
2269
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
6144
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
4798
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 function parameter main (char argc, char **argv) if that's correct? Bill
2
2115
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 using Console.WriteLine("®¾Öþ¬«§¯"); , the console actually displays it as r_Ö_¬«§_
11
17411
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
8256
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
8650
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
6406
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
9081
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7816
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4408
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2031
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.