w3resource

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. 

Flowchart:

C Exercises Flowchart: Convert a string to an integer

C Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C program to perform a binary search in an array.
Next: Write a C program to convert a string to a double.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Static function in C

Making a function static hides it from other translation units, which helps provide encapsulation.

helper_file.c

intf1(int);        /* prototype */
staticintf2(int); /* prototype */

intf1(int foo){
return f2(foo); /* ok, f2 is in the same translation unit */
/* (basically same .c file) as f1         */
}

intf2(int foo){
return42 + foo;
}

main.c:

intf1(int); /* prototype */
intf2(int); /* prototype */

intmain(void){
    f1(10); /* ok, f1 is visible to the linker */
    f2(12); /* nope, f2 is not visible to the linker */
return0;
}

Ref : https://bit.ly/3slAgzt

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook