Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
Is there a limit on how many local variables we can declare in C?

Given the fact that local variables inside a function in C get pushed to the stack once the function is called (After pushing the variables being passed to the function), is there any limit to the quantity of said variables before the stack buffer overflows? Or that limit is just given by the amount of RAM a determined host has?

I've tried to test this by creating a 4,6gb .C file with a single function having 25000*13 variables declared and initialized to 0. Said function is called inside main() but it compiled just fine (With -O0) and it didn't crash.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

For example according to the C Standard (5.2.4.1 Translation limits)

1 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

— 4095 external identifiers in one translation unit

— 511 identifiers with block scope declared in one block

over 4 years ago · Santiago Trujillo Report

0

The default stack size for the main thread is 8 MiB on macOS (from the ld man page), 2 MiB on Linux (per the --stack switch here), and 1 MiB on Windows (per this page). Additional threads created within the program may have smaller stacks. (Note: The various sources cited use “MB” or “Mb”; I have interpreted these in context to mean 1,048,576 bytes = 1 MiB.)

A different stack size can be requested when linking the program into an executable file.

Functions use stack space for various things, including passing arguments, establishing stack frames for throwing exceptions, sometimes debugging information, and temporary work space, so there is not a direct correlation between the amount of memory used for the stack and the number of variables that can be defined.

over 4 years ago · Santiago Trujillo Report

0

As you do not ask about the minimum requirements I will write about the C standard

Modern conforming compilers nowadays do not set any limits (Microsoft does not count as it is not conforming). You are limited by the available computer resources.

Another part of the toolchain (linker, object and executable files limitations etc) might put some additional limits, but modern mainline compilers usually do not have any.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!