c programming interview questions and answers_ static variable in c

12
8/14/2015 C programming Interview questions and answers: static variable in c http://www.cquestions.com/2011/02/static-variable-in-c.html 1/12 C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types array recur sion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime number series factorial palindrome code programs examples on c++ tutorials and pdf C programming Interview questions and answers C tu t or ia l C Pr og r am mi n g Qu est io ns C I nt er vi ew Q u es ti ons C Pr ogra ms C Te st C pr ogr am mi n g pd f Pr ogram of c+ + S ql Se r ve r  static variable in c static keyword in c:  Keyword static is used for declaring static variables in c. This modifier is u sed with all data types like int, float, double, array, pointe r, structure, function etc. Important points about static keyword: 1. It is not default storage class of global variables. For example, analyze the following three programs and its output. (a) #include<stdio.h> int a; int main(){  printf( "%d",a);  return 0; } Output: 0 (b) #include<stdio.h> static int a; int main(){  printf( "%d",a);  return 0; } Output: 0 (c) #include<stdio.h> extern int a; int main(){  printf( "%d",a);  return 0; } Output: Compilation error At first glance if you will observe the output of above three codes you can say default storage class of global variable is C progra m examples C interview qu estions and answers Data type questions Variable naming rule questions Operators questions Control flow questions Switch case questions Looping questions Pointer questions String questions Printf,Scanf questions Preprocessor questions Structure questions Commad line argument C questions in Linux C online test C mixed practice sets C tricky questions Example of recursion in c C programming forums C QUESTIONS AND ANSWERS Memory mapping tutorial in c Variables tutorial in c Data types tutorial in c Storage classes tutorial in c Looping tutorial in c Pointers tutorial in c Function tutorial in c Array tutorial in c Preprocessor tutorial in c Advanced c tutorial C TUTORIAL C program examples | Interview Complete List POPULAR POSTS Se  Adv  Arra C pr C++ Data Exa File Fun Java linux Loop Mem Oper pdf  ( Poin Prep SQL LAB PAG 7 with Me  Alre C L E D SUB STA ?

Upload: rajesh

Post on 14-Feb-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 1/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 1

C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arra

recur sion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numb

series factorial palindrome code programs examples on c++ tutorials and pdf

C programming Interview questions and answers

C tutorial C Programming Questions C Interview Questions C Programs C Test C programming pdf Program of c++ Sql Serve

static variable in c

static keyword in c:

Keyword static is used for declaring static variables in

c. This modifier is used with all data types like int, float,

double, array, pointer, structure, function etc.

Important points about static keyword:

1. It is not default storage class of global variables. For

example, analyze the following three programs and its output.

(a)

#include<stdio.h>

int a;

int main()

printf("%d",a);

return 0;

Output: 0

(b)

#include<stdio.h>

static int a;

int main()

printf("%d",a);

return 0;

Output: 0

(c)

#include<stdio.h>

extern int a;

int main()

printf("%d",a);

return 0;

Output: Compilation error

At first glance if you will observe the output of above three

codes you can say default storage class of global variable is

C program examples

C interview questions and answers

Data type questions

Variable naming rule questions

Operators questions

Control flow questions

Switch case questions

Looping questions

Pointer questions

String questions

Printf,Scanf questions

Preprocessor questions

Structure questions

Commad line argument

C questions in Linux

C online test

C mixed practice sets

C tricky questions

Example of recursion in c

C programming forums

C QUESTIONS AND ANSWERS

Memory mapping tutorial in c

Variables tutorial in c

Data types tutorial in c

Storage classes tutorial in c

Looping tutorial in c

Pointers tutorial in c

Function tutorial in c

Array tutorial in c

Preprocessor tutorial in c

Advanced c tutorial

C TUTORIAL

C program examples | Interview

Complete List

POPULAR POSTS

A

A

C

C

D

E

F

F

J

li

L

M

O

p

P

P

S

L

P

7

A

C

S

S?

Page 2: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 2/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 2

static. But it is not true. Why? Read extern storage class.

2. Default initial value of static integral type variables are

zero otherwise null. For example:

#include <stdio.h>

static char c;

static int i;

static float f;

static char *str;

int main()

printf("%d %d %f %s",c,i,f,str);

return 0;

Output: 0 0 0.000000 (null)

3. A same static variable can be declared many times but we

can initialize at only one time. For example:

(a)

#include <stdio.h>

static int i; //Declaring the variable i.

static int i=25; //Initializing the variable.

static int i; //Again declaring the variable i.

int main()

static int i; //Again declaring the variable i.

printf("%d",i);

return 0;

Output: 25

(b)

#include <stdio.h>

static int i; //Declaring the variable

static int i=25; //Initializing the variable

int main()

printf("%d",i);

return 0;

static int i=20; //Again initializing the variable

Output: Compilation error: Multiple initialization variable i.

4. We cannot write any assignment statement globally. For

example:#include <stdio.h>

static int i=10; //Initialization statement

i=25; //Assignment statement

int main()

printf("%d",i);

return 0;

Output: Compilation error

Note: Assigning any value to the variable at the time of

C interview questions and answers

QUICK SORT USING C PROGRAM

Check given number is prime number or

not using c program

Program to convert decimal to binary in

c

Find out the perfect number using c

program

TO FIND FACTORIAL OF A NUMBERUSING C PROGRAM

TO FIND FIBONACCI SERIES USING C

PROGRAM

Write a c program to reverse a string

Merge sort program in c

C questions and answers

C interview questions and answers

Debugging questions in c with answers

Aptitude questions and answers in c

C basic questions

C PROGRAMMING QUESTIONS ANDANSWER

There was an error in this gadget

133

VD

Page 3: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 3/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 3

declaration is known as initialization while assigning any

value to variable not at the time of declaration is known

assignment.

(b)

#include <stdio.h>

static int i=10;

int main()

i=25; //Assignment statement

printf("%d",i); return 0;

Output: 25

(5) A static variable initializes only one time in whole

program. For example:

#include <stdio.h>

static int i=10;

int main()

i=5;

for(i=0;i<5;i++)

static int a=10; //This statement will execute

//only time.

printf("%d",a++);//This statement will execute

//five times.

return 0;

Output: 10 11 12 13 14

(6)If we declared static variable locally then its visibility

will within a block where it has declared. For example:

#include<stdio.h>

int main()

static int a=5;

printf("%d",a);

//printf("%d",a); variable a is not visible here.

return 0;

Output: 5

7. If declared a static variable or function globally then its

visibility will only the file in which it has declared not in

the other files. For example:

(a)

#include<stdio.h>

static float a=144.0f; //global to all function

int main()

printf("%d",a); //variable a is visible here.

Page 4: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 4/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 4

//printf("%d",b); variable b is not visible here.

printf("%d",a); //variable a is visible here.

//printf("%d",b); variable b is not visible here.

return 0;

static int b=5; //Global to only calculation function

void calculation()

printf("%d",a); //variable a is visible here.

printf("%d",b); //variable b is visible here.

(b) Consider a c program which has written in two files named

as one.c and two.c:

//one.c

#include<conio.h>

static int i=25;

static int j=5;

void main() clrscr();

sum();

getch();

//two.c

#include<stdio.h>

extern int i; //Declaration of variable i.

extern int j; //Declaration of variable j.

/**

Above two lines will search the initialization statement of

variable i and j either in two.c (if initialized variable is

static or extern) or one.c (if initialized variable is

extern)

*/

extern void sum()

int s;

s=i+j;

printf("%d",s);

Compile and execute above two file one.c and two.c at the same

time:

In Turbo c compiler

Step 1: Write above two codes in the file named as one.c and

two.c (You can give any name as you like) and save it.

Step 2: In Turbo c++ IDE click on Project -> Open project menu

as shown in following screen dump.

Page 5: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 5/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 5

Step 3: After Clicking on open project you will get following

screen:

In Open project File text field write any project name with

.prj extension. In this example I am writing project name as

CProject.PRJ. Now press OK button.

Step 4: After pressing OK button you will get following

screen:

Page 6: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 6/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 6

Now click on Project -> Add item menu.

Step 5: After clicking Add item you will get following screen:

In the name text field write down all c source code file one

by one i.e. first write one.c and click on Add button

Then write two.c and click on Add button and so on

Page 7: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 7/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 7

Step 6: At the end click on Done button. After clicking on

done button you will get following screen:

At the lower part of window you can see project name, list of

files you have added etc.

Step7: To compile the two files press Alt+F9 and to run the

above program press Ctrl+F9

Note: To close the project click on Project -> Close project.

Output: Compilation error: Unknown symbol i and j.

Hence we can say variable i and j which has initialized into

two.c is not visible in file one.c. This example proves

visibility of globally declared static variable is file.

Note: In the above example function sum which was declared and

defined in two.c has also storage class extern. So we can call

from other file (one.c).If it will static then we cannot call

Page 8: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 8/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 8

function sum since static storage class is only visible to the

file where it has declared.

(8)If we static variable has declared locally or globally its

scope will always whole the program. For example:

(a) //locally declaration of static variable

#include<stdio.h>

void visit();

int main()

int i=0;

//Opening inner block

static int a=5; //locally declaration

XYZ:; //Label of goto statement

printf("%d ",a);

a++;

i++;

//closing inner block.

visit();

/* printf("%d",a); Variable a is not visible here but

it is alive. */ if(i<5)

goto XYZ;

return 0;

void visit()

Output: 5 6 7 8 9

Explanation: When program control will come out of inner block

where variable a has declared then outside of inner block

variable a is not visible but its scope is outside the program

i.e. variable a hasn’t dead .If with help of goto statement

control again comes inside the inner block it prints previous

incremented values which was not possible in case of auto or

register variables.

(b)

//Locally declarations of variable

There are two c source code files:

//one.c

#include<stdio.h>

#include<conio.h>

void main()

int i;

for(i=0;i<3;i++)

static int a=5;

printf("%d\n",a);

a++;

visit();

getch();

Page 9: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 9/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 9

//two.c

#include<stdio.h>

void visit()

printf("Don’t disturb, I am learning storage class");

/* printf("%d",a); Variable a is not visible here but

It is alive. */

Now compile and execute both files together:Output:

5

disturb, I am learning storage class

6

disturb, I am learning storage class

7

disturb, I am learning storage class

Explanation: When control goes to another file and comes even

that variable didn’t dead and it prints previous incremented

value.

Note: In both examples if you will declare static variableglobally you will get same output.

9. A static variables or functions have internal linkage. An

internal linkage variables or functions are visible to the

file where it has declared.

Introduction

auto

register

static

extern

C tutorial home.

+6 Recommend this on Google

Replies

Reply

21 comments:

Aarti 2/18/11, 12:11 AM

explanation is just fab.

Reply

akash rai 8/5/15, 10:10 PM

But concepts are wrong... pls try to execute the prg your self u will find lots of error

Anonymous 6/16/11, 12:23 PM

In 8th point (b)part,you have called visit in one.c file but in two.c file you have not declared the extern keyword

before its defination...can u plz tel me why?

Reply

Anonymous 8/6/11, 1:46 PM

Page 10: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 10/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 10

Replies

Reply

Replies

Reply

in the 8th point we r learning the scope of static at out side of the files..

Reply

Anonymous 9/21/11, 3:24 PM

in the 8th point the visit() function must be declared extern in two.c .....correct me if i am wrong..??

Reply

Simas A. 6/16/14, 4:59 PM

All functions have extern as the default storage class, so no you don't need to write it.

dinhpq 10/13/11, 11:14 PM

Thanks.

Reply

Anonymous 11/18/11, 1:08 PM

reaaly nice 1....commendable

Reply

ranjith 1/2/12, 4:59 PM

#include

static int i; //Declaring the variable i.

static int i=25; //Initializing the variable.

static int i; //Again declaring the variable i.

int main()

static int i; //Again declaring the variable i.

printf("%d",i);

return 0;

Output: 25

her if we compile output we should get 0 only ..becz in main block locally i of course static is not initialised...so

it intialissed by default as zero....comparing local and global local variable got more preference than global

static variable(i=25)..so compulsory we will get zero as output....

plz comment to this comment ..to conform it correct analysis or not

Reply

Anitha Venkatesan 1/26/13, 3:16 AM

Ya thats correct actually it gives output as o.

Vijai 2/11/14, 9:05 PM

Yes you are correct in gcc I do get the same.

vijay

urvinder 2/25/12, 1:38 AM

i checked in vc++,it gives the error while compiling /**error 'i' : redefinition; different storage class**/

Reply

Anonymous 2/25/13, 8:58 PM

that was a complete tutorial!! thanks brother!!

Reply

Page 11: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 11/12

8/14/2015 C programming Interview questions and answers: static variable in c

http://www.cquestions.com/2011/02/static-variable-in-c.html 11

Replies

Reply

Atiqur Rahman 4/7/13, 11:11 AM

Reply

SALIL AGRAWAL 12/24/13, 12:02 PM

Similar Article see if it makes sense

Static Variables in C

Reply

karthik nayak 5/19/14, 7:29 PM

brilliant explanation :D

Reply

pravin 9/2/14, 11:22 AM

yes correct

Reply

Amit Kumar 2/22/15, 3:34 PM

Why its giving output as : 43

Can anyone please explain it to me?

Thanx in advance ?

#include

static int i; //Declaring the variable i.

static int i=25; //Initializing the variable.

static int i; //Again declaring the variable i.

int main()

int i; //Again declaring the variable i.

printf("%d",i);

return 0;

Reply

Nóra Vörös 3/4/15, 5:59 PM

int i; //Again declaring the variable i.

There is no static keyword in front of the declaration. It is "another" i, an automatic, uninitialized i.

The content of the memory area where it is placed is accidentally 43.

Raj Naik 6/15/15, 3:02 PM

3. A same static variable can be declared many times but we can initialize at only one time. For example:

(a)

#include

static int i; //Declaring the variable i.

static int i=25; //Initializing the variable.

static int i; //Again declaring the variable i.

int main()

static int i; //Again declaring the variable i.

printf("%d",i);

return 0;

Output: 25

Here Output should be zero, not 25.

Reply

Page 12: C Programming Interview Questions and Answers_ Static Variable in c

7/23/2019 C Programming Interview Questions and Answers_ Static Variable in c

http://slidepdf.com/reader/full/c-programming-interview-questions-and-answers-static-variable-in-c 12/12

8/14/2015 C programming Interview questions and answers: static variable in c

Newer Post Older PostHome

Subscribe to: Post Comments (Atom)

Sign out

Notify me

Enter your comment...

Comment as: Ha ri kr ishn a Ko

Publish

Preview

Create a Link

Meenu Rajput 7/31/15, 10:32 AM

Here, it gives an output 25 but not zero because it has been initialized as 25 although it has been declared

again as a local variable.

Reply

Links to this post

Copyright@Priyanka. Picture Window template. Powered by Blogger .