Read Strngs with spaces

Hello, i'm using C and i don't ever programed in C++. Now i'm doing a program that needs to read strings with spaces but when i use "scanf" funcion it cuts the scan at the fist space. I can do it with C or I need C++?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string.h>
#include <stdio.h>

int main()
{ 
    {
        enum { MAXSZ = 1024 } ;
        char input[MAXSZ] = "" ;
        
        puts( "please enter a string with spaces in it:" ) ;
        
        // http://www.cplusplus.com/reference/cstdio/fgets/
        if( fgets( input, MAXSZ, stdin ) )
        {
            // knock off the newline character appended at the end
            size_t len = strlen(input) ;
            input[len-1] = 0 ;
            
            printf( "you entered: '%s'\n", input ) ;
        }
    }
}

http://coliru.stacked-crooked.com/a/701e42bc337d05f3
Topic archived. No new replies allowed.