What does atof stand for? - c

In C atof=a-to-f(loat) converts a string into a double precision float. I am wondering what the a part of atof stand for.

atof is a function in the C programming language that converts a string into a floating point numerical representation. atof stands for ASCII to float. It is included in the C standard library header file stdlib.h. Its prototype is as follows
double atof (const char *str);
The str argument points to a string, represented by an array of characters, containing the character representation of a floating point value. If the string is not a valid textual representation of a double, atof will silently fail, returning zero (0.0) in that case. [1]
Note that while atoi and atol return variable types corresponding with their name ("atoi" returns an integer and "atol" returns a long integer), atof however, does not return a float, it returns a double.
A related function is sscanf. This function extracts values from strings and its return argument is the number of valid values it managed to extract (so, unlike atof, sscanf can be used to test if a string starts with a valid number).

To best answer what the a stands for, go back to early 1970s when bytes cost approached dollars each.
Even if a originally stood for ASCII, atof() did not and still does not mean to convert ASCII into double as the implementation may have used an alternate character encoding. With EBCDIC or PETSCII, one could think of a as alpha and write code for atof() per that non-ASCII encoding.

Related

How do I compare single multibyte character constants cross-platform in C?

In my previous post I found a solution to do this using C++ strings, but I wonder if there would be a solution using char's in C as well.
My current solution uses str.compare() and size() of a character string as seen in my previous post.
Now, since I only use one (multibyte) character in the std::string, would it be possible to achieve the same using a char?
For example, if( str[i] == '¶' )? How do I achieve that using char's?
(edit: made a type on SO for comparison operator as pointed out in the comments)
How do I compare single multibyte character constants cross-platform in C?
You seem to mean an integer character constant expressed using a single multibyte character. The first thing to recognize, then, is that in C, integer character constants (examples: 'c', '¶') have type int, not char. The primary relevant section of C17 is paragraph 6.4.4.4/10:
An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g.,’ab’ ), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.
(Emphasis added.)
Note well that "implementation defined" implies limited portability from the get-go. Even if we rule out implementations defining perverse behavior, we still have alternatives such as
the implementation rejects integer character constants containing multibyte source characters; or
the implementation rejects integer character constants that do not map to a single-byte execution character; or
the implementation maps source multibyte characters via a bytewise identity mapping, regardless of the byte sequence's significance in the execution character set.
That is not an exhaustive list.
You can certainly compare integer character constants with each other, but if they map to multibyte execution characters then you cannot usefully compare them to individual chars.
Inasmuch as your intended application appears to be to locate individual mutlibyte characters in a C string, the most natural thing to do appears to be to implement a C analog of your C++ approach, using the standard strstr() function. Example:
char str[] = "Some string ¶ some text ¶ to see";
char char_to_compare[] = "¶";
int char_size = sizeof(char_to_compare) - 1; // don't count the string terminator
for (char *location = strstr(str, char_to_compare);
location;
location = strstr(location + char_size, char_to_compare)) {
puts("Found!");
}
That will do the right thing in many cases, but it still might be wrong for some characters in some execution character encodings, such as those encodings featuring multiple shift states.
If you want robust handling for characters outside the basic execution character set, then you would be well advised to take control of the in-memory encoding, and to perform appropriate convertions to, operations on, and conversions from that encoding. This is largely what ICU does, for example.
I believe you meant something like this:
char a = '¶';
char b = '¶';
if (a == b) /*do something*/;
The above may or may not work, if the value of '¶' is bigger than the char range, then it will overflow, causing a and b to store a different value than that of '¶'. Regardless of which value they hold, they may actually both have the same value.
Remember, the char type is simply a single-byte wide (8-bits) integer, so in order to work with multibyte characters and avoid overflow you just have to use a wider integer type (short, int, long...).
short a = '¶';
short b = '¶';
if (a == b) /*do something*/;
From personal experience, I've also noticed, that sometimes your environment may try to use a different character encoding than what you need. For example, trying to print the 'á' character will actually produce something else.
unsigned char x = 'á';
putchar(x); //actually prints character 'ß' in console.
putchar(160); //will print 'á'.
This happens because the console uses an Extended ASCII encoding, while my coding environment actually uses Unicode, parsing a value of 225 for 'á' instead of the value of 160 that I want.

Why does `"%c"` exist in `printf` if `char` is converted to `int`?

In C you have the "%c" and "%f" formats flags for printf- and scanf-like functions. Both of these function use variable length arguments ..., which always convert floats to doubles and chars to ints.
My question is, if this conversion occurs, why do separate flags for char and float exist? Why not just use the same flags as for int and double?
Related question:
Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
Because the way it gets printed out is different.
printf("%d \n",100); //prints 100
printf("%c \n",100); //prints d - the ascii character represented by 100
Because float and double have different machine representations or sizes, and calling conventions: many processors have registers dedicated to floating point which might be used for argument passing.
And the C standard requires that short arguments are converted to int and float arguments are converted to double.

Is %d a cast in C?

int a;
printf("%d\n", a);
I wonder if %d is a cast?
In any case it won't be a cast but a reinterpretation (like getting the address, casting to a pointer of a different type and then getting the contents as a new type).
Example:
printf("%d\n", 1.5);
won't print integer 1, but the integer value of the representation of 1.5 in IEEE 754. If you want to cast, you must explicitly put (int) in front of the value.
No, it is part of the format specifier string for printf() function's first argument; the format string. It will print out a decimal representation of that int you passed as the second argument.
It is not. It is just a "hint" for printf() function to treat the 'a' argument as an 'int'
No, it's not a cast, but I suggest you take a look at the source for printf() to understand this. There's nothing special about printf() -- it's just a varargs function like any other. It's one of the first functions you learn in C, usually well before you learn varargs, so it often sticks out in people's minds as special when it's really not. A quick study of the source will probably be enlightening.
When you pass a format string to printf(), you're telling the function what to expect in its argument list (generally on the stack), but that might not agree with what you actually put there. With %d, you're telling printf() to take the next integer-sized chunk of bytes off the argument list and format those bytes as if they represent a signed decimal number. So when printf() parses the format string and encounters a %d, it will probably do something like:
int num = va_arg(args, int);
And then format and output the bytes in "num" as if they were an integer, regardless of what kind of argument you actually passed. If you put a float in the arguments where printf() is told to expect an integer, the output will be a decimal representation of the IEEE floating point bytes -- probably not what you intended, and not what a true cast would have done.
No, it is a format specifier. It has semantic meaning only to the formatted I/O functions and is not part of the C language itself. You could equally write yopur own
All it does is specify the 'human readable' representation in which to present an int value; there is no type conversion or translation.

Is there an equivalent to Perls' split() function in C?

I'm trying to split real numbers in a C program using the decimal point as the delimter such that such that say, 1234.56 yields
(int) whole_num = 1234
(int) fraction = 56
Any ideas how I can go about doing this? Its been a loooong while since I mucked around with C, see? :)
void split( double num, int& whole_number, double& fraction) {
fraction = modf(num, &whole_number);
}
This works since modf takes the integer part of the double and returns the fractional part.
Assuming you want to split a string.
strtok_r and your favorite string-to-num function like strtol
If you're dealing with an actual floating-point number, as opposed to a string representation of such, you should use modf for splitting out the integral and fractional parts.
Perl's split splits by regex, so to replicate full functionality you'd need a regex library. For general string-splitting, you may be able to use strtok, but because it changes the string in-place, strtok_r (described on the same page) is recommended instead.

C Compatibility Between Integers and Characters

How does C handle converting between integers and characters? Say you've declared an integer variable and ask the user for a number but they input a string instead. What would happen?
The user input is treated as a string that needs to be converted to an int using atoi or another conversion function. Atoi will return 0 if the string cannot be interptreted as a number because it contains letters or other non-numeric characters.
You can read a bit more at the atoi documentation on MSDN - http://msdn.microsoft.com/en-us/library/yd5xkb5c(VS.80).aspx
Uh?
You always input a string. Then you parse convert this string to number, with various ways (asking again, taking a default value, etc.) of handling various errors (overflow, incorrect chars, etc.).
Another thing to note is that in C, characters and integers are "compatible" to some degree. Any character can be assigned to an int. The reverse also works, but you'll lose information if the integer value doesn't fit into a char.
char foo = 'a'; // The ascii value representation for lower-case 'a' is 97
int bar = foo; // bar now contains the value 97
bar = 255; // 255 is 0x000000ff in hexadecimal
foo = bar; // foo now contains -1 (0xff)
unsigned char foo2 = foo; // foo now contains 255 (0xff)
As other people have noted, the data is normally entered as a string -- the only question is which function is used for doing the reading. If you're using a GUI, the function may already deal with conversion to integer and reporting errors and so in an appropriate manner. If you're working with Standard C, it is generally easier to read the value into a string (perhaps with fgets() and then convert. Although atoi() can be used, it is seldom the best choice; the trouble is determining whether the conversion succeeded (and produced zero because the user entered a legitimate representation of zero) or not.
Generally, use strtol() or one of its relatives (strtoul(), strtoll(), strtoull()); for converting floating point numbers, use strtod() or a similar function. The advantage of the integer conversion routines include:
optional base selection (for example, base 10, or base 10 - hex, or base 8 - octal, or any of the above using standard C conventions (007 for octal, 0x07 for hex, 7 for decimal).
optional error detection (by knowing where the conversion stopped).
The place I go for many of these function specifications (when I don't look at my copy of the actual C standard) is the POSIX web site (which includes C99 functions). It is Unix-centric rather than Windows-centric.
The program would crash, you need to call atoi function.

Resources