Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program For Int to Char Conversion
Next article icon

C Program For Char to Int Conversion

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to convert the given numeric character to integer.

Example:

Input: '3'
Output: 3
Explanation: The character '3' is converted to the integer 3.

Input: '9'
Output: 9
Explanation: The character '9' is converted to the integer 9.

Different Methods to Convert the char to int in C

There are 3 main methods to convert the char to int in C language as follows:

Table of Content

  • Using ASCII Values
  • Using sscanf() Function
  • Using atoi() Function

Let's discuss each of these methods in detail.

1. Using ASCII Values

Each character in C has an ASCII value, a unique numerical representation. For numeric characters like '0', '1', etc., the ASCII value starts from 48 for '0' and increases sequentially. To convert a char to its integer equivalent, subtract the ASCII value of '0' from the character.

C Program to Convert char to int Using ASCII Values

C
// C program to convert the given char to int using // ASCII values #include <stdio.h>  int main() {        // Define a numeric character variable     char ch = '7';      // Convert the character to integer by subtracting   	// ASCII value of '0'     int N = ch - '0';      // Print the result     printf("The integer value of character '%c' is %d\n", ch, N);      return 0; } 

Output
The integer value of character '7' is 7 

Time Complexity: O(1)
Space Complexity: O(1)

2. Using sscanf() Function

The sscanf() function can be used to read formatted input from a string. It can convert a character to an integer by treating the character as a string and reading it using the format specifier for an integer (%d).

C Program to Convert char to int Using sscanf() Function

C
// C program to demonstrate conversion of // char to int using sscanf() #include <stdio.h>  int main() {        // Define a character variable     char ch = '8';      // Declare an integer variable to store the converted value     int N;      // Use sscanf to convert the character to an integer     sscanf(&ch, "%d", &N);      printf("The integer value of character '%c' is %d\n", ch, N);      return 0; } 

Output
103

Time Complexity: O(1)
Space Complexity: O(1)

3. Using atoi() Function

The atoi() function from the C Standard Library <stdlib.h> can convert a string of digits into an integer. Although atoi() is designed for strings, it can be used with single-character strings.

C Program to Convert char to int Using atoi()

C
// C program to demonstrate conversion of char to int // using atoi() #include <stdio.h> #include <stdlib.h>  int main() {        // Define a character variable     char ch = '5';      // Convert the character to a string and then use     // atoi() to convert it to an integer     int N = atoi(&ch);      // Print the result     printf("The integer value of character '%c' is %d\n", ch, N);      return 0; } 

Output
The integer value of character '5' is 5 

Time Complexity: O(1)
Space Complexity: O(1)


Next Article
C Program For Int to Char Conversion

K

ksrikanth0498
Improve
Article Tags :
  • C Programs
  • C Language
  • C Conversion Programs
  • C Conver

Similar Reads

  • C Program For Int to Char Conversion
    To convert the int to char in C language, we will use the following 2 approaches: Using typecastingUsing sprintf() Example: Input: N = 65 Output: A1. Using TypecastingMethod 1:Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.Typecast
    2 min read
  • C Program For Long to String Conversion
    To convert the long to string in C language we will use the following 2 approaches: Using Macros with sprintfUsing sprintf Input: long = 1234 Output: string 12341. Using Macros and sprintf C/C++ Code // C Program for Long to // String Conversion #include <stdio.h> #include <string.h> #de
    1 min read
  • C Program For Octal to Decimal Conversion
    The number system is one of the ways to represent numbers. Every number system has its own base or radix. For example, Binary, Octal, Decimal, and Hexadecimal Number systems are some of the number systems and are also used in microprocessor programming. These numbers are easy to convert from one sys
    3 min read
  • C Program to Print Continuous Character Pattern
    Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss
    5 min read
  • C Program For Hexadecimal to Decimal Conversion
    Here we will build a C program for hexadecimal to decimal conversion using 5 different approaches i.e. Using format SpecifierUsing Switch caseUsing array Using while loopUsing for loop We will keep the same input in all the mentioned approaches and get an output accordingly. Input:  hexanumber = "2D
    4 min read
  • Convert String to int in C
    In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language. Example: Input: "1234"Output: 1234Explanation
    6 min read
  • C Program to Find the Size of int, float, double and char
    Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen. Examples Input: charOutput: Size of char: 1 byte Input: intOutput:Size of int: 4 bytes Different Methods to Find the Size of int, float, double and char in CWe can find the
    4 min read
  • C Program To Print Continuous Character Pyramid Pattern
    There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the n
    5 min read
  • Lex program for Decimal to Hexadecimal Conversion
    Problem: Write a Lex program for Decimal to Hexadecimal conversion. Explanation: Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The function yylex() is the main flex function which runs the Rule Section. Prerequ
    1 min read
  • C Program to Convert Decimal to Octal
    Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences