Did I misunderstand my professor’s C programming output question?

0
0
Asked By CodingNinja77 On

Hey everyone! I've just started a C programming course, and I had an online test where one of the questions presented this code snippet. I selected the option that says it would print "B", but my professor marked it wrong and claimed it results in a syntax error. I ran the code in an IDE at home, and it printed "B" just fine! I did some digging and learned about the dangling else problem, where the else statement relates to the nearest if statement, but my professor insists there's a syntax error. Am I the one confused here? I'd really appreciate your insights as I'm new to coding.

3 Answers

Answered By TechieTraveler89 On

Actually, in C, it's not mandatory to have curly brackets around if-else statements if you only have one line of code. Your code should work fine as is, and it should output "B" unless there's something odd with your setup. But hear me out, a syntax error usually means it wouldn’t compile at all, so that seems off here.

CurlyBraces474 -

Yeah, I agree! If it's only one line that follows the if condition, the brackets can be skipped. Sounds more like a misunderstanding than an actual syntax error.

Answered By CodeMaster45 On

I think you're on the right track. If the output was "B", then it sounds like the code is correct. Maybe it's a tricky question? In C, if you haven't defined the conditions properly, it could lead to confusion, but your output checks out!

Answered By SyntaxSleuth77 On

A syntax error would prevent your code from running at all. It sounds like a mix-up. You could also check with some coding tools or even ask your professor for clarification on what to expect from the output to clear this up.

BeginnerBytes22 -

That’s a good idea! It never hurts to double-check or ask for more info, especially since you're new to this stuff.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.