Web lists-archives.com

Re: [Mingw-users] is this mingw error?

On 2/27/2017 12:24 PM, Chan Oak wrote:
> here source code
> #include <stdio.h>
> #define MAX 100
> char aaa[MAX];
> int addstr(char a)
> {
>      static int cur = 0;
>      if (cur > MAX)
>          return 0;
>      else
>          aaa[cur] = a;
>      return 1;
> }
> int main()
> {
>      char a;
>      int i;
>      while ((a = getchar()) != EOF)
>      {
>          addstr(a);
>      }
>      printf("%c", aaa[0]);
>      printf("hello\n");
> }
> here is output
> a
> ^Z
> hello
> the global array aaa[0] is modified yet it does not print aaa[0].
> if I put printf inside function addstr to print aaa[0], it does print

In addstr(), cur is not much of a use unless you increment it.
aaa[0] ends up with the last character before the ^Z

   printf("%c%d", aaa[0], aaa[0]);

Modified Output:


Windows command prompt does line-by-line stdin. So the last 
character before the end-of-input ^Z is a newline, which should be 
a CRLF or an LF (apparently I got LF only, so LF it is). The last 
aaa[0] will be LF which is ASCII 10, as shown above. The empty 
line after ^Z is the output of %c (which is LF), the 10 is the 
output of %d (ASCII value of LF).

Kein-Hong Man (esq.)
Selangor, Malaysia

Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
MinGW-users mailing list

This list observes the Etiquette found at 
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

You may change your MinGW Account Options or unsubscribe at:
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe