variable in c

variable in c


chapters

  • what is variable in c ?

  • how to declare and initialize a variable ?
    • declaration of variable
    • initialization of variable
  • local and global variable in c.
    • local variable
    • global variable

  • question and answer.

variable in c

In c language a variable is a name assign to the memory location which holds a value. in c variables allows you to store the data, modify the data, and retrieve data in your program.

example :

       int a;
       float b;
       char ch;

How to declare and initialize a variable ?

Declaration of variable :

Declaring a variable means telling the compiler about the name of variable and the type of data which a variable will hold.

Syntax for declaring variable :

datatype variable_name;

  • datatype: It specifies which type of data the variable  will hold, such as int, float, char, etc.
  • variable_name: it is the name you give to the variable.
Example

#include <stdio.h>
int main ()
{
int num;       //declaring variable name with type
return 0;
}

In above program we declared a vriable named num with int datatype which means the variable num will hold the integer value


Initialization of variable :

initializing a variable means assigning a initial value to the variable at the time of declaration.

Example

#include <stdio.h>
int main()
{
int n = 160;
return 0;
}

In the above example we declared variable named n with integer data type and assigned the value 160 to the variable. 

local and global variable in c

Local variable : In c local variables are declared inside the function or a block of code and you can only accessed local variable inside that function or block. Local variables cannot be accessed from outside the function or block in which they are declared. Local variable is typically used for temporary  storage  within a specific function or block.

example

#include <stdio.h>
int main()
{
int x;     //declared local variable named x
x=120;
printf("value of x is %d",x);
return 0;
}

In the above example we declared a local variable names x within main function ,that mean it can only be  used within  the main function and not outside of it.


Global variable: Global variables are declared outside of any function and it can be accessed from any function in the program. In c global variables are quite different from local variables, Global variables have  longer lifetime compared to  local variables. Global variables are typically used for values that need to be shared among other functions.

example

#include <stdio.h>
int x = 10;      // declared a global variable named x
in main()
{
printf("global variable is: %d",x);
return 0;
}
In the above example we declared a global variable named x outside the function so it can be accessed from any function in the program. inside main() we are accessing and printing the value of global variable. Since x is a global variable we can also modify its value by just assigning new value inside main() like this x=20; so the value of x will get modified to 10 to 20 .

Questions

  1. What is a variable in C?
  2. How do you declare a variable in C?
  3. How do you initialize a variable in C?
  4. What is the syntax for declaring a variable in C?
  5. What is a local variable in C?
  6. What is a global variable in C?
  7. Where are local variables declared in C?
  8. Where are global variables declared in C?
  9. Can a local variable be accessed outside its function or block?
  10. Can a global variable be accessed from any function in a program?

Answers

1. In C, a variable is a name assigned to the memory location which holds a value. Variables allow you to store, modify, and retrieve data in your program.

2. To declare a variable in C, you specify the data type followed by the variable name. For example: int a;.

3. To initialize a variable, you assign it an initial value at the time of declaration. For example: int n = 160;.

4. syntax is: datatype variable_name; where datatype specifies the type of data and variable_name is the name of the variable.

5. A local variable is declared inside a function or block of code and can only be accessed within that function or block.

6. A global variable is declared outside of any function and can be accessed from any function within the program.

7. Local variables are declared inside a function or a block of code.

8. Global variables are declared outside the function.

9. No, a local variable cannot be accessed outside the function or block where it is declared.

10. Yes, a global variable can be accessed from any function in the program.






Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.