#include <stdio.h>
#include <stdlib.h>
int main() {
	
	char* input = malloc(sizeof(char));
	if (input == NULL){
		perror("ERROR: input memory allocation failed.");
		exit(EXIT_FAILURE);
	}
	
	int ch = 0;
	
	int i = 0;
	
	printf("Please give an integer: ");
	
	while ((ch = getchar()) != EOF && ch != '\n'){
	    input = realloc(input, (size_t)(i+1) * sizeof(char));
		if (input == NULL){
	        perror("ERROR: input memory allocation failed.");
	        exit(EXIT_FAILURE);
	    }
		if (ch < 48 || ch > 57){
			free(input);
			fprintf(stderr, "ERROR: Please give a valid integer.\n");
			exit(EXIT_FAILURE);
		}
		input[i] = ch;
		i++;
	}
	
	input[i] = '\0';
	
	printf("Given integer is: %s\n", input);
	
	free(input);
	
	return 0;
}