SlideShare a Scribd company logo
©LPU CSE101 C Programming
• Storage Classes and Scope Rules
©LPU CSE101 C Programming
Outline
• Storage Classes
– auto
– static
– extern
– register
• Scope Rules
©LPU CSE101 C Programming
Storage Classes
• Storage Classes are used to describe the features of a
variable. These features basically include the scope, visibility
and life-time which help us to trace the existence of a
particular variable during the runtime of a program.
• Storage class specifies four things-
I. Storage location: Where the variable will be stored?[ In
memory /or CPU register]
II. Scope: Block in which the variable is accessible.
III. Lifetime: How long would the variable exist?
IV. Default initial value: What will be the initial value of the
variable, if initial value is not specifically assigned ?
©LPU CSE101 C Programming
Storage Classes: Auto
• Automatic storage
– auto int x, y;
– It is the default storage class for a local variable
– This is the default storage class for all the variables
declared inside a function or a block
⮚ Storage − Memory(RAM).
⮚ Default initial value − An unpredictable value, which is
often called a garbage value.
⮚ Scope − Local to the block in which the variable is
defined.
⮚ Lifetime − Till the control remains within the block in
which the variable is defined.
©LPU CSE101 C Programming
Program example-auto storage class
#include<stdio.h>
void func1()
{
auto int a=10; // Local variable of func1()
printf("n a=%d",a);
}
void func2()
{
auto int a=20; //Local variable of func2()
printf("n a=%d",a);
}
int main()
{
auto int a=30;//Local variable of main()
func1();
func2();
printf("n a=%d",a);
return 0;
}
©LPU CSE101 C Programming
Storage Classes: Register
• register: tries to put variable into high-speed
registers.
– register int counter = 1;
⮚ Storage - CPU registers.
⮚ Default initial value - Garbage value.
⮚ Scope - Local to the block in which the variable is
defined.
⮚ Lifetime - Till the control remains within the block in
which the variable is defined.
©LPU CSE101 C Programming
More points in relation to register storage class
• This storage class declares register variables which have the same
functionality as that of the auto variables. The only difference is that the
compiler tries to store these variables in the register of the
microprocessor if a free register is available.
• This makes the use of register variables to be much faster than that of the
variables stored in the memory during the runtime of the program. If a
free register is not available, these are then stored in the memory only.
• Usually few variables which are to be accessed very frequently in a
program are declared with the register keyword which improves the
running time of the program. An important and interesting point to be
noted here is that we cannot obtain the address of a register variable
using pointers.
©LPU CSE101 C Programming
Program example-register storage class
#include<stdio.h>
int main()
{
register int i; // i will be used frequently so, it can be given register storage class
for(i=1;i<=20;i++)
{
printf("n%d",i);
}
return 0;
}
©LPU CSE101 C Programming
Storage Classes: Static
• Static storage
⮚ Storage − Memory(RAM).
⮚ Default initial value − Zero.
⮚ Scope − Local to the block in which the variable is
defined.
⮚ Life time − variable will retain its value throughout the
program
©LPU CSE101 C Programming
More points in relation to static storage class
• Static variables have a property of preserving their value even
after they are out of their scope! Hence, static variables
preserve the value of their last use in their scope.
• So we can say that they are initialized only once and exist till
the termination of the program. Thus, no new memory is
allocated because they are not re-declared.
• Their scope is local to the function to which they were
defined. Global static variables can be accessed anywhere in
the program. By default, they are assigned the value 0 by the
compiler.
©LPU CSE101 C Programming
Program example-static storage class
#include<stdio.h>
void function();
int main()
{
function();
function();
function();
return 0;
}
void function()
{
int a=10;
static int b=10;
printf("n Value of a:%d, Value of
b:%d",a,b);
a++;
b++;
}
Output:
Value of a:10, Value of b:10
Value of a:10, Value of b:11
Value of a:10, Value of b:12
©LPU CSE101 C Programming
Storage Classes: extern
Extern storage class simply tells us that the variable is
defined elsewhere and not within the same block
where it is used. Basically, the value is assigned to it in
a different block and this can be overwritten/changed
in a different block as well
⮚ Storage − Memory(RAM).
⮚ Default initial value − Zero.
⮚ Scope − Global.
⮚ Life − As long as the program’s execution doesn’t come
to an end.
©LPU CSE101 C Programming
More points in relation to extern storage
class
• extern variable is nothing but a global variable
initialized with a legal value where it is declared in
order to be used elsewhere. It can be accessed within
any function/block.
• Also, a normal global variable can be made extern as
well by placing the ‘extern’ keyword before its
declaration/definition in any function/block.
• This basically signifies that we are not initializing a new
variable but instead we are using/accessing the global
variable only. The main purpose of using extern
variables is that they can be accessed between two
different files which are part of a large program.
©LPU CSE101 C Programming
Program example 1-extern storage class
External variable in the same file
#include<stdio.h>
void first();
int main()
{
extern int x; /* declaration in main() */
printf("nx=%d",x); // x is used before its definition[Possible because of extern]
first();
printf("nx=%d",x);// Changes done by first are visible here
return 0;
}
void first()
{
extern int x; /* declaration in first() */
printf("nx=%d",x); // x is used again before its definition[Possible because of extern]
x=x+10;
}
int x=10; /* definition of external variable, here x is global variable */
©LPU CSE101 C Programming
Program example 2-extern storage class
External variable in different file
extern1.c file
#include<stdio.h>
#include"extern2.c"
//Global variable declared in extern1.c
int x=30;
int main()
{
print();
printf("%d",x);//Changes done by extern2.c
file are also reflected
}
extern2.c file
void print()
{
extern int x;//Taking reference of
global variable in different file or
Declaration
printf("%dn",x);
x=x+10;
}
//Output:
30
40
©LPU CSE101 C Programming
Summary
©LPU CSE101 C Programming
Scope Rules
• The scope of a variable is the portion of a
program where the variable has meaning
(where it exists).
• A global variable has global (unlimited) scope.
• A local variable’s scope is restricted to the
function that declares the variable.
• A block variable’s scope is restricted to the
block in which the variable is declared.
©LPU CSE101 C Programming
Local variables
• Parameters and variables declared inside the
definition of a function are local.
• They only exist inside the function body.
• Once the function returns, the variables no
longer exist!
©LPU CSE101 C Programming
Example-local variable
#include<stdio.h>
void function();
int main()
{
int a=1,b=2;
printf("n a is:%d,b is:%d",a,b);//a,b are local variables of main()
function();
return 0;
}
void function()
{
int c=3;
printf("n Value of c is:%d",c);// c is a local variable of function
}
©LPU CSE101 C Programming
Block Variables
• You can also declare variables that exist only
within the body of a compound statement (a
block):
{
int f;
…
…
}
©LPU CSE101 C Programming
Example-block scoped variable
#include<stdio.h>
int main()
{
int b=2;
{
int a=1; // a is block variable
printf("nValue of a is:%d",a);
}
printf("nValue of b is:%d",b);
return 0;
}
©LPU CSE101 C Programming
Global variables
• You can declare variables outside of any
function definition – these variables are
global variables.
• Any function can access/change global
variables.
©LPU CSE101 C Programming
Example-global variable
#include<stdio.h>
int a=1;// a is a global variable
void print();
int main()
{
printf("nValue of a is:%d",a);
print();
return 0;
}
void print()
{
printf("nValue of a is:%d",a);
}
©LPU CSE101 C Programming
More examples-Scope rules
#include<stdio.h>
int a=10;// a is a global variable
void print();
int main()
{
int a=1;
printf("nValue of a is:%d",a);// It will
access local a
print();
return 0;
}
void print()
{
printf("nValue of a is:%d",a); //It will
access global a
}
Output:
Value of a is:1
Value of a is:10
Note:
When we have same named
local and global variables,
priority is always given to local
variable first, that is why in
main() function value of local a is
printed, whereas in function
definition, there is no local
variable so preference is given
to global version of a
©LPU CSE101 C Programming
More examples-Scope rules
#include<stdio.h>
int a=10;// a is a global variable
void print();
int main()
{
printf("nValue of a is:%d",a);
print();
printf("nValue of a is:%d",a);// Change
done by print to global a is reflected here
return 0;
}
void print()
{
printf("nValue of a is:%d",a);
a=20;
}
Output:
Value of a is:10
Value of a is:10
Value of a is:20
©LPU CSE101 C Programming
More examples-Scope rules
#include<stdio.h>
int main()
{
int a=5;
{
int a=50;
{
int a=500;
printf("na:%d",a);
}
printf("na:%d",a);
}
printf("na:%d",a);
return 0;
}
Output:
a:500
a:50
a:5
©LPU CSE101 C Programming
Q1
What will be the output of the following
C code?
#include <stdio.h>
int x;
void m();
int main()
{
m();
printf("%d", x);
return 0;
}
void m()
{
x = 4;
}
A. 0
B. 4
C. Compile time error
D. Runtime error
©LPU CSE101 C Programming
Q2
What will be the output of the following C code?
#include <stdio.h>
int x = 5;
void m();
void n();
int main()
{
int x = 3;
m();
printf("%d", x);
return 0;
}
void m()
{
x = 8;
n();
}
void n()
{
printf("%d", x);
}
A. 8 3
B. 3 8
C. 8 5
D. 5 3
©LPU CSE101 C Programming
Q3
What will be the output of following
code?
#include <stdio.h>
int main()
{
int x=1;
{
x=2;
{
int x=3;
}
}
printf("%d",x);
return 0;
}
A. 1
B. 2
C. 3
D. Compile time error
©LPU CSE101 C Programming
Q4
In case of a conflict between the names of a
local and global variable what happens?
A. The global variable is given a priority.
B. The local variable is given a priority.
C. Which one will get a priority depends upon
which one is defined first.
D. The compiler reports an error.
©LPU CSE101 C Programming
Q5
What will be the storage class of variable i in the code
written below?
#include<stdio.h>
int main()
{
int i = 10;
printf("%d",i);
return 0;
}
A. Automatic storage class
B. Extern storage class
C. Static storage class
D. Register storage class
©LPU CSE101 C Programming
Q6
What will be the behaviour of following code?
#include<stdio.h>
int main()
{
register int a;
printf("nEnter value of a:");
scanf("%d",&a);
return 0;
}
A. Program will work normally
B. Compile time error
C. Runtime error
D. None of the above
©LPU CSE101 C Programming
Q7
#include<stdio.h>
int incr(int i)
{
static int count = 0;
count = count + i;
return (count);
}
int main()
{
int i,j;
for (i = 0; i <=2; i++)
j = incr(i);
printf("%d",j);
return 0;
}
A. 10
B. 4
C. 3
D. 2
©LPU CSE101 C Programming
Q8
#include<stdio.h>
void update();
int main()
{
update();
update();
return 0;
}
void update()
{
auto int a=1;
static int b=1;
a++;
b++;
printf("%d,%dn",a,b);
}
A. 2,2
2,3
B. 2,2
2,2
C. 1,1
1,2
D. 2,1
2,2
©LPU CSE101 C Programming
Q9
#include<stdio.h>
int main()
{
extern int a;
printf("%d",++a);
return 0;
}
int a;
A. 0
B. 1
C. -1
D. Compile time error

More Related Content

DOCX
Storage class
PPTX
Storage class
PPT
Lecture 13 - Storage Classes
PPTX
Storage classes in C
PPTX
Storage classes in c language
PPTX
Storage classes in c language
PDF
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
PPTX
Storage classes
Storage class
Storage class
Lecture 13 - Storage Classes
Storage classes in C
Storage classes in c language
Storage classes in c language
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
Storage classes

Similar to Storage_classes_and_Scope_rules.pptx (20)

PPTX
Storage class in c
PPTX
Storage classes
PPTX
STORAGE CLASS.pptx
PDF
Advanced C Programming Notes
PDF
Storage classes arrays & functions in C Language
PPT
Storage classes
PPTX
Storage Class in C Progrmming
PPTX
Storage classes in C
PPT
Storage classes
PPTX
Storage class
PPTX
Storage classes
PPT
S torage class in C
PDF
Storage class
PDF
Latest C Interview Questions and Answers
PPTX
C language updated
PDF
C Programming Storage classes, Recursion
PPTX
Storage Classes and Functions
DOC
Storage classess of C progamming
PPTX
visiblity and scope.pptx
PPTX
C language
Storage class in c
Storage classes
STORAGE CLASS.pptx
Advanced C Programming Notes
Storage classes arrays & functions in C Language
Storage classes
Storage Class in C Progrmming
Storage classes in C
Storage classes
Storage class
Storage classes
S torage class in C
Storage class
Latest C Interview Questions and Answers
C language updated
C Programming Storage classes, Recursion
Storage Classes and Functions
Storage classess of C progamming
visiblity and scope.pptx
C language
Ad

More from CheriviralaNikhil (6)

PPT
A323568347_29695_5_2024_Lecture0_INT232.ppt
PDF
Introduction to OS.pdf
PDF
DPPM V.pdf
PPT
Lecture1-2.ppt
PPTX
UNIT1Lesson 2.pptx
PPTX
Data Models.pptx
A323568347_29695_5_2024_Lecture0_INT232.ppt
Introduction to OS.pdf
DPPM V.pdf
Lecture1-2.ppt
UNIT1Lesson 2.pptx
Data Models.pptx
Ad

Recently uploaded (20)

PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
English Language Teaching from Post-.pdf
PDF
Types of Literary Text: Poetry and Prose
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PPTX
How to Manage Global Discount in Odoo 18 POS
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Landforms and landscapes data surprise preview
PPTX
ACUTE NASOPHARYNGITIS. pptx
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Onica Farming 24rsclub profitable farm business
Introduction and Scope of Bichemistry.pptx
English Language Teaching from Post-.pdf
Types of Literary Text: Poetry and Prose
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
How to Manage Starshipit in Odoo 18 - Odoo Slides
vedic maths in python:unleasing ancient wisdom with modern code
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Module 3: Health Systems Tutorial Slides S2 2025
Sunset Boulevard Student Revision Booklet
Renaissance Architecture: A Journey from Faith to Humanism
NOI Hackathon - Summer Edition - GreenThumber.pptx
UTS Health Student Promotional Representative_Position Description.pdf
How to Manage Global Discount in Odoo 18 POS
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Landforms and landscapes data surprise preview
ACUTE NASOPHARYNGITIS. pptx
Software Engineering BSC DS UNIT 1 .pptx

Storage_classes_and_Scope_rules.pptx

  • 1. ©LPU CSE101 C Programming • Storage Classes and Scope Rules
  • 2. ©LPU CSE101 C Programming Outline • Storage Classes – auto – static – extern – register • Scope Rules
  • 3. ©LPU CSE101 C Programming Storage Classes • Storage Classes are used to describe the features of a variable. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program. • Storage class specifies four things- I. Storage location: Where the variable will be stored?[ In memory /or CPU register] II. Scope: Block in which the variable is accessible. III. Lifetime: How long would the variable exist? IV. Default initial value: What will be the initial value of the variable, if initial value is not specifically assigned ?
  • 4. ©LPU CSE101 C Programming Storage Classes: Auto • Automatic storage – auto int x, y; – It is the default storage class for a local variable – This is the default storage class for all the variables declared inside a function or a block ⮚ Storage − Memory(RAM). ⮚ Default initial value − An unpredictable value, which is often called a garbage value. ⮚ Scope − Local to the block in which the variable is defined. ⮚ Lifetime − Till the control remains within the block in which the variable is defined.
  • 5. ©LPU CSE101 C Programming Program example-auto storage class #include<stdio.h> void func1() { auto int a=10; // Local variable of func1() printf("n a=%d",a); } void func2() { auto int a=20; //Local variable of func2() printf("n a=%d",a); } int main() { auto int a=30;//Local variable of main() func1(); func2(); printf("n a=%d",a); return 0; }
  • 6. ©LPU CSE101 C Programming Storage Classes: Register • register: tries to put variable into high-speed registers. – register int counter = 1; ⮚ Storage - CPU registers. ⮚ Default initial value - Garbage value. ⮚ Scope - Local to the block in which the variable is defined. ⮚ Lifetime - Till the control remains within the block in which the variable is defined.
  • 7. ©LPU CSE101 C Programming More points in relation to register storage class • This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. • This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only. • Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.
  • 8. ©LPU CSE101 C Programming Program example-register storage class #include<stdio.h> int main() { register int i; // i will be used frequently so, it can be given register storage class for(i=1;i<=20;i++) { printf("n%d",i); } return 0; }
  • 9. ©LPU CSE101 C Programming Storage Classes: Static • Static storage ⮚ Storage − Memory(RAM). ⮚ Default initial value − Zero. ⮚ Scope − Local to the block in which the variable is defined. ⮚ Life time − variable will retain its value throughout the program
  • 10. ©LPU CSE101 C Programming More points in relation to static storage class • Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. • So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. • Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
  • 11. ©LPU CSE101 C Programming Program example-static storage class #include<stdio.h> void function(); int main() { function(); function(); function(); return 0; } void function() { int a=10; static int b=10; printf("n Value of a:%d, Value of b:%d",a,b); a++; b++; } Output: Value of a:10, Value of b:10 Value of a:10, Value of b:11 Value of a:10, Value of b:12
  • 12. ©LPU CSE101 C Programming Storage Classes: extern Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well ⮚ Storage − Memory(RAM). ⮚ Default initial value − Zero. ⮚ Scope − Global. ⮚ Life − As long as the program’s execution doesn’t come to an end.
  • 13. ©LPU CSE101 C Programming More points in relation to extern storage class • extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. • Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. • This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
  • 14. ©LPU CSE101 C Programming Program example 1-extern storage class External variable in the same file #include<stdio.h> void first(); int main() { extern int x; /* declaration in main() */ printf("nx=%d",x); // x is used before its definition[Possible because of extern] first(); printf("nx=%d",x);// Changes done by first are visible here return 0; } void first() { extern int x; /* declaration in first() */ printf("nx=%d",x); // x is used again before its definition[Possible because of extern] x=x+10; } int x=10; /* definition of external variable, here x is global variable */
  • 15. ©LPU CSE101 C Programming Program example 2-extern storage class External variable in different file extern1.c file #include<stdio.h> #include"extern2.c" //Global variable declared in extern1.c int x=30; int main() { print(); printf("%d",x);//Changes done by extern2.c file are also reflected } extern2.c file void print() { extern int x;//Taking reference of global variable in different file or Declaration printf("%dn",x); x=x+10; } //Output: 30 40
  • 16. ©LPU CSE101 C Programming Summary
  • 17. ©LPU CSE101 C Programming Scope Rules • The scope of a variable is the portion of a program where the variable has meaning (where it exists). • A global variable has global (unlimited) scope. • A local variable’s scope is restricted to the function that declares the variable. • A block variable’s scope is restricted to the block in which the variable is declared.
  • 18. ©LPU CSE101 C Programming Local variables • Parameters and variables declared inside the definition of a function are local. • They only exist inside the function body. • Once the function returns, the variables no longer exist!
  • 19. ©LPU CSE101 C Programming Example-local variable #include<stdio.h> void function(); int main() { int a=1,b=2; printf("n a is:%d,b is:%d",a,b);//a,b are local variables of main() function(); return 0; } void function() { int c=3; printf("n Value of c is:%d",c);// c is a local variable of function }
  • 20. ©LPU CSE101 C Programming Block Variables • You can also declare variables that exist only within the body of a compound statement (a block): { int f; … … }
  • 21. ©LPU CSE101 C Programming Example-block scoped variable #include<stdio.h> int main() { int b=2; { int a=1; // a is block variable printf("nValue of a is:%d",a); } printf("nValue of b is:%d",b); return 0; }
  • 22. ©LPU CSE101 C Programming Global variables • You can declare variables outside of any function definition – these variables are global variables. • Any function can access/change global variables.
  • 23. ©LPU CSE101 C Programming Example-global variable #include<stdio.h> int a=1;// a is a global variable void print(); int main() { printf("nValue of a is:%d",a); print(); return 0; } void print() { printf("nValue of a is:%d",a); }
  • 24. ©LPU CSE101 C Programming More examples-Scope rules #include<stdio.h> int a=10;// a is a global variable void print(); int main() { int a=1; printf("nValue of a is:%d",a);// It will access local a print(); return 0; } void print() { printf("nValue of a is:%d",a); //It will access global a } Output: Value of a is:1 Value of a is:10 Note: When we have same named local and global variables, priority is always given to local variable first, that is why in main() function value of local a is printed, whereas in function definition, there is no local variable so preference is given to global version of a
  • 25. ©LPU CSE101 C Programming More examples-Scope rules #include<stdio.h> int a=10;// a is a global variable void print(); int main() { printf("nValue of a is:%d",a); print(); printf("nValue of a is:%d",a);// Change done by print to global a is reflected here return 0; } void print() { printf("nValue of a is:%d",a); a=20; } Output: Value of a is:10 Value of a is:10 Value of a is:20
  • 26. ©LPU CSE101 C Programming More examples-Scope rules #include<stdio.h> int main() { int a=5; { int a=50; { int a=500; printf("na:%d",a); } printf("na:%d",a); } printf("na:%d",a); return 0; } Output: a:500 a:50 a:5
  • 27. ©LPU CSE101 C Programming Q1 What will be the output of the following C code? #include <stdio.h> int x; void m(); int main() { m(); printf("%d", x); return 0; } void m() { x = 4; } A. 0 B. 4 C. Compile time error D. Runtime error
  • 28. ©LPU CSE101 C Programming Q2 What will be the output of the following C code? #include <stdio.h> int x = 5; void m(); void n(); int main() { int x = 3; m(); printf("%d", x); return 0; } void m() { x = 8; n(); } void n() { printf("%d", x); } A. 8 3 B. 3 8 C. 8 5 D. 5 3
  • 29. ©LPU CSE101 C Programming Q3 What will be the output of following code? #include <stdio.h> int main() { int x=1; { x=2; { int x=3; } } printf("%d",x); return 0; } A. 1 B. 2 C. 3 D. Compile time error
  • 30. ©LPU CSE101 C Programming Q4 In case of a conflict between the names of a local and global variable what happens? A. The global variable is given a priority. B. The local variable is given a priority. C. Which one will get a priority depends upon which one is defined first. D. The compiler reports an error.
  • 31. ©LPU CSE101 C Programming Q5 What will be the storage class of variable i in the code written below? #include<stdio.h> int main() { int i = 10; printf("%d",i); return 0; } A. Automatic storage class B. Extern storage class C. Static storage class D. Register storage class
  • 32. ©LPU CSE101 C Programming Q6 What will be the behaviour of following code? #include<stdio.h> int main() { register int a; printf("nEnter value of a:"); scanf("%d",&a); return 0; } A. Program will work normally B. Compile time error C. Runtime error D. None of the above
  • 33. ©LPU CSE101 C Programming Q7 #include<stdio.h> int incr(int i) { static int count = 0; count = count + i; return (count); } int main() { int i,j; for (i = 0; i <=2; i++) j = incr(i); printf("%d",j); return 0; } A. 10 B. 4 C. 3 D. 2
  • 34. ©LPU CSE101 C Programming Q8 #include<stdio.h> void update(); int main() { update(); update(); return 0; } void update() { auto int a=1; static int b=1; a++; b++; printf("%d,%dn",a,b); } A. 2,2 2,3 B. 2,2 2,2 C. 1,1 1,2 D. 2,1 2,2
  • 35. ©LPU CSE101 C Programming Q9 #include<stdio.h> int main() { extern int a; printf("%d",++a); return 0; } int a; A. 0 B. 1 C. -1 D. Compile time error