Need help interpreting this C (programming) Code

  • Thread starter Thread starter pyroknife
  • Start date Start date
  • Tags Tags
    Code Programming
AI Thread Summary
The discussion focuses on a C program that categorizes a single character input as either alphabetic, a digit, or a special character. A user expressed confusion regarding the behavior of the `else if` statement, questioning why entering "100" resulted in the output "It's a digit." It was clarified that `scanf()` reads only one character at a time, so when "100" is input, only the first character '1' is processed. The importance of understanding how `scanf()` works and the data type of the variable `c` was emphasized. The conversation highlights the need for clarity on input handling in C programming.
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."
 
Thread 'Physics of Stretch: What pressure does a band apply on a cylinder?'
Scenario 1 (figure 1) A continuous loop of elastic material is stretched around two metal bars. The top bar is attached to a load cell that reads force. The lower bar can be moved downwards to stretch the elastic material. The lower bar is moved downwards until the two bars are 1190mm apart, stretching the elastic material. The bars are 5mm thick, so the total internal loop length is 1200mm (1190mm + 5mm + 5mm). At this level of stretch, the load cell reads 45N tensile force. Key numbers...
After over 25 years of engineering, designing and analyzing bolted joints, I just learned this little fact. According to ASME B1.2, Gages and Gaging for Unified Inch Screw Threads: "The no-go gage should not pass over more than three complete turns when inserted into the internal thread of the product. " 3 turns seems like way to much. I have some really critical nuts that are of standard geometry (5/8"-11 UNC 3B) and have about 4.5 threads when you account for the chamfers on either...
Back
Top