Lets say I want to write a very simple C program wich a is string passed with a pipe at my program and translate every letter to ASCII and store it in an array. I want to use less library as possible and not use malloc

For example in a Linux terminal : echo Translate this!| ./myprogram

So I tough that I should count the letters and initialise an array the size of the string.

#include <stdio.h>
#include <ctype.h>

int main() 
{   int c;
    int count = 0;

    while ((c = getchar()) != EOF)
        count = count + 1;

    int array[count];

    return 0;

It seems to me that getchar "clears" my input and my string is not reusable. The problem is that I cannot manage to reuse getchar() to go through my string again. Lets say the input will be always variable. Sometimes it could be 10 letters, sometimes 1 million letters. How can I manage to store my input in an array without malloc ?

I tough doing somekind of temporary array and translate n letters at a time ? Is it a more suitable option ?

Thanks !

sometimes 1 million letters

Are you using windows? As I recall, the windows stack size default is 1MB, so a million letters will be too much to store on the stack and you'll have no choice but to use the heap (and I see you're using int values in your code, each of which is a lot larger than a char). If you're copying hundreds of thousands of elements around from one array to another, you'll run out of memory even faster.

You can change the stack size from the default if you have to.

If you are dealing with a variable size input you have a few options:

  1. If you know the maximum size of the input you can create a static array that is on the stack (heed Moschops' warning about stack size). Then you fill in the elements as you read them.
  2. If the input maximum size is not known, you can do the same as #1, but produce a warning/error when the input is too large for your static array.
  3. If you dont need to store the input just read each character, apply your function, and then output. No storage necessary.

These all depend on your needs and ultimate goals.

I managed to do it without storing the values, I just applied some functions recursivly. Thanks !

#include <stdio.h>
#include <string.h>

    char str[50];
    int i = 0;
    int res[50];
    while (str[i] != '\0')
        res[i] = str[i];
        printf("%d ", str[i]);
    return 0;