You said: certain amount of numbers; do you know that number upfront? If yes - you can allocate memory for that many values. If not - you'd need to implement some logic to allocate additional storage as needed. For example by doubling the already allocated space. You can increment the capacity by one to avoid wasted space, at the cost of repeated copying of your data.

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

int main()
{
  char ch = 0;
  int size = 0, capacity = 1;
  int* c = malloc(sizeof(int) * capacity);
  while (1) {
    scanf("%d%c", &c[size], &ch);
    if (ch == '\n')
      break;
    size++;
    if (size == capacity)
    {
      capacity *= 2; // use whatever policy to increase the capacity
      c = realloc(c, sizeof(int) * capacity);
    }
  }
  return 0;
}
Answer from Vlad Feinstein on Stack Overflow
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ dynamic-memory-allocation-examples.aspx
C Dynamically Memory Allocation: Functions and Examples
Enter limit of the text: 100 Enter text: I am mike from California, I am computer geek. Inputted text is: I am mike from California, I am computer geek. In this program we will allocate memory for one dimensional array and print the array elements along with sum of all elements.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 173340-dynamic-memory-allocation-very-simple-question.html
Dynamic memory allocation. Very simple question.
June 9, 2017 - Devoted my life to programming... ... Hi guys, After messing up the code, according to your recommendations, I changed the loop "for" for "while". This is the code right now: ... #include <stdio.h> #include <stdlib.h> int main() { int num, i=1; char c, *ptr; printf("Insert the number of characters to input: "); scanf("%d", &num); ptr=(char*) malloc(num*sizeof(char)); if (ptr==NULL) { printf("There is no enough memory to allocate"); } else { printf("Insert the text: "); scanf(" %c", ptr); while (c!='\n') { c=getc(stdin); ptr=(char*) realloc(ptr, num*sizeof(char)); ptr[i]=c; i++; } } printf("\nThe characters inserted are: %s", ptr); free(ptr); return 0; } Just before you yell at me, printf("\nThe characters inserted are: %s", ptr); I know is %s, you told me to change into %c but doesn't work with %c.
Discussions

User input and dynamic memory allocation in C - Stack Overflow
I'm still fairly new to C programming and I've been trying to create a program which needs user input first. In the beginning, I need the user to input certain amount of numbers such as: 4 12 8 6 5... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to read input of a string with an indefinite size using Dynamic Memory Allocation?
read chars one at a time and realloc when needed More on reddit.com
๐ŸŒ r/C_Programming
9
7
June 21, 2022
c - How to dynamically allocate memory space for a string and get that string from user? - Stack Overflow
I want to read input from user using C program. I don't want to use array like, char names[50]; because if the user gives string of length 10, then the remaining spaces are wasted. If I use chara... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Dynamically Allocated Array of strings?
The basic idea is to malloc a small amount of memory, and then call realloc whenever you reach full capacity to resize the array. More on reddit.com
๐ŸŒ r/C_Programming
7
2
April 9, 2018
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-dynamic-memory-allocation
C Dynamic Memory Allocation Using malloc(), calloc(), free() & realloc()
memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); // deallocating the memory free(ptr); return 0; } ... Here, we have dynamically allocated the memory for n number of int. // Program to calculate the sum of n numbers entered by the user #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); if(ptr == NULL) { printf("Error!
๐ŸŒ
Quora
quora.com โ€บ How-do-you-write-a-C-program-using-dynamic-allocating-memory-to-allow-the-user-entering-two-characters-then-the-program-will-print-out-characters-between-these-in-ascending-order
How to write a C program using dynamic allocating memory to allow the user entering two characters then the program will print out characters between these in ascending order - Quora
Answer (1 of 3): Why use C to force the programmer to handle memory issues manually when they should and can be handled by language, compiler and runtime. Do you want to develop a program to do what you want in the second part of the question, or prematurely specify how you think it should be do...
๐ŸŒ
w3resource
w3resource.com โ€บ cpp-exercises โ€บ dynamic-memory-allocation โ€บ cpp-dynamic-memory-allocation-exercise-4.php
C++ Dynamic Memory Allocation: Input character and string with memory allocation
November 28, 2025 - #include <iostream> // Including ... to store a single character // Input a character from the user std::cout << "Input a character: "; std::cin >> * dynamicChar; // Taking a single character input and storing it in the dynamically ...
๐ŸŒ
Jyotiprakash's Blog
blog.jyotiprakash.org โ€บ basic-dynamic-memory-allocation-programming-questions
Basic Dynamic Memory Allocation Programming Questions
December 27, 2023 - Dynamic Array: Create a program that dynamically allocates memory for an array of integers. Allow the user to input the size of the array and elements, and then print the array.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_memory_allocate.php
C Allocate Memory
Dynamic memory is memory that is allocated after the program starts running. Allocation of dynamic memory can also be referred to as runtime memory allocation. Unlike with static memory, you have full control over how much memory is being used at any time. You can write code to determine how much memory you need and allocate it. Dynamic memory does not belong to a variable, it can only be accessed with pointers.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to read input of a string with an indefinite size using dynamic memory allocation?
r/C_Programming on Reddit: How to read input of a string with an indefinite size using Dynamic Memory Allocation?
June 21, 2022 -

