Week 5 Notes

Week 5 Notes - Week 5 Blocks Block is a stateent enclosed...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Week 5 Blocks Block is a stateent enclosed in braces. if(x) { int i = 2; x = i + 3; } You can have local vas in a compound statement. Global Variables Global vars define outside the body of the function xx.c int flag; int x(){ ... } <ul lifetime of a global variable is the entire execution of the program. scope is from where the variable is declared to the end of the file a global variable can also be used in a different file: you can declare extern xx.h extern int flag; yy.c #include "xx.h" int y() { flag = 3; } Global variables are initialized with 0 by default int flag; int xx; printf("flag = %d\n",flag); output flag = 0; Local variables won't be initialiezed. They will contain initially whatever was in the stack at execution int xx(){ int i; printf("i = %d\n",i); } main() { xx(); } output: i = -57899 //some random number Storage In C, there are 3 ways to store variabes. auto local variable, that s their lifetime starts when they are defined and ends at the end of the block. if(x){ int i = 5; } You can also write: if (x) { auto int i -5; } Auto is assumed by defaut Static : lifetime will be the duration of program. Static variables preserve their values across invocation. void login(){ static int counter = 0; printf("counter = %d\n", counter); counter++; } main(){ login(); login(); login(); } Output: counter = 0; counter = 1; counter = 2; Sometimes, static variables are used to execute code only in the first invocation of a function int valueOfX() { static int init = 0; static double x; if(!init) { //complex x = sin(x) + ln(x); init = 1; } return x; } You can also define a static value outside a function. This will make that function with a scope only in that file and nowhere else. a.c static int x = 3; int xx() { printf("x = %d\n",x); } b.c static int x = 4; int yy() { printf("x = %d\n", x); } d.c main() { xx(); yy(); } gcc -o prog a.c b.c d.c output: x = 3; y = 4; Without static, compiling error because you initialize the same variable twice. Register A register can be used to store variables and fastest memory you can have. You can tell the compiler to put a variable in a register register int i; This is just a "hint" for the compiler. If there are enough registers the compiler will use a register to store; otherwise, it will be stored in RAM....
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue.

Page1 / 10

Week 5 Notes - Week 5 Blocks Block is a stateent enclosed...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online