1

So I have a simple code that scans every letter of a word and writes it's value and adress using a pointer.

My problem is that %c doesn't scan spaces, and I would like them to. How do I manage that?

#include <stdio.h>

int main()
{
    char c1, c2, *p_c;
    while (scanf_s(" %c", &c1) == 1)
    {
        if (c1 == '*')
            break;
        p_c = &c1;
        c2 = *p_c;
        printf("c1: %c (%p) c2: %c (%p) c_p: %c (%p)\n", c1, &c1, c2, &c2, *p_c, p_c);
    }

    return 0;
}

For example

Input:

is this C?*

Expected output:

c1: i (00D3F7CF) c2: i (00D3F7C3) c_p: i (00D3F7CF)
c1: s (00D3F7CF) c2: s (00D3F7C3) c_p: s (00D3F7CF) 
c1:   (00D3F7CF) c2:   (00D3F7C3) c_p:   (00D3F7CF)
c1: t (00D3F7CF) c2: t (00D3F7C3) c_p: t (00D3F7CF)
c1: h (00D3F7CF) c2: h (00D3F7C3) c_p: h (00D3F7CF)
c1: i (00D3F7CF) c2: i (00D3F7C3) c_p: i (00D3F7CF)
c1: s (00D3F7CF) c2: s (00D3F7C3) c_p: s (00D3F7CF)
c1:   (00D3F7CF) c2:   (00D3F7C3) c_p:   (00D3F7CF)
c1: C (00D3F7CF) c2: C (00D3F7C3) c_p: C (00D3F7CF)
c1: ? (00D3F7CF) c2: ? (00D3F7C3) c_p: ? (00D3F7CF)

My code just entirely skips spaces as if they weren't there at all. Thanks in advance.

Community
  • 1
  • 1
  • 1
    The output you show, is that the *actual* or the *expected* output? Please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 03 '18 at 02:53
  • 5
    Also, with the `scanf` format `" %c"`, what do you think that leading space does? Please read e.g. [this `scanf` (and family) reference](https://en.cppreference.com/w/c/io/fscanf) for more information. – Some programmer dude Nov 03 '18 at 02:54
  • 1
    Specifically the paragraph immediately following the heading **"A directive is one of the following:"** in [scanf(3) - Linux manual page](http://man7.org/linux/man-pages/man3/scanf.3.html) – David C. Rankin Nov 03 '18 at 03:19
  • the format specifier '%c' can pickup ANY byte value, including a space. The core of the problem is that the format string to `scanf()` is " %c" and the leading space results in ALL leading 'white space' to be skipped. (white space is spaces, tabs, newlines, etc ) – user3629249 Nov 03 '18 at 03:24

1 Answers1

0

Coding error

"%c" with scanf_s() requires 2 arguments, rsize_t and char*.
This also implies compilation might not be done with all warnings enabled.
Save time, enable all warnings.

The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s. C11dr §K.3.5.3.4 4

The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments ... §K.3.5.3.2 4

// scanf_s(" %c", &c1)
scanf_s(" %c", &c1, sizeof c1)

My problem is that %c doesn't scan spaces, and I would like them to. How do I manage that?

" " consumes white-space

To not consume white-space like ' ', omit the leading " "

// scanf_s(" %c", &c1, sizeof c1)
scanf_s("%c", &c1, sizeof c1)

Or simplify with scanf()

scanf("%c", &c1)

Or simplify further with getchar()

char c1;
int ich;
while ((ich = getchar()) != EOF) {
  c1 = (char) ich;
  ...
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks, removing the space helped, I was using it in earlier stages to get rid of problems with print. Although I can't use the scanf function, because my compiler in Visual Community Studio 2017 never let's me not use the scanf_s, says it may be unsafe... – Michal Kováč Nov 03 '18 at 20:25
  • @MichalKováč Perhasp [Remove secure warnings ... from projects by default in Visual Studio](https://stackoverflow.com/q/16883037/2410359). IAC, `scanf_s()` is not safer than `scanf()`. – chux - Reinstate Monica Nov 03 '18 at 20:28