3.conversions
There are a few functions that exist to convert strings to integer, long integer and float values. They are:
double atof(char *string) -- Convert string to floating point value.
int atoi(char *string) -- Convert string to an integer valueint
atol(char *string) -- Convert string to a long integer value.
double strtod(char *string, char *endptr) -- Convert string to a floating point value.
long strtol(char *string, char *endptr, int radix) -- Convert string to a long using a given radix.
unsigned long strtoul(char *string, char *endptr, int radix) -- Convert string to unsigned long.
Most of these are fairly straightforward to use.
For example:char *str1 = "100";
char *str2 = "55.444";char *str3 = " 1234";
char *str4 = "123four";char *str5 = "invalid123";
int i;
float f;i = atoi(str1); /* i = 100 */f = atof(str2); /* f = 55.44 */i = atoi(str3); /* i = 1234 */i = atoi(str4); /* i = 123 */i = atoi(str5); /* i = 0 */
Note:Leading blank characters are skipped.Trailing illegal characters are ignored.If conversion cannot be made zero is returned and errno (See Chapter 17) is set with the value ERANGE.
4.Searching and Sorting
The stdlib.h provides 2 useful functions to perform general searching and sorting of data on any type. In fact we have already introduced the qsort() function in Chapter 11.3. For completeness we list the prototype again here but refer the reader to the previous Chapter for an example.The qsort standard library function is very useful function that is designed to sort an array by a key value of any type into ascending order, as long as the elements of the array are of fixed type.
qsort is prototyped (in stdlib.h):
void qsort(void *base, size_t num_elements, size_t element_size,
int (*compare)(void const *, void const *));
9.Streams
Streams are a portable way of reading and writing data. They provide a flexible and efficient means of I/O.A Stream is a file or a physical device (e.g. printer or monitor) which is manipulated with a pointer to the stream.There exists an internal C data structure, FILE, which represents all streams and is defined in stdio.h. We simply need to refer to the FILE structure in C programs when performing I/O with streams.We just need to declare a variable or pointer of this type in our programs.We do not need to know any more specifics about this definition.We must open a stream before doing any I/O,then access itand then close it.Stream I/O is BUFFERED: That is to say a fixed ``chunk'' is read from or written to a file via some temporary storage area (the buffer).
5.string manupulation
char *stpcpy (const char *dest,const char *src) -- Copy one string into another.
int strcmp(const char *string1,const char *string2) - Compare string1 and string2 to determine alphabetic order.
char *strcpy(const char *string1,const char *string2) -- Copy string2 to stringl.
char *strerror(int errnum) -- Get error message corresponding to specified error number.int strlen(const char *string) -- Determine the length of a string.
char *strncat(const char *string1, char *string2, size_t n) -- Append n characters from string2 to stringl.
int strncmp(const char *string1, char *string2, size_t n) -- Compare first n characters of two strings.
char *strncpy(const char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to stringl .
int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp().
int strncasecmp(const char *s1, const char *s2, int n) -- case insensitive version of strncmp().
6.comparison
The strcmp() function lexically compares the two input strings and returns:
Less than zero-- if string1 is lexically less than string2
Zero-- if string1 and string2 are lexically equal
Greater than zero-- if string1 is lexically greater than string2
This can also confuse beginners and experience programmers forget this too.
rest will be published later.......
There are a few functions that exist to convert strings to integer, long integer and float values. They are:
double atof(char *string) -- Convert string to floating point value.
int atoi(char *string) -- Convert string to an integer valueint
atol(char *string) -- Convert string to a long integer value.
double strtod(char *string, char *endptr) -- Convert string to a floating point value.
long strtol(char *string, char *endptr, int radix) -- Convert string to a long using a given radix.
unsigned long strtoul(char *string, char *endptr, int radix) -- Convert string to unsigned long.
Most of these are fairly straightforward to use.
For example:char *str1 = "100";
char *str2 = "55.444";char *str3 = " 1234";
char *str4 = "123four";char *str5 = "invalid123";
int i;
float f;i = atoi(str1); /* i = 100 */f = atof(str2); /* f = 55.44 */i = atoi(str3); /* i = 1234 */i = atoi(str4); /* i = 123 */i = atoi(str5); /* i = 0 */
Note:Leading blank characters are skipped.Trailing illegal characters are ignored.If conversion cannot be made zero is returned and errno (See Chapter 17) is set with the value ERANGE.
4.Searching and Sorting
The stdlib.h provides 2 useful functions to perform general searching and sorting of data on any type. In fact we have already introduced the qsort() function in Chapter 11.3. For completeness we list the prototype again here but refer the reader to the previous Chapter for an example.The qsort standard library function is very useful function that is designed to sort an array by a key value of any type into ascending order, as long as the elements of the array are of fixed type.
qsort is prototyped (in stdlib.h):
void qsort(void *base, size_t num_elements, size_t element_size,
int (*compare)(void const *, void const *));
9.Streams
Streams are a portable way of reading and writing data. They provide a flexible and efficient means of I/O.A Stream is a file or a physical device (e.g. printer or monitor) which is manipulated with a pointer to the stream.There exists an internal C data structure, FILE, which represents all streams and is defined in stdio.h. We simply need to refer to the FILE structure in C programs when performing I/O with streams.We just need to declare a variable or pointer of this type in our programs.We do not need to know any more specifics about this definition.We must open a stream before doing any I/O,then access itand then close it.Stream I/O is BUFFERED: That is to say a fixed ``chunk'' is read from or written to a file via some temporary storage area (the buffer).
5.string manupulation
char *stpcpy (const char *dest,const char *src) -- Copy one string into another.
int strcmp(const char *string1,const char *string2) - Compare string1 and string2 to determine alphabetic order.
char *strcpy(const char *string1,const char *string2) -- Copy string2 to stringl.
char *strerror(int errnum) -- Get error message corresponding to specified error number.int strlen(const char *string) -- Determine the length of a string.
char *strncat(const char *string1, char *string2, size_t n) -- Append n characters from string2 to stringl.
int strncmp(const char *string1, char *string2, size_t n) -- Compare first n characters of two strings.
char *strncpy(const char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to stringl .
int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp().
int strncasecmp(const char *s1, const char *s2, int n) -- case insensitive version of strncmp().
6.comparison
The strcmp() function lexically compares the two input strings and returns:
Less than zero-- if string1 is lexically less than string2
Zero-- if string1 and string2 are lexically equal
Greater than zero-- if string1 is lexically greater than string2
This can also confuse beginners and experience programmers forget this too.
rest will be published later.......