Variable definition inside switch statement

In the following code, why is the variable i not assigned the value 1 ? #include <stdio.h> int main(void) { int val = 0; switch (val) { int i = 1; //i is defined here case 0: printf("value: %dn", i); break; default: printf("value: %dn", i); break; } return 0; } When I compile, I g

switch语句中的变量定义

在下面的代码中,为什么变量i没有赋值1 ? #include <stdio.h> int main(void) { int val = 0; switch (val) { int i = 1; //i is defined here case 0: printf("value: %dn", i); break; default: printf("value: %dn", i); break; } return 0; } 当我编译时,虽然int i = 1;但我得到了一个关于i没有

C programming, Can anyone explain this?

#include <stdio.h> int main() { int c; while ((c = getchar()) != EOF) { if (c == 't') printf("\t"); else if (c == 'b') printf("\b"); else if (c == '\') printf("\\"); else putchar(c); } return 0; } In this case for an input of hi how are youdoing I get an output hithowtaretyou\doing #

C编程,任何人都可以解释这个吗?

#include <stdio.h> int main() { int c; while ((c = getchar()) != EOF) { if (c == 't') printf("\t"); else if (c == 'b') printf("\b"); else if (c == '\') printf("\\"); else putchar(c); } return 0; } 在这种情况下输入 hi how are youdoing 我得到一个输出 hithowtaretyou\doing #include <s

Macros evaluation in c programming language

Possible Duplicate: What does “#define STR(a) #a” do? #include <stdio.h> #define f(a,b) printf("yes") #define g(a) #a #define h(a) g(a) int main() { printf("%sn",h(f(1,2))); printf("%sn",g(f(1,2))); } Can somebody explain why output is different for both printf() statements. The output is different because of the order in which the preprocessor does thi

用c语言编写的宏评估

可能重复: “#define STR(a)#a”做什么? #include <stdio.h> #define f(a,b) printf("yes") #define g(a) #a #define h(a) g(a) int main() { printf("%sn",h(f(1,2))); printf("%sn",g(f(1,2))); } 有人可以解释为什么printf()语句的输出不同。 由于预处理器执行的操作的顺序不同,输出不同,这在C99标准的6.10.3节(以及后面的章节)中有描述。 特别是6.10.3.1/1中的这句话

Using Case/Switch and GetType to determine the object

Possible Duplicate: C# - Is there a better alternative than this to 'switch on type'? If you want to switch on a type of object, what is the best way to do this? Code snippet private int GetNodeType(NodeDTO node) { switch (node.GetType()) { case typeof(CasusNodeDTO): return 1; case typeof(BucketNodeDTO): return 3; case typeof

使用Case / Switch和GetType来确定对象

可能重复: C# - 有没有比'开启类型'更好的选择? 如果你想switch一个类型的对象,最好的办法是什么? 代码片段 private int GetNodeType(NodeDTO node) { switch (node.GetType()) { case typeof(CasusNodeDTO): return 1; case typeof(BucketNodeDTO): return 3; case typeof(BranchNodeDTO): return 0; case typeof(LeafNodeDT

do { ... } while (0) — what is it good for?

Possible Duplicate: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? I've been seeing that expression for over 10 years now. I've been trying to think what it's good for. Since I see it mostly in #defines, I assume it's good for inner scope variable declaration and for using breaks (instead of gotos.) Is it good for anything else? Do yo

做{...} while(0) - 有什么好处?

可能重复: 为什么在C / C ++宏中有时会出现无意义的do / while和if / else语句? 我已经看到这个表达超过10年了。 我一直在想着它有什么好处。 由于我主要在#defines中看到它,因此我认为它适用于内部作用域变量声明和使用中断(而不是gotos)。 对其他任何东西都有好处吗? 你用它吗? 这是C语言中唯一可用于#define多语句操作的构造,在后面放置分号并仍在if语句中使用。 一个例子可能有助于: #define FOO(x) fo

C# switch statement limitations

When writing a switch statement, there appears to be two limitations on what you can switch on in case statements. For example (and yes, I know, if you're doing this sort of thing it probably means your object-oriented (OO) architecture is iffy - this is just a contrived example!), Type t = typeof(int); switch (t) { case typeof(int): Console.WriteLine("int!"); break;

C#switch语句限制

在编写switch语句时,在case语句中可以打开什么看起来有两个限制。 例如(是的,我知道,如果你正在做这种事情,这可能意味着你的面向对象(OO)体系结构是有限的 - 这只是一个人为的例子!), Type t = typeof(int); switch (t) { case typeof(int): Console.WriteLine("int!"); break; case typeof(string): Console.WriteLine("string!"); break; default: Console.Wri

Free variables that are declared inside switch statement blocks

I have a switch statement which declares different variables inside each case block. I want to free these variables at the end of the routine but compiler throw an error that identifier "variable name" is not defined. I do not want to declare these variables before the switch statement. How can I resolve this error? Here is the pseudo code: int CaseType; switch(CaseType){ Case 1:

在switch语句块中声明的自由变量

我有一个switch语句,它在每个case块内声明不同的变量。 我想在例程结束时释放这些变量,但编译器会抛出一个错误,即标识符“变量名称”未定义。 我不想在switch语句之前声明这些变量。 我该如何解决这个错误? 这是伪代码: int CaseType; switch(CaseType){ Case 1: { double *a = malloc(100 * sizeof(double)); << some operation here >> break; } Case 2: { double *b = malloc(200 * si

Why break cannot be used with ternary operator?

while(*p!='' && *q!='') { if(*p==*q) { p++; q++; c++; } else break; } I have written this using ternary operator but why its giving error for break statement? *p==*q?p++,q++,c++:break; gcc compiler gives this error: expected expression before 'break' When you use a ternary operator, it is not

为什么break不能和三元运算符一起使用?

while(*p!='' && *q!='') { if(*p==*q) { p++; q++; c++; } else break; } 我用三元运算符写了这个,但是为什么它给出了break语句的错误? *p==*q?p++,q++,c++:break; gcc编译器给出这个错误:'break'之前的预期表达式 当你使用三元运算符时,它不像if 。 三元运算符具有这种形式: (condition ? expression_if_tru

single linked list primary expression error

I get the error, "expected primary expression before struct" , in the switch case in the display function on the line display(struct node* ptr); . Can anyone explain why? #include<stdio.h> #include<malloc.h> void insbeg(); void delmid(); void display(struct node* ptr); struct node { int data; struct node* next; }* start=NULL; int main() { int n; printf

单链表主要表达式错误

我在行display(struct node* ptr);的显示函数的开关情况下得到错误, “期望在结构之前的主要表达式” display(struct node* ptr); 。 谁能解释为什么? #include<stdio.h> #include<malloc.h> void insbeg(); void delmid(); void display(struct node* ptr); struct node { int data; struct node* next; }* start=NULL; int main() { int n; printf("1-insbegn"); printf("2-delmidn");

Expected ' ' before '=' token in struct definition

I have a struct I am making for a case to model a simple semaphore, the code is as follows struct semaphore{ int count = 1; struct PCB *Sem_Queue; }; When I try to compile I get the error Expected ':', ',' etc before '=' token int count = 1; Can anyone point out to me why this error is occurring? I assume you are trying to set a default value for a field in

在结构定义中''''标记之前预期''

我有一个struct我正在模拟一个简单的信号量的情况下,代码如下 struct semaphore{ int count = 1; struct PCB *Sem_Queue; }; 当我尝试编译时,我得到错误 在'='token int count = 1之前预期的':',','等; 任何人都可以指出为什么这个错误发生? 我假设你正在尝试为struct定义中的字段设置默认值。 你不可以做这个。 您必须像使用PCB一样声明count字段:仅使用类型和名称,如下所