Difficulty: Medium
Correct Answer: atoi() converts a string to int, itoa() converts an int to string and gcvt() converts a double or float to a string representation.
Explanation:
Introduction / Context:
This question focuses on common C style conversion functions that move data between string form and numeric form. Understanding what atoi(), itoa() and gcvt() do helps you parse numeric input and format numeric output without always relying on scanf and printf. Although not all of these functions are part of the ISO C standard (itoa() and gcvt() are often extensions), they are widely used in many environments and appear frequently in exam questions.
Given Data / Assumptions:
• atoi() takes a string (char *) argument.
• itoa() typically takes an integer, a buffer and a base.
• gcvt() typically takes a double value, a number of significant digits and a destination buffer.
• The question asks what conversion each function performs.
• We assume typical implementations such as those in standard libraries or common compilers.
Concept / Approach:
The name atoi stands for "ASCII to int" and converts a numeric string to an integer value. The name itoa stands for "int to ASCII" and converts an integer into its string representation in a specified base (such as base 10 or 16). The function gcvt is often used to convert a floating point number (double) to a string with a given number of significant digits. Correctly pairing each function with its direction of conversion is the key to answering this question.
Step-by-Step Solution:
Step 1: For atoi(), recall that it parses characters in a string, skipping whitespace and reading optional sign and digits, returning an int value. Example: atoi("123") yields 123.
Step 2: For itoa(), a typical prototype is char *itoa(int value, char *str, int base); it stores the textual representation of value in str using the given base, e.g., itoa(255, buf, 10) produces "255".
Step 3: For gcvt(), a typical prototype is char *gcvt(double value, int ndigit, char *buf); it converts value into a string with ndigit significant digits, stored in buf, such as gcvt(3.14159, 4, buf) yielding something like "3.142".
Step 4: Compare these behaviors with the options and select the one that accurately describes each function's role.
Verification / Alternative check:
To verify, you can write small examples: use atoi("42") and confirm that an int 42 is returned. Use itoa(42, buf, 10) and observe that buf contains "42". For gcvt(), pass a double such as 123.456 and request a few digits; the result in the buffer is a human readable decimal string. These quick experiments confirm the described directions of conversion, distinguishing them from any string case transformation or file I/O behavior.
Why Other Options Are Wrong:
Option B reverses the roles of atoi() and itoa(), claiming that atoi() converts int to string and itoa() does the opposite, which contradicts their names and behavior. It also incorrectly states that gcvt() converts a string to uppercase, which is unrelated to numeric conversion. Option C suggests all three perform generic file I/O, which is completely incorrect. Option D suggests that the functions are limited only to certain data structures (character arrays or structures), misrepresenting them; they are specifically about numeric to string and string to numeric conversions.
Common Pitfalls:
A common pitfall is to assume that all conversions should always use scanf/printf, ignoring these helper functions. Another mistake is mixing up the direction of conversion for functions whose names are easy to misremember. A helpful mnemonic is that the first letter indicates the source type: a in atoi means ASCII (string) to int, i in itoa means int to ASCII, and g in gcvt is often remembered in the context of "general" or "G format" style conversion for floating point numbers. Being clear on these directions avoids confusion in both exams and code.
Final Answer:
Correctly summarized, atoi() converts a string to int, itoa() converts an int to string and gcvt() converts a double or float to a string representation.
Discussion & Comments