Im trying to create a program that takes a users commands and with them creates an HTML file.So for example the input could be newPar [text].And the command will be saved in a string called cmd that has a max of 10 characters.But how can I save the input that is the text of the paragraph in a string that I dont know the length of until the user has typed it?

Edit:Thank you all for the help!!Every suggestion was extremely helpful!Sorry for not answering to all of the comments and taking a while to answer or upvote the comments Unfortunately I didnt manage to finish the program before the deadline I needed to due to other problems I had with making it and not having enough time.But again thank you for helping me overcome the problem I very much appreciate it! :)

๐ŸŒ
Electro4u
electro4u.net โ€บ blog โ€บ write-a-c-program-to-allocate-dynamic-memory-for-n-integers-scan-it-and-print-n-value-takes-it-run-times-625
C Program: Dynamic Memory Allocation, Scanning, and Printing n Integers
ptr = (int *)malloc(n * sizeof(int)); // Check the return value of malloc() to make sure that the memory was allocated successfully. if (ptr == NULL) { printf("Error: Could not allocate memory.\n"); return 1; } // Prompt the user to enter the values of the integers. printf("Enter the values ...
๐ŸŒ
Medium
medium.com โ€บ @indradeephalder โ€บ day-9-dynamic-horizons-exploring-dynamic-memory-allocation-in-c-a8aa9f43dbe6
Day 9: Dynamic Horizons: Exploring Dynamic Memory Allocation in C | by Indradeep Halder | Medium
October 23, 2023 - #include <stdio.h> #include <stdlib.h> int main() { int size; printf("Enter the initial size of the array: "); scanf("%d", &size); int* dynamicArray = allocateMemory(size); // Access and manipulate the dynamic array int newSize; printf("Enter ...
๐ŸŒ
w3resource
w3resource.com โ€บ cpp-exercises โ€บ dynamic-memory-allocation โ€บ cpp-dynamic-memory-allocation-exercise-2.php
C++ Dynamic Memory Allocation: Array of integers and strings Initialization
Write a C++ program to dynamically allocate an array of integers and strings and initialize its elements. ... #include <iostream> // Including the Input/Output Stream Library #include <string> // Including the String Library int main() { // ...
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ structure-dynamic-memory-allocation
C Program to Store Data in Structures Dynamically
#include <stdio.h> #include <stdlib.h> struct course { int marks; char subject[30]; }; int main() { struct course *ptr; int noOfRecords; printf("Enter the number of records: "); scanf("%d", &noOfRecords); // Memory allocation for noOfRecords structures ptr = (struct course *)malloc(noOfRecords * sizeof(struct course)); for (int i = 0; i < noOfRecords; ++i) { printf("Enter subject and marks:\n"); scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks); } printf("Displaying Information:\n"); for (int i = 0; i < noOfRecords; ++i) { printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks); } free(ptr); return 0; }
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ C_dynamic_memory_allocation
C dynamic memory allocation - Wikipedia
February 15, 2026 - These limitations are avoided by using dynamic memory allocation, in which memory is more explicitly (but more flexibly) managed, typically by allocating it from the heap (free storage), an area of memory structured for this purpose. In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns.
๐ŸŒ
W3Resource
w3resource.com โ€บ c-programming โ€บ c-dynamic-memory-allocation.php
Dynamic Memory Allocation in C: malloc(), calloc(), realloc(), free()
2 weeks ago - Prevents memory leaks by deallocating dynamically allocated memory. PREV : C Command-Line Arguments NEXT : C printf() ๏ปฟ
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ dynamic memory allocation in c | all 4 functions (+examples)
Dynamic Memory Allocation In C | All 4 Functions (+Examples)
May 1, 2024 - Next, we use printf() statement to print the value stored in the dynamically allocated memory space, accessing the value through the pointer ptr.
๐ŸŒ
Binaryupdates
binaryupdates.com โ€บ home โ€บ dynamic memory allocation in c programming
Dynamic Memory Allocation in C Programming
August 4, 2021 - If in case there isnโ€™t enough free space in memory of current block to extend, new block is allocated for the full size of reallocation. In this case we copy existing data to new memory block and then free the old memory block. Example: Extend the size of memory block A with size double as B. #include<stdio.h> #include<stdlib.h> int main() { int n; printf("Enter the size of array: "); scanf("%d", &n); int *A = (int*) malloc(n*sizeof(int)); for(int i=0; i<n; i++) { A[i]=i+1; } int *B = (int*) realloc(A, 2*n*sizeof(int)); printf("Prev.