EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: Cujo on August 23, 2020, 07:20:27 am

Title: C programming
Post by: Cujo on August 23, 2020, 07:20:27 am
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses != '\0'; i++)
  {
   printf("%c", guesses);
  }

//  return display_guesses_lib ( guesses ) ;
}
Title: Re: C programming
Post by: Nusa on August 23, 2020, 07:33:16 am
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses[i] != '\0'; i++)
  {
   printf("%c", guesses[i]);
  }

//  return display_guesses_lib ( guesses ) ;
}

Fooled me for a second...the code doesn't display right in forum if you don't put it in a code block (inserted above).

As to your second question, look up the toupper() library function.
The first question I don't know enough about your environment to answer, or whether the array you're passing is even populated with anything.
Title: Re: C programming
Post by: T3sl4co1l on August 23, 2020, 07:36:45 am
You want to use the whole-ass printf() to write... single chars at a time? :o

Just use puts().

Tim
Title: Re: C programming
Post by: Cujo on August 23, 2020, 07:40:30 am
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses[i] != '\0'; i++)
  {
   printf("%c", guesses[i]);
  }

//  return display_guesses_lib ( guesses ) ;
}

Fooled me for a second...the code doesn't display right in forum if you don't put it in a code block (inserted above).

As to your second question, look up the toupper() library function.
The first question I don't know enough about your environment to answer, or whether the array you're passing is even populated with anything.

The array is populated in another function. The entire project is complete but we are required to rewire the code that is in the _lib file in our own code.
Title: Re: C programming
Post by: brucehoult on August 23, 2020, 08:26:37 am
Maybe you need to output a newline to make it flush the output buffer. Or do that explicitly with fflush(stdout) if you don't want a newline.

Or maybe there's no data in the array.
Title: Re: C programming
Post by: SiliconWizard on August 25, 2020, 04:05:53 pm
Most probable cause is hinted by Bruce above.

The output of printf() is usually not flushed to the console until it encounters a newline character.

The usual way of making sure each character is displayed here at each iteration would be to add a "fflush(stdout);" after the printf() call.

Edit: I focused on the printf() part (assuming this would be the culprit) and didn't even look at the rest... but obviously your use of the array was completely bogus. People below pointed you to what would be correct for your needs (which weren't even clear to begin with until you specified what the array really contained and what you wanted printed out.)

Still, you can keep in mind what I said above for later. Although it's implementation-defined, on most consoles, a printf() will not display anything until a "newline" (or end-of-file, I think, which is CTRL-Z?) character is encountered. So to print character by character, you'd need a fflush() call as I suggested. I don't remember if putc() suffers from the same behavior.
Title: Re: C programming
Post by: MarkF on August 25, 2020, 07:36:18 pm
I think you want:

Code: [Select]
void display_guesses(char *guesses)
{
   for (int i=0; guesses[i] != 0; i++) {
      if (guesses[i]>='a' && guesses[i]<='z')
         guesses[i] &= 0x5f;
      printf("%c", guesses[i]);
   }
}

void main(void)
{
   char testString[64]="Some test characters";

   display_guesses(testString);
}
Title: Re: C programming
Post by: Cujo on August 26, 2020, 05:14:59 am
It's not an array of characters, it's an array of 1's and 0's. Each element of the array corresponds to a letter, 0 is 'a', 1 is 'b', etc, 25 is 'z'. So you need to check the entire array, and only print out the letters that have a '1' in their spot.

I'm too confused on how to do this?
Title: Re: C programming
Post by: T3sl4co1l on August 26, 2020, 05:41:00 am
Do you mean guesses[0] corresponds to 'a', and so on; or do you mean a value of 0 in a given position corresponds to "a", and so on?

'1' is not in the range 'a' to 'z' (single quotes mean "use the numeric value of this character") so it's not obvious that that should do anything at all?

If the elements have numeric values 0-25 and you want to convert them to letters, use:
putc(guesses[ i ] + 'a');

Tim
Title: Re: C programming
Post by: Cujo on August 26, 2020, 06:19:26 am
Do you mean guesses[0] corresponds to 'a', and so on;

That is exactly what I mean. So how do I implement that into my code?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses != '\0'; i++)
  {
   printf("%c", guesses);
  }

//  return display_guesses_lib ( guesses ) ;
}
Title: Re: C programming
Post by: T3sl4co1l on August 26, 2020, 06:48:25 am
So guesses[] is not so much char as bool type?  Only takes values 0 or 1?  (char can be used either way, but I mean semantically, how it's being used.)

Would that be a

Code: [Select]
if (guesses[i]) putc('a' + i);

in the loop then?

Tim
Title: Re: C programming
Post by: grumpydoc on August 26, 2020, 06:51:46 am
Do you mean guesses[0] corresponds to 'a', and so on;

That is exactly what I mean. So how do I implement that into my code?

Code: [Select]
#include <stdio.h>

int display_guesses(unsigned char *guesses)
{
  int i;

  for(i = 0; i < 26; i++)  {
    if (guesses[i] != 0)
      putchar(i + 'a');   
  }
  putchar('\n'); /* or fflush(stdout) if you don't want the newline. */
}

Ah, Tim beat be to it :)
Title: Re: C programming
Post by: T3sl4co1l on August 26, 2020, 07:00:30 am
Note that if checks for nonzero, so the "!=" isn't necessary, but can help for clarity.

Note also that guesses[] needs to be size 26 or greater, otherwise the for loop breaks (reads gibberish memory); and you can't check for a zero terminator if zero is a normal value, i.e. it's not a string array.

Also I don't remember the difference between putc, putchar etc. because I mostly do embedded and don't use either. :P One of those (or other related) will do the job in any case.

Tim
Title: Re: C programming
Post by: grumpydoc on August 26, 2020, 07:22:48 am
Note that if checks for nonzero, so the "!=" isn't necessary, but can help for clarity.

Cujo is clearly an inexperienced C programmer so I actually left in the check for clarity. I agree an experienced programmer would elide this.

Quote
Also I don't remember the difference between putc, putchar etc. because I mostly do embedded and don't use either. :P One of those (or other related) will do the job in any case.
putchar assumes stdout, putc/fputc takes the stream as a parameter.