📖 Scenario: Imagine you are building a simple calculator app that takes user input as text. You need to convert the typed number strings into actual numbers to perform calculations.
🎯 Goal: Build a small program that converts a string representing a number into an integer using the atoi logic.
📋 What You'll Learn
Create a string variable with a number as text
Create a variable to hold the converted integer
Write code to convert the string to an integer manually (without using library atoi)
Print the converted integer
💡 Why This Matters
🌍 Real World
Converting user input strings into numbers is common in calculators, form inputs, and data parsing.
💼 Career
Understanding how to manually convert strings to integers helps in debugging, writing parsers, and working with low-level data processing.
Progress0 / 4 steps
1
Create the input string
Create a character array called str and initialize it with the string "1234".
DSA C
Hint
Use double quotes to create a string in C.
2
Create the integer variable
Create an integer variable called result and initialize it to 0.
DSA C
Hint
Initialize result to zero before conversion.
3
Convert string to integer
Write a for loop using variable i to iterate over str until the null character '\0'. Inside the loop, update result by multiplying it by 10 and adding the integer value of str[i] minus '0'.
DSA C
Hint
Each character digit can be converted to an integer by subtracting '0'.
4
Print the converted integer
Write a printf statement to print the value of result.
DSA C
Hint
Use printf("%d\n", result); to print the integer.
Practice
(1/5)
1. What does the atoi function do in C programming?
easy
A. Checks if a string contains only digits
B. Converts a string representing a number into an integer
C. Calculates the length of a string
D. Converts an integer into a string
Solution
Step 1: Understand the purpose of atoi
The atoi function stands for ASCII to integer, which means it converts a string of digits into an integer value.
Step 2: Compare options with definition
Only Converts a string representing a number into an integer correctly describes this conversion from string to integer.
Final Answer:
Converts a string representing a number into an integer -> Option B
Quick Check:
atoi = string to integer conversion [OK]
Hint: Remember atoi means ASCII to integer conversion [OK]
Common Mistakes:
Confusing atoi with itoa (integer to ASCII)
Thinking atoi calculates string length
Assuming atoi validates string digits
2. Which of the following is the correct syntax to use atoi in C?
easy
A. int num = atoi(char *str);
B. int num = atoi();
C. int num = atoi(int str);
D. int num = atoi(str);
Solution
Step 1: Check function parameter type
The atoi function takes a single argument of type const char *, which is a string pointer.
Step 2: Validate correct syntax
int num = atoi(str); correctly shows calling atoi with a string variable str without specifying type in call.
Final Answer:
int num = atoi(str); -> Option D
Quick Check:
atoi takes string variable as argument [OK]
Hint: Call atoi with just the string variable name [OK]
Common Mistakes:
Including type in function call parentheses
Calling atoi without arguments
Passing integer instead of string
3. What will be the output of the following C code?
char str[] = "-123abc";
int val = atoi(str);
printf("%d", val);
medium
A. -123
B. 0
C. 123
D. Error
Solution
Step 1: Analyze atoi behavior with mixed string
atoi converts initial numeric part including sign until non-digit character. Here, "-123" is converted, stops at 'a'.
Step 2: Determine output value
So, val becomes -123 and printf prints -123.
Final Answer:
-123 -> Option A
Quick Check:
atoi stops at first non-digit, returns -123 [OK]
Hint: atoi reads number until first non-digit character [OK]
Common Mistakes:
Expecting atoi to ignore leading sign
Thinking atoi returns 0 on trailing letters
Assuming runtime error due to letters
4. Identify the error in this code snippet using atoi:
char *str = NULL;
int num = atoi(str);
printf("%d", num);
medium
A. Passing NULL pointer to atoi causes undefined behavior
B. Missing header file
C. atoi cannot convert strings with digits
D. No error, code runs fine
Solution
Step 1: Check argument passed to atoi
The code passes a NULL pointer to atoi, which expects a valid string pointer.
Step 2: Understand consequences of NULL argument
Passing NULL leads to undefined behavior, often causing a crash or runtime error.
Final Answer:
Passing NULL pointer to atoi causes undefined behavior -> Option A