w3resource logo
C Programming Exercises

C Exercises: Convert a string to an integer

C Variable Type : Exercise-13 with Solution

Write a C program to convert a string to an integer.

Sample Solution:

C Code:

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

int main ()
	{
		int num1;
		char my_array [256];

		printf ("\nInput a number : ");
		fgets (my_array, 256, stdin);

		num1 = atoi (my_array);
		printf ("The value Input is %d.\n\n", num1);

		return 0;
	}

Sample Output:

Input a number : 100                                                                                          
The value Input is 100. 

C Programming Code Editor:

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

int main ()
	{
		int num1;
		char my_array [256];

		printf ("\nInput a number : ");
		fgets (my_array, 256, stdin);

		num1 = atoi (my_array);
		printf ("The value Input is %d.\n\n", num1);

		return 0;
	}

Improve this sample solution and post your code through Disqus