C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • 1. String to Integer Conversion
  • 2. Integer to String Conversion
  • 3. String to Double Conversion
  • 4. Double to String Conversion

Was this helpful?

  1. Strings

Converting Strings

In C programming, converting strings often involves changing the representation of data, such as converting a string to an integer or vice versa. Let's explore common string conversion operations in C.

1. String to Integer Conversion

Converting a string to an integer can be achieved using the atoi (ASCII to Integer) function from the <stdlib.h> library.

#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *numberString = "123";
    
    // Convert string to integer
    int convertedNumber = atoi(numberString);
    
    printf("Converted Number: %d\n", convertedNumber);

    return 0;
}

This program converts the string "123" to an integer using atoi and prints the result.

2. Integer to String Conversion

Converting an integer to a string can be done using the sprintf (String Print Formatted) function.

#include <stdio.h>

int main() {
    int number = 456;
    char resultString[10]; // Sufficient size to hold the string representation
    
    // Convert integer to string
    sprintf(resultString, "%d", number);
    
    printf("Converted String: %s\n", resultString);

    return 0;
}

This program converts the integer 456 to a string using sprintf and prints the result.

3. String to Double Conversion

Converting a string to a double can be done using the atof (ASCII to Float) function from the <stdlib.h> library.

#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *doubleString = "123.45";
    
    // Convert string to double
    double convertedDouble = atof(doubleString);
    
    printf("Converted Double: %lf\n", convertedDouble);

    return 0;
}

This program converts the string "123.45" to a double using atof and prints the result.

4. Double to String Conversion

Converting a double to a string can be done using the sprintf function as well.

#include <stdio.h>

int main() {
    double pi = 3.14159;
    char resultString[20]; // Sufficient size to hold the string representation
    
    // Convert double to string
    sprintf(resultString, "%.2lf", pi);
    
    printf("Converted String: %s\n", resultString);

    return 0;
}

This program converts the double 3.14159 to a string with two decimal places using sprintf and prints the result.

Understanding these conversion techniques is crucial for handling various types of data in C programming.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousSearching, Tokenizing, and Analyzing StringsNextDebugging

Was this helpful?