Static Variables and Functions in C Programming

Static Variables and Functions in C Programming
Slide Note
Embed
Share

Examples and exercises demonstrating the usage of static variables and functions in C programming. Understand how static storage duration works, and how these elements retain their values between function calls. Additionally, practice implementing if-else statements, loops, and patterns to enhance your C programming skills.

  • C programming
  • Static variables
  • Functions
  • Loops
  • If-else statements

Uploaded on Apr 23, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Static #include <stdio.h> void next(void); static int counter = 7; /* global variable */ int main() { while(counter<10) { next(); counter++; } return 0;} void next( void ) { /* function definition */ static int iteration = 13; /* local static variable */ iteration ++; printf("iteration=%d and counter= %d\n", iteration, counter);}

  2. answer iteration=14 and counter= 7 iteration=15 and counter= 8 iteration=16 and counter= 9

  3. Static Ex-2 #include <stdio.h> static int a= 7; /* global variable */ int main() { add(a); add(a); add(a); } int add(int b) { /* function definition */ b++; printf("%d\n",b); return b; }

  4. Exercise number 1 1. Program to check the number enter by user is less than 10 or not ,using if else 2. 3. 4. Print 1 to 15 numbers using while Numbers not divisible by 2, 3, 5 using if else Print the table of a given no. using for loop 5. 6. 7. Print the pattern of a star using for loop Square roots of 1 to 9 numbers using for loop Print whether a number is lowercase or uppercase using if else 8. Right angled triangle pattern

  5. Displaying right angled triangle for 5 rows * * * * * * * * * * * * * * *

  6. A right angle triangle

  7. Mcq 1 #include< stdio.h> int main() { extern int a; static char j = E ; printf( %c %d , ++j, ++a); return 0; } a) E 2 b) F 1 c) F Garbage d) F 0

  8. Mcq 2 Value of static storage variable A changes during different function calls B persists between different function calls C increases during different function calls D decreases during different function calls Chapters

  9. Mcq 3 Which of the following statement are correct? (i) The value stored in the CPU register can always be accessed faster than that stored in memory. (ii) A register storage class variable will always be stored in a CPU register. a) Only I is correct b) Only II is correct c) Both I & II are correct d) Both I & II are incorrect

  10. Mcq 4 Which of the following statement are correct? (i) The maximum value a variable can hold depends upon its storage class. (ii) By default all variables enjoy a static storage class. a) Only I is correct b) Only II is correct c) Both I & II are correct d) Both I & II are incorrect

  11. Mcq 5 #include < stdio.h> static int y = 1; int main() { static int z; printf( %d %d , y, z); return 0; } a) Garbage value b) 0 0 c) 1 0 d) 1 1

  12. Mcq 6 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.

  13. Mcq 7 #include < stdio.h> extern int p; int sum = 5; int main() { p = bomb(); printf( %d %d , sum, p); return 0; } bomb() { sum ++; return (sum); } a) The code reports an error as expression syntax b) The code gets compiled successfully but will not give any output. c) The code gives an output as 6. d) The code reports an error as bomb undefined.

  14. Mcq 8 Use of functions A.Helps to avoid repeating a set of statements many times. B.Enhances the logical clarity of the program. C.Helps to avoid repeated programming across programs. D.Makes the debugging task easier. E.All of the above

  15. Mcq 9 Any c program A Must contain at least one function. B.Need not contain any function. C.Needs input data. D.None of the above

  16. Mcq 10 What characters are allowed in a C function name identifier? a) Alphabets, Numbers, %, $, _ b) Alphabets, Numbers, Underscore ( _ ) c) Alphabets, Numbers, dollar $ d) Alphabets, Numbers, %

  17. Mcq 11 #include <stdio.h> int main() { printf("%d", main); return 0; } (A) Address of main function (B) Compiler Error (C) Runtime Error (D) Some random value

  18. Mcq 12 main() { int x=20; int y=10; swap(x,y); printf( %d %d ,y,x+2); } swap(int x,int y) { int temp; temp =x; x=y; y=temp; }

  19. Mcq 13

  20. Mcq 14

More Related Content