C programming - Page 1
Forum section: electronics / general

author: Cujo
author_rank: Regular Contributor
timestamp: August 23, 2020, 07:20:27 am
content: 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 ) ;}

author: Nusa
author_rank: Super Contributor
timestamp: August 23, 2020, 07:33:16 am
content: Quote from: Cujo on August 23, 2020, 07:20:27 amFooled 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: August 23, 2020, 07:36:45 am
content: You want to use the whole-assprintf()to write... single chars at a time?Just useputs().Tim

author: Cujo
author_rank: Regular Contributor
timestamp: August 23, 2020, 07:40:30 am
content: Quote from: Nusa on August 23, 2020, 07:33:16 amThe 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.

author: brucehoult
author_rank: Super Contributor
timestamp: August 23, 2020, 08:26:37 am
content: 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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 25, 2020, 04:05:53 pm
content: 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.

author: MarkF
author_rank: Super Contributor
timestamp: August 25, 2020, 07:36:18 pm
content: I think you want:Code:void display_guesses(char *guesses){for (int i=0; guesses != 0; i++) {if (guesses>='a' && guesses<='z')guesses &= 0x5f;printf("%c", guesses);}}void main(void){char testString="Some test characters";display_guesses(testString);}

author: Cujo
author_rank: Regular Contributor
timestamp: August 26, 2020, 05:14:59 am
content: 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?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: August 26, 2020, 05:41:00 am
content: Do you mean guesses 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

author: Cujo
author_rank: Regular Contributor
timestamp: August 26, 2020, 06:19:26 am
content: Quote from: T3sl4co1l on August 26, 2020, 05:41:00 amThat is exactly what I mean. So how do I implement that into my code?Code:int display_guesses( unsigned char guesses[] ){int i;for(i = 0; guesses != '\0'; i++){printf("%c", guesses);}// return display_guesses_lib ( guesses ) ;}

author: T3sl4co1l
author_rank: Super Contributor
timestamp: August 26, 2020, 06:48:25 am
content: 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 aCode:if (guesses) putc('a' + i);in the loop then?Tim

author: grumpydoc
author_rank: Super Contributor
timestamp: August 26, 2020, 06:51:46 am
content: Quote from: Cujo on August 26, 2020, 06:19:26 amCode:#include <stdio.h>int display_guesses(unsigned char *guesses){int i;for(i = 0; i < 26; i++) {if (guesses != 0)putchar(i + 'a');}putchar('\n'); /* or fflush(stdout) if you don't want the newline. */}Ah, Tim beat be to it

author: T3sl4co1l
author_rank: Super Contributor
timestamp: August 26, 2020, 07:00:30 am
content: Note thatifchecks 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.One of those (or other related) will do the job in any case.Tim

author: grumpydoc
author_rank: Super Contributor
timestamp: August 26, 2020, 07:22:48 am
content: Quote from: T3sl4co1l on August 26, 2020, 07:00:30 amCujo is clearly an inexperienced C programmer so I actually left in the check for clarity. I agree an experienced programmer would elide this.Quoteputchar assumes stdout, putc/fputc takes the stream as a parameter.