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

  • SEARCH
  • Home
  • 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 6888343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:00:09+00:00 2026-05-27T06:00:09+00:00

In C, does initialising a variable to it’s own value make sense? If yes,

  • 0

In C, does initialising a variable to it’s own value make sense? If yes, what for?

Allow me to elaborate. In Git sources there are some examples of initialising a variable to it’s own undefined value, as seen in transport.c or wt-status.c. I removed assignments from those declarations and run tests. Seeing no regressions, I thought that those assignments were redundant.

On the other hand, I did some simple tests with GCC 4.6 and Clang 2.9.

#include <stdio.h>
int main() {
  printf("print to increase probability of registers being non-zero\n");
  int status = status;
  return printf("%i\n", status);
}

Compiling with -Wall -std=c99 and various -O levels prints no warnings and shows that status == 0. Clang with a non-zero optimisation level prints some garbage values though. It makes me infer that results of such expressions are undefined.

I can imagine that such assignment can suppress an uninitialised variable warning, but it’s not the case for the examples taken from Git. Removing assignments doesn’t introduce any warnings.

Are such assignments an undefined behaviour? If not, what do you use them for?


I’ve suggested the change on the Git mailing list. Here’s what I’ve learned.

  • 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-27T06:00:10+00:00Added an answer on May 27, 2026 at 6:00 am

    This compiles because Standard C99 §6.2.1/7 says:

    Any identifier that is not a structure, union, or enumeration tag “has scope that begins just after the completion of its declarator.” The declarator is followed by the initializer.

    However, value of status is Indeterminate. And you cannot rely on it being initialized to something meaningful.

    How does it work?
    int status creates an space for the variable to exist on the stack(local storage) which is then further read to perform status = status, status might get initialized to any value that was present in the stack frame.

    How can you guard against such self Initialization?
    gcc provides a specific setting to detect self Initializations and report them as errors:

    -Werror=uninitialized -Winit-self

    Why is it used in this code?
    The only reason I can think it is being used in the said code is to suppress the unused variable warning for ex: In transport.c, if the control never goes inside the while loop then in that control flow cmp will be unused and the compiler must be generating a warning for it. Same scenario seems to be with status variable in wt-status.c

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

Sidebar

Related Questions

Does anyone know how to transform a enum value to a human readable value?
What is the best way to define that a value does not exist in
I have an activity for initialising a game, that does multiple selects and inserts
What does assigning a variable to {} , mean? Is that initializing it to
I'm trying to understand the importance of initializing a variable, before assigning a value
Does Google force employees who have offers from Facebook to leave immediately?
Does anyone remember the XMP tag? What was it used for and why was
Does the Java language have delegate features, similar to how C# has support for
Does anybody know any good resources for learning how to program CIL with in-depth
Does anyone know how to get IntelliSense to work reliably when working in C/C++

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.