Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:06:16+00:00 2026-05-28T18:06:16+00:00

In a local scope (like a function), given these 4 examples: (1) int x;

  • 0

In a local scope (like a function), given these 4 examples:

(1)

int x;
int y;
// code...
x = 4;
y = 5;

(2)

int x = 4;
int y = 5;
// code...

(3)

// code...
int x = 4;
// code...
int y = 5;

(4)

// any other possibility

There is some performance difference in the form I declare and initiate my variables, Or compile take track of that for me?

Edit

I’m asking because I have read often that its better to put all declarations at the most first lines that would be better for performance. Like:

func(){
    int x,y,z,w;
    long bla,ble;
    MYTYPE weeee;
    // more declarations..
    //code..
}

But I didnt know why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:06:17+00:00Added an answer on May 28, 2026 at 6:06 pm

    I’m asking because I have read often that its better to put all declarations at the most first lines that would be better for performance.

    I can ensure you that this is pure nonsense. People making such statements have no idea whatsoever how C code is translated into machine code.

    I would be very surprised if any of your 3 examples gave different machine code.


    However there exists a special case: had the variables been declared as “globals” or static, then they would have static storage duration. And then they would be initialized before main() is called. All globals/statics that aren’t explicitly initialized by the programmer, are set to zero. So in that case, your example 1) would have been slower:

    int x; /* global variable, no explicit init so it will get set to 
              zero before main() is called */
    ...
    x = 4; // variable gets set a second time, elsewhere, in "runtime"
    

    is slower than

    int x = 4; // global variable, gets initialized before main() is called
    

    The performance difference between these two is however likely just one CPU instruction, so in 99.9% of all applications it won’t matter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below we have an IIFE which (like any function) creates a local scope. Inside
If I do something like // global scope function stuff() { // local scope
When I call a function, a local scope is erected for that call. Is
Why is the following code prints xxY? Shouldn't local variables live in the scope
Given: function foo(){ var bar = quux; console.log(/*mystery code here*/); } I'm looking for
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Basically this is a question how to access local scope handler. I trying to
I currently use my local web server to allow costumers to preview some applications
Is there a way to locate all variables inside a scope? e.g. var localScope
There's a few previous questions on StackOverflow questioning how one goes about accessing local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.