switch(var){ case 1: { int variable; // Declare variable in new block // Do other stuff here :P } break; ... default: break;}
New blocks are useful in some switch statements because when you use them to jump to different cases, you can create a new block and create local variables inside of it.Code: [Select]switch(var){ case 1: { int variable; // Declare variable in new block // Do other stuff here :P } break; ... default: break;}It's perfectly valid
Code: [Select]const float pi = 3.14159f;{float stinky = pi;}
const float pi = 3.14159f;{float stinky = pi;}
You said that is a bad style in the code, but funny, you just did that.
const float pi = 3.14159f; { float stinky = pi; }
So the code following is a bad programming practice, right?Code: [Select]const float pi = 3.14159f; { float stinky = pi; }
Quote from: TheProgrammerSo the code following is a bad programming practice, right?Code: [Select]const float pi = 3.14159f; { float stinky = pi; } Actually, that code is perfectly good programming practice.The compiler will throw out everything inside the braces.
You're really confusing me. Why would you write a C code that doesn't look like C?
/* This may or may not compile, I wrote it as part of the post */#include <stdio.h>int main() { const float pi = 3.14159f; { float stinky = pi; } return 0;}
/* This may or may not compile, I wrote it as part of the post */#include <stdio.h>void foo(int*);void bar(float*);void baz(int*);int main() { /* Test foo */ { int i1, i2, i3; i3 = 6; foo(&i1); foo(&i2); if (i3 == (i1-i2)) { printf("foo() passes!\n"); } } /* i1, i2, and i3 are popped off the stack and look * as if they never existed. This lets each test * think it's in its own separate testing function * and removes conflicts without having to write * a crapload of testing functions. */ /* Test bar */ { float f1, f2, f3; /* .... */ } /* Test baz */ { /* .... */ } return 0;}
/* some function in foo.c, assume constants are defined */int calculateFoo(int iParam) { int i; for (i=0;i<10;i++) { iParam+=i; } { int temp; temp = doSomething(); temp = iParam * PI*PI * temp; return temp; }}
if (expression) command
if (a == 1) printf("hi");
if (a==1) printf("hi"); printf("hello");
if (a==1) { printf("hi"); printf("hello"); }
printf("hi");{ printf("hello");}
printf("hi");printf("hello");
But since { is like a command, it can be used everywhere commands can be used, including plain source code:Code: [Select]printf("hi");{ printf("hello");}which this is equal to:Code: [Select]printf("hi");printf("hello");
int a = 10;{int a = 20;a *= 20;}printf("a = %d\n", a);
int a = 10;a = 20;a *= 20;printf("a = %d\n", a);
Get a book on writing a compiler. It'll help. A lot.
"If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend."
Get a book on writing a compiler.
Quote from: The RomeroGet a book on writing a compiler.hey, you know where i can get, compiler writer tuteral on the internet?