English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Return value of printf() and scanf() in C

When using the static keyword, the variable or data member or function cannot be modified again. It is allocated throughout the life cycle of the program. Static functions can be called directly using the class name.

Static variables are initialized only once. The compiler retains the variable until the end of the program. Static variables can be defined inside or outside a function. They are local. The default value of static variables is zero. Static variables are valid before the program execution.

This is the syntax of static variables in C language,

static datatype variable_name = value;

Here,

datatype-The data type of the variable, such as int, char, float, etc.

variable_name-This is the variable name given by the user.

Value-to initialize any value of the variable. By default, it is zero.

This is an example of a static variable in C language,

Example

#include <stdio.h>
int main() {
   static int a = 8;
   int b = 10;
   printf("Value of static variable : %d\n", a);
   printf("Value of non",-static variable : %d\n", b);
   return 0;
}

Output Result

Value of static variable : 8
Value of non-static variable : 10

In the above program, two variables are declared, one is a static variable and the other is a non-static variable. The variables are initialized with values and displayed as follows-

static int a = 8;
int b = 10;
printf("Value of static variable : %d\n", a);
printf("Value of non",-static variable : %d\n", b);