Need help interpreting this C (programming) Code

  • Thread starter Thread starter pyroknife
  • Start date Start date
  • Tags Tags
    Code Programming
Click For Summary

Discussion Overview

The discussion revolves around interpreting a C programming code snippet that categorizes a single character input by the user. Participants explore the behavior of the if and else if statements, particularly in relation to how input is processed by the scanf function.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant questions the logic of the else if statement, suggesting that it should only recognize single-digit numbers, based on their input of "100".
  • Another participant clarifies that scanf only captures the first character of the input, which in the case of "100" is '1'.
  • A third participant advises against making assumptions about the value of c and suggests using a debugger or printing the value to understand its behavior better.
  • A later reply confirms the understanding that scanf reads a single character due to the variable being defined as "char c".

Areas of Agreement / Disagreement

Participants generally agree on the behavior of scanf and its handling of input, but there is an initial misunderstanding regarding the interpretation of the input value.

Contextual Notes

The discussion highlights the importance of understanding how input functions work in C, particularly in relation to variable types and the nature of user input.

pyroknife
Messages
611
Reaction score
4
Code:
// Program to categorize a single character that is entered at the terminal

#include <stdio.h>

int main (void)

{
     char c;

     printf ("Enter a single character:\n");
     scanf ("%c", &c);

     if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
          printf ("It's an alphabetic character.\n");
     else if ( c >= '0' && c <= '9' )
          printf ("It's a digit.\n");
     else
          printf ("It's a special character.\n");

     return 0;
}

Alright so I don't quite get the if and else if statements.

For "else if ( c >= '0' && c <= '9' )" shouldn't that only work for single digit #s? I typed in 100, which set c=100, but the output still gave me "It's a digit." I don't understand why. 100 is >0, but not <9.
 
Last edited by a moderator:
Engineering news on Phys.org
It's only taking the first character of what you type in, which in your example is 1
 
pyroknife said:
I typed in 100, which set c=100.

Don't "guess" the value of c that you read in. Either look at it with a debugger, or print it out.

As PHinds said, you haven't understood what the scanf() call does.
 
oh I see. scanf() only takes in one character b/c the input is defined as "char c."
 

Similar threads

Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
14
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K