0

I have a string, which I declare as char myStr[ ] = "5.3 2.4 1.8".

When I print it directly with printf, it seems to print what I would expect. However, when I print each index, instead of getting myStr[3] = ' ' , I get myStr[3] = 32. Below is my code and output. I would like to know why this is happening, and how I can ensure that the spaces will be interpreted as space and not the number 32?

#include <stdio.h>
#include <string.h>

int words(char myStr[ ]);

int main(){
char myStr[ ] = “5.3 2.4 1.8”;
printf(“myStr is: %s\n", myStr);
words(myStr);
}

int words(char myStr[ ]){
  int i, length, count=0, prev=0;
  length= strlen(myStr);

   printf("The length of myStr is: %d\n", length);

  for (i=0; i<length; i++){
   printf("The %d letter of myStr is: %d\n", i, myStr[i]);
   if (myStr[i] != ' '){
      if (prev=0)
        count++;
      else
        prev=1;
    }
    else
      prev=0;
  }
  return count;
}

Below is the output:

The first line is: 5.3 2.4 1.8
The length of myStr is: 11
The 0 letter of myStr is: 53
The 1 letter of myStr is: 46
The 2 letter of myStr is: 51
The 3 letter of myStr is: 32
The 4 letter of myStr is: 50
The 5 letter of myStr is: 46
The 6 letter of myStr is: 52
The 7 letter of myStr is: 32
The 8 letter of myStr is: 49
The 9 letter of myStr is: 46
The 10 letter of myStr is: 56

Thank you for any advice!

3
  • 1
    Use %c to interpret it as a character instead of %d which will print the ASCII value of the character. Also change if (prev=0) to if (prev==0). = is the assignment operator, and is different from ==, which is the comparision operator. Commented Mar 13, 2015 at 2:53
  • 1
    To print a character, use "%c" as the format specifier. Commented Mar 13, 2015 at 2:53
  • 1
    32 is the ASCII (Unicode) value for space or blank. Just like the digits have values between 48 and 57, blanks are 32 and decimal points are 46. Commented Mar 13, 2015 at 2:55

2 Answers 2

2

Cause each char is stored as an 8-bit-width integer from the memory's perspective, according to ASCII table

In printf use %c will get char and %d will get the int

e.g:

char a = 'A';
printf("%c\n", a);
printf("%d\n", a);

the output will be:

A
65

the piece of a's memory just like

        +-----------+                
    a   | 01000001  |                
        +-----------+                

two different results just because the output format of printf

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @simon_xia. One side question I notice is the line if (myStr[i] != ' '). For some reason, this evaluates for false each time, even though myStr is character type. So, when I run the int words() function, I get zero words, when I should get 3 (since there are two spaces in myStr "5.3 2.4 1.8"). Do you know how I could get my program to recognize myStr[3] as a ' ' , so that the statement if (myStr[i] != ' ') will evaluate to true twice, and return a word count of three? Many thanks again...
@user2808302 the mystr[i] != ' ' is working correctly. You're probably confused because if (prev=0) doesn't do what you think it does
@user2808302 the statment if (prev=0) should be if (prev==0)
PS: It's a good practice to use int words(char *myStr); instead of int words(char myStr[ ]); @user2808302
1

If we look at the table of format strings for printf(), we see that if we want to print an integer we would use %d. You claim that you would like to print a character. Which is %c.

So, let's fix your call to printf():

printf("The %d letter of myStr is: %c\n", i, myStr[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.