w3resource logo


C Programming Exercises

C Exercises: Convert a string to an unsigned long integer

Secondary Nav

C Variable Type : Exercise-2 with Solution

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

Expected Output :

Input an unsigned number: 25                                            
Output: 25

C Code:

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

	int main ()
	{
		char buffer[123];
		unsigned long ul;

		printf ("\nInput an unsigned number: ");
		fgets (buffer,123,stdin);

		ul = strtoul (buffer,NULL,0);
		printf ("Output: %lu\n\n",ul);
		return 0;
	}

Solution

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

	int main ()
	{
		char buffer[123];
		unsigned long ul;

		printf ("\nInput an unsigned number: ");
		fgets (buffer,123,stdin);

		ul = strtoul (buffer,NULL,0);
		printf ("Output: %lu\n\n",ul);
		return 0;
	}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program