|
Mike,
char mychar; /* this defines a single character, not a string */
char mystring[6]; /* this defines a string of 6 bytes. it contains
garbage */
/* until you initialize it */
How to compare a character (note the use of single quote):
if (mychar == '1')
foo();
How to compare a string (note the use of double quote):
if (!strcmp(mystring, "hello"))
foo();
There are other string comparisons. strncmp() compares strings for a given
number of bytes. stricmp() ignores case on the compare, etc.
If you want to check the value of a single character in a string you can do:
if (mystring[4] == '1')
foo();
This checks the fifth position in the string for the character value of '1'.
Strings are not null terminated until you make them so. Some C functions
will automatically terminate a string with a null. Some examples:
memset(mystring, 0, sizeof(mystring)); /* sets the entire string to
nulls */
strcpy(mystring, "hello"); /* creates a null terminated
string */
Patrick
At 09:03 PM 6/20/2000 -0400, Pantzopoulos, Mike wrote:
>I'm a bit confused about how to deal with a single character. Is a char
>x[1]; or char x; statement defining a string or a character?
>
>Ishould it be compared via a ............if(x== '1') statement or should
>a strcmp function be invoked? I presume that any definition such as char
>x[6] defines a string (as null terminated). So is char x[1] defining a
>null terminated string of length 2 (inclusive of the null character)?
>
---
IBM AS/400 communications, FTP automation, and network security
software and consulting services.
http://www.patownsend.com
+---
| This is the C/400 Mailing List!
| To submit a new message, send your mail to C400-L@midrange.com.
| To subscribe to this list send email to C400-L-SUB@midrange.com.
| To unsubscribe from this list send email to C400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: bob@cstoneindy.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.