[go: up one dir, main page]

0% found this document useful (0 votes)
96 views4 pages

Scope and Lifetime

The document discusses different types of variables in C programming and their scope, lifetime, and memory location. It defines local variables, formal parameters, global variables, static variables, extern variables, register variables, auto variables, volatile variables, const variables, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views4 pages

Scope and Lifetime

The document discusses different types of variables in C programming and their scope, lifetime, and memory location. It defines local variables, formal parameters, global variables, static variables, extern variables, register variables, auto variables, volatile variables, const variables, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Local variable: this is a variable defined inside any function (user-defined function or inside the main

function).

Scope: Inside the function at where it was defined (can’t be seen/used outside the function).

Lifetime:

 Created when the compiler pass by the definition statement.


 Destroyed when the compiler exits the function.

Location in memory: Stack

Formal parameter: the parameters that the function takes.

Scope: Inside the function at where it was defined (can’t be seen/used outside the function).

Lifetime:

 Created when the compiler enters the function.


 Destroyed when the compiler exits the function.

Location in memory: Stack

Global variable: a variable that is defined outside all the function in the file (before the main function)

Scope: Inside the file at where it was defined (can’t be seen/used outside the file without using extern).

Lifetime:

 Created when the compiler pass by the definition statement.


 Destroyed when the compilation is complete (end of the program).

Location in memory: Data segment


Static + local variable: globalizes the variable

Scope: like the normal local variable

Lifetime:

 Created when the compiler pass by the definition statement.


 Destroyed when the compilation is complete (end of the program).

Memory: Data Segment

Static + global variable: makes it invisible outside the file at where it was declared (can’t use extern)

Scope: ONLY the file at where it was declared

Lifetime:

 Created when the compiler pass by the definition statement.


 Destroyed when the compilation is complete (end of the program).

Memory: Data Segment

extern + global variable: this is a declaration (not definition) of a variable


i.e.: the programmer tells the compiler that this variable exits in another file

register + variable: tell the compiler to put the variable in the internal register inside the processor
this is done with the frequently accessed variables. Ex.: loop index may be defined as register to save the
time.

auto + local variable = local variable (it’s the default)

volatile + variable: tell the compiler not to make optimizations


const + (local/global)variable: value of the variable cannot be modified by the program.

Scope: same as normal local and global variables

Lifetime: same as normal local and global variables

Location in memory: same as normal local and global variables

In all compilers size of Char = 8bits = 1 byte.


In most compilers size of int = 2 size of char = 2 bytes.
In all compilers size of short int = size of int.
In all compilers size of long int = 2 size of int.
In all complier int = signed short int (default).

unsigned + variable: all the range is +ve

signed + variable: half the range is –ve and the other half is +ve

short + variable: the same range as the variable

long + variable: doubles the range

Write functions to do the following:

1- The function take (pointer to integer) and return (pointer to integer) that contains the factorial
of the input parameter. Check all the corner cases.
2- Define pulse width modulation and mention why do we need it?
3- What does Real time system means? Is it good to finish the required task in ¾ of the time
specified for the task?
4- Mention the difference between the micro controller and the micro-processor
5- Write a function that take an array of integer and the number of elements and returns the
average of the elements of the array. Check all the corner cases.
6- Write a function that multiplies two 3x3 arrays using the following equation :

7- Explain this: ((A>B)? (A-B): (B-A))

You might also like