Local variables

I will explain about local variables in C language. Local variables are variables declared inside a function.

Declaration of local variables

Variables are declared with the following syntax.

Type name Variable name;

Variable declarations written in functions are declarations of local variables.

int main (void) {
  Type name Variable name;
}

Let's write a sample that declares a variable of type int32_t.

#include <stdint.h>

int main (void) {
  int32_t num;
}

Initialization of local variables

Variable declarations can be made at the same time as variable initialization.

#include <stdint.h>

int main (void) {
  int32_t num = 1;
}

Note that in C, if you do not initialize the variable, it will not be automatically initialized to 0 and the value will be undefined.

For Perl variables, The difference is that it is initialized with the undefined value undef.

Scope of local variables

I will write about the scope of local variables in C language. In C language, the scope can be described using "{}". You can make variable declarations within the scope. Variables can only be referenced within scope. You can only use scopes inside functions.

#include <stdint.h>

int main (void) {
  {
    int32_t num1;
    
    1 + 1;
    
    int32_t num2;
  }
}
// You cannot see num1 and num2 at this position.

Local variable declaration is possible at any position from C99

From C99, you can declare local variables at any position. The restriction that the declaration of a local variable must be at the beginning of the block is removed.

#include <stdint.h>

int main (void) {
  {
    int32_t num1 = 5;
    
    1 + 1;
    
    // Local variables can be declared at any position
    int32_t num2 = 4;
  }
}

const modifier

You can use the const modifier on a local variable to prohibit assignment to a local variable other than initialization. This raises a compile-time error.

#include <stdio.h>
#include <stdint.h>

int main (void) {
  const int32_t num = 1;
  
  // Compile error
  num = 2;
}

constant char *

for expressing a character string

const is often used when you want to prevent assignments to string elements.

I wrote about the type "const char *" as the type that expresses a character string.

// A type that represents a string
const char * message = "Hello";

Here, I will explain the role of const in detail. const is a syntax for prohibiting assignment. So what kind of assignment is prohibited in "const char *"?

It's easy to misunderstand, but it doesn't mean that assignment to "message" is prohibited. This is ok.

// OK
message = "Goodby";

Actually, the const here depends on the pointer "*". In other words, it means prohibiting assignment to the element pointed to by the pointer.

// Compile error
message [3] ='a';

It doesn't make much sense, but if you want to prohibit assignments to variables, write: In this case const depends on the variable name. Check where it is.

char * const message = "Hello";

const can be removed by casting

const can be removed with typecast. "Eh".

char * message2 = (char *) message;

This means that const modification only means reducing mistakes. Still, you can explicitly indicate that you're using it as a string constant, and it's easy to make mistakes, so it's better to add const.

Representing an array of strings

Now, I would like to introduce another scene where const is used. This is how to create an array that cannot change the elements that include the constant string.

#include <stdio.h>

int main (void) {
  
  const char * constant strings [] = {
    "Apple",
    "Orange",
    "Grape",
  };;

  printf("%s%s\n", strings [0], strings [1]);
}

Where does const depend? Hmmm. Difficult to understand.

const char * const strings []

The first const "const char *" will be where the pointer points. In other words, prohibition of assignment of character strings to elements.

What's next? "Const strings []". Well, writing the answer prohibits assigning to variables and also to elements of arrays. Why? I don't know.

Please note that the functions of static introduced next and const introduced just now are completely different. It's easy to get confused because the atmosphere is similar.

Static local variables

In C, you can declare static local variables by adding the static qualifier to local variables.

int foo (void) {
  // Static local variables
  static int32_t foo;
  
  static int32_t bar = -5;
}

It's called a static local variable, but when you think about its actual role, it's better to think of it as a scoped global variable.

The lifespan is from the beginning to the end of the program, but you can have a scope to prevent direct access from outside the scope.

Static local variables, like global variables, are initialized with 0 (all bits are 0). If you write a constant on the right side of the assignment operator, it will be initialized with that value.

Note that the assignment in the initialization of the static declaration is done only once at the beginning of the program. The initial value is not assigned each time the function is executed.

Sample static local variables

This is a sample of static local variables. Notice that static local variables exist from the beginning to the end of the program.

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

void foo (void) {
  static int32_t num = -5;
  
  num ++;
  
  printf("%d\n", num);
}

int main (void) {
  foo ();
  foo ();
  foo ();
  foo ();
  foo ();
}

This is the output result.

-Four
-3
-2
-1
0

Associated Information