开发者

Getting a single char and passing it to a function in C

I'm new at C and a bit at this site. We were asked to balance symbols using stacks in C. I have a function push and pop which adds and deletes the symbols to be balanced, respectively. Whenever the string contains a {, (, [ or < symbol it pushes the开发者_如何学JAVA single char to the stack. Else, it pops it. If at the end of the whole process, the stack is empty, it means the string entered is balanced.

if(string[i] == '(' || string[i] == '{' || string[i] == '[' || string[i] == '<')
     push(string[i], s);
else
     pop(s);

However, when I view it, the terminal prints numbers instead of the pushed symbols. Here is my view function:

int i;
for(i = 0; i < (s->tos + 1); i++)
    printf("%d ", s->arr[i]);


You want to tell printf to print a character not a decimal

ie.

printf("%c ", s->arr[i]);


The "%d" format flag is for integers. That's why you're seeing integers.

To see characters, use the "%c" format flag, which is for characters:

int i;
for (i = 0; i < (s->tos + 1); i++)
    printf("%c ", s->arr[i]);

See the documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