简体   繁体   English

C 中的堆栈括号匹配

[英]Stack Parenthesis Matching in C

When I execute this code and debug, I see the "h" between op1 and op2 values when I entered two parentheses () .当我执行此代码并进行调试时,当我输入两个括号()时,我看到op1op2值之间的“h”。 I don't know where it came from.我不知道它是从哪里来的。 Can you help me to solve this problem?你能帮我解决这个问题吗? It's about data structure without pointers.这是关于没有指针的数据结构。 I don't know where this garbage value comes from.我不知道这个垃圾值是从哪里来的。

Problems:问题:

op = pop();
printf("op1: %ch\n", op); //() = h
op2 = ch;
printf("op2: %ch\n", op2);   

Link below which has full of code.下面的链接有完整的代码。 https://paste2.org/yNhFcx5s https://paste2.org/yNhFcx5s

The format specifier for char is %c. char 的格式说明符是 %c。 the extra h added by you is being printed正在打印您添加的额外 h

If op and op2 have type short int , the correct code would be:如果opop2的类型为short int ,则正确的代码为:

op = pop();
printf("op1: %hc\n", op);
op2 = ch;
printf("op2: %hc\n", op2);

and since the type is actually char , you might even write:由于类型实际上是char ,你甚至可以写:

op = pop();
printf("op1: %hhc\n", op);
op2 = ch;
printf("op2: %hhc\n", op2);

But you could just use %c as short and char values are promoted to int when passed to variadic functions such as printf() .但是您可以只使用%c作为short并且char值在传递给诸如printf()之类的可变参数函数时会提升为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM