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."
 
How did you find PF?: Via Google search Hi, I have a vessel I 3D printed to investigate single bubble rise. The vessel has a 4 mm gap separated by acrylic panels. This is essentially my viewing chamber where I can record the bubble motion. The vessel is open to atmosphere. The bubble generation mechanism is composed of a syringe pump and glass capillary tube (Internal Diameter of 0.45 mm). I connect a 1/4” air line hose from the syringe to the capillary The bubble is formed at the tip...
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...
I'd like to create a thread with links to 3-D Printer resources, including printers and software package suggestions. My motivations are selfish, as I have a 3-D printed project that I'm working on, and I'd like to buy a simple printer and use low cost software to make the first prototype. There are some previous threads about 3-D printing like this: https://www.physicsforums.com/threads/are-3d-printers-easy-to-use-yet.917489/ but none that address the overall topic (unless I've missed...
Back
Top