Question

1- Variables are made up of 6 attributes list and briefly describe what each one tells...

1- Variables are made up of 6 attributes list and briefly describe what each one tells us about a variable

2- Describe how lifetime scope are similar and how they different

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1)

There are 6 attribute­­ associated with a variable. The first three are very important to start programming . The last three are important as we advance

  1. Name
  2. Type
  3. Value
  4. Scope
  5. Life Time
  6. Location (in the memory)
  1. Name

The name is Symbolic. It speaks about the "title" of the data that is being stored with the variable. The name might be the most vital property to the software engineer, since this is the means by which we "access" the variable. Each variable must have a specific/unique name. For eg, a variable storing age of person will be better relatable if it is name AgeOfPerson rather ‘x’, ‘y’ or something like this

  1. Type

The type represents what "kind" of data is stored with the variable. In C, Java the type of a variable must be explicitly declared when the name is created. Suppose we are storing the Age of person in completed years, it should be an integer rather than a float. We have different data types like int, char, float, bool

Eg of declaration of variable,

Char Name[];

int Score;

float AvgScore

  1. Value

This is the actual data store or assigned to the variable. We can change the value assigned to a variable by reassigning it later down the program. So it can be a variable which changes overtime

Eg; int Pupil_Age= 22;

Here we are giving a value of 22 to the variable Pupil_Age. We can alter it down by reassigning it to different number later

  1. Scope

Great projects are "broken" into little independent areas (called functions) particularly like a decent book is broken into sections, and a decent section is additionally partitioned into passages, and so forth. A variable that is seen and utilized in one function isn't accessible in another area. This enables us to reuse variable names, for example, Name. In one function 'Name' could allude to the Name of a person, and in another capacity 'Name' could allude to the Model name of Sports car. Further this keeps us from "unknowingly" changing data that is vital to another piece of our program.

Consider there are 2 functions in a program

Void Main()

{ int Ret_Age(int id);

Int Ret_Price(int id);

}

Ret_Age function may be a function to give the age of student given an ‘id’ . Ret_price may give the price of a car given an ‘id’. Both the cases we use same variable name ‘id’ but it supports a different function

Similarily if a variable is declared in the main section it is accessible in the area of it only. They may be called local variables

Eg main()

{ float avg_points;

}

If the variables are declared outside the main they are called global variable and are accessible in the entire program

#include <stdio.h>

/* global variable declaration */

Char grade;

int main () {

}

  1. Life Time

The lifetime of a variable is the time period for which the value of the variable is valid or it may be accessible. The Life time of a variable is firmly related to the scope of the variable. At the point when a program starts, variables will be allocated or come alive when the program reaches the line of code where they are "declared". They are erased when the program leaves the "scope" of the variable.

  1. Location

We need not worry much about this aspect in high level programming languages right from C, Java,python as the low level languages needed to assign a memory block for the program first and then allocate accordingly for variables, Arithmetic operations. We might have to be careful if there are any functions in loop and they are iterated infinitely we might face problem in high level languages also

Q2) Life time refers to the time period the variable is available to access where as the Scope refers to the visibility of a variable that is declared

Consider the below example

#include <stdio.h>
void disp();

int i = 5;  // global variable

int main()
{
    ++i;     // variable n is not declared in the main() function
    disp();
    return 0;
}

void disp()
{
    ++i;     // variable n is not declared in the display() function
    printf("i = %d", i);
}

The output will be  7 as 'i' is defined globally and is accessible in function which it is not declared also. This talks about Scope

Consider another example

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

int main () {
   char *st;

   /* Initial memory allocation */
   st = (char *) malloc(15);
   strcpy(st, "HomeworkLibstudy");
   printf("String = %s,  Address = %u\n", st, st);
/* WE can see output similar to String= HomeworkLibstudy, Address=XX900

   /* Deallocate allocated memory */
   free(st);
   
   printf("String = %s, Address = %u\n", st, st);

/* RETURNS ERROR AS IT MEMORY IS FREED

return(0);
}

We have tried to acces the variable after it is deallcoated. This is about life time of a variable

Add a comment
Know the answer?
Add Answer to:
1- Variables are made up of 6 attributes list and briefly describe what each one tells...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT