You have to make four changes:

  1. Change

    char * str[25];
    

    to

    char str[25];
    

    as you want an array of 25 chars, not an array of 25 pointers to char.

  2. Change

    char car;
    

    to

    int car;
    

    as getchar() returns an int, not a char.

  3. Change

    scanf("%[^\n]s", &str);
    

    to

    scanf( "%24[^\n]", str);
    

    which tells scanf to

    1. Ignore all whitespace characters, if any.
    2. Scan a maximum of 24 characters (+1 for the Nul-terminator '\0') or until a \n and store it in str.
  4. Change

    printf("\nThe sentence is %s, and the character is %s\n", str, car);
    

    to

    printf("\nThe sentence is %s, and the character is %c\n", str, car);
    

    as the correct format specifier for a char is %c, not %s.

Answer from Spikatrix on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ c_standard_library โ€บ c standard library - scanf function
C Standard Library - scanf Function
August 29, 2012 - The C library function scanf(const char *format, ...) reads formatted input from standard input stream (typically the keyboard). Following is the C library syntax of the scanf() function โˆ’ ... format: This is a C string that contains a format ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ scanf-in-c
scanf in C - GeeksforGeeks
In C, scanf() is a standard input function used to read formatted data from the standard input stream (stdin), which is usually the keyboard.
Published ย  October 13, 2025
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstdio โ€บ scanf
scanf
Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type. There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function. These arguments are expected to be pointers: to store the result of a scanf operation on a regular variable, its name should be preceded by the reference operator (&) (see example).

You have to make four changes:

  1. Change

    char * str[25];
    

    to

    char str[25];
    

    as you want an array of 25 chars, not an array of 25 pointers to char.

  2. Change

    char car;
    

    to

    int car;
    

    as getchar() returns an int, not a char.

  3. Change

    scanf("%[^\n]s", &str);
    

    to

    scanf( "%24[^\n]", str);
    

    which tells scanf to

    1. Ignore all whitespace characters, if any.
    2. Scan a maximum of 24 characters (+1 for the Nul-terminator '\0') or until a \n and store it in str.
  4. Change

    printf("\nThe sentence is %s, and the character is %s\n", str, car);
    

    to

    printf("\nThe sentence is %s, and the character is %c\n", str, car);
    

    as the correct format specifier for a char is %c, not %s.

Answer from Spikatrix on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ can someone please tell me how to declare a string in c and read them with scanf()? tutorials point/geekforgeeks/other internet resources are total garbage and hard to follow for beginners.
r/C_Programming on Reddit: Can someone please tell me how to declare a string in C and read them with scanf()? Tutorials point/geekforgeeks/other internet resources are total garbage and hard to follow for beginners.
April 18, 2021 -

Hi,

So tutorialspoint, geeksforgeek, javatpoint, those websites that claim to have C/C++ tutorials are a bunch of shitty Indian garbage tutorials that are not suited for beginners at all. I'm trying to learn how to declare a string variable with C and read input with scanf() and I get a bunch of different information from all three sites and I'm really frustrated and confused. Do you do?

#include <stdio.h>  
int main(void)
{
    char name[100000000];
    printf("What's your name?\n");
    scanf("%c", char);
}

Or is it

#inclue <stdio.h>

int main(void)
{
    char** x;
    printf("What's your name?\n");
    scanf("%c", x)
}

Or is it

#include <stdio.h>

int main(void)
{
    string x;
    printf("What's your name?\n");
    scacnf("%s", x);
}

Or is it

#include <stdio.h>

int main(void)
{
    char **name;
    printf("What's your name?\n");
    scanf("%c", name)

What the fuck is it? Is it %c or %s for the format code, and it is char name[] or char **var_name or **char var_name?? Someone please tell me. All these online tutorials are fucking GARBAGE and I can't find a clear answer anywehere

Top answer
1 of 3
3
K&R is not intended to be a complete manual for every function in the C library: that's what man pages are for. K&R is a tutorial that introduces the C language (that is, the things that compilers deal with -- keywords, data types, syntax, ...), and shows some ways they can be connected together to make working programs. There are countless libraries that C can call: X-windows graphic libraries, and encryption libraries, and database access libraries, and networking libraries. They all come with their own manual pages. If the C Language book covered all those, then (a) you would not be able to lift it, and (b) it would be permanently out of date, as things evolve. My K&R is a First Edition from 1978, so the page numbering is not going to be the same as yours -- I'm not sure the section numbering will even be the same. That's why you use your own volume's index. Section 7.4 "Formatted Input" in mine covers scanf, 7.5 for sscanf, and 7.6 for fscanf. They are very similar, and use the same format "conversion specification" options: scanf reads from stdin, fscanf reads from a file, and sscanf reads from a string in memory. They work best for input where you are not too fussed about how it fits into input lines, so they are quite sloppy about whitespace. getline appears in section 1.9, as an example of how to write a function in C, and again in section 4.1 which is about how to structure a larger program. At the time, there was no standard library function getline(). When this was added into a library later, it was made to do the same kind of thing, but allows for expanding the input length automatically. So getline() reads a whole line from a file (which can be stdin or any other file or device), but it does not unpack any fields or values itself: there are a whole family of other functions that deal with those things. Some of those functions have names like strtok, strtol, strtod, and they all have man pages. Usually, there is a "See Also" section that links you into similar or related functions. fgets() is in section 7.8 "Line Input and Output" along with fputs(). fgets() is like getline, but simpler: you provide space for the string, but if it is too short you don't get a whole line of data, and you have to deal with that problem yourself. gets() is like fgets() but for stdin only. Also, it does not care at all about the space you provided for the input, and so can be misused to corrupt your program. The first four words in the man page are "Never use this function". All the code sections you posted are wrong, and here's why. char name[100000000]; reserves a buffer of 100 million characters as a local (on-stack) variable. That will use up all your stack and more besides, and your program won't run (probably not even load). char *x; would create a pointer to some chars, but it is not initialised to point to anywhere -- it is a pointer to something that does not exist. char **x; is even worse: is is a pointer to a pointer that does not exist, that would point to some chars that don't exist either. string x; uses a type "string" that does not exist in C. Some CS50 courses provide a header file that fixes up stuff like "string" to make your code easier to write, but they are not proper C and they hide the actual details, which is a piss-poor way to learn. scanf ("%c") reads exactly one character from the input. That is not a string. scanf ("%s") reads a string (like a name) from the input. It reads a real word: it skips any preceding whitespace, and it then only fetches text up to the next whitespace. Also, second example says #inclue and misses a ; off the scanf line, third one says scacnf, fourth one misses ; off scanf and a closing } . Compilers don't allow for carelessness. "string" is not part of the C language. It is verbal shorthand for "A row of text characters that ends with a zero (ASCII NUL) character. A row of things in C is usually called an array. To read and show a name in C, this would be good: #include int main(void) { char name [84]; //.. Allowing space for NUL terminator. printf("What's your name?\n"); scanf("%80s", name); //.. Limit field width on read. printf ("Good to meet you, %s!\n", name); } OK, I've spent 75 minutes writing this up. You need to do some work now. K&R is quite readable -- worth skimming the whole thing just to get an idea of what is in there -- get the shape of the language and fill in the detail later. Those dumb tutorials you found are mainly written for vanity by people who don't understand C themselves. 80% of it is junk. It really is worth reading enough man pages to understand why the information is presented like they do.
2 of 3
3
You should DM u/JacksonSteel -- you are brothers under the skin.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Scanf
scanf - Wikipedia
December 3, 2025 - scanf, short for scan formatted, is a C standard library function that reads and parses text from standard input. The function accepts a format string parameter that specifies the layout of input text. The function parses input text and loads values into variables based on data type.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1746871 โ€บ how-to-input-a-string-using-scanf-in-c-
How to input a string using scanf() in C ? | Sololearn: Learn to code for FREE!
1- Declare your string variable. 2- In scanf(), use %s as your format specifier. 3- When assigning the format specifier to a variable, don't use '&' (according to my experience, adding & never worked.)
Find elsewhere
๐ŸŒ
Sternum IoT
sternumiot.com โ€บ home โ€บ scanf c function โ€“ syntax, examples, and security best practices
scanf C Function โ€“ Syntax, Examples, and Security Best Practices | Sternum IoT
January 31, 2024 - The scanf() function then reads the number and stores it in the variable num. The program then prints the number back to the user. ... The gets() function reads a line from stdin into the buffer pointed to by str, until it reaches either a ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ using-scanf-in-c
How to Use scanf( ) in C to Read and Store User Input
March 6, 2023 - The importance of input validation ... as follows: ... The scanf() function returns the number of items successfully read, or EOF if an error occurs or the end of the input stream is reached....
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-strings
Strings in C (With Examples)
#include <stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; } ... Enter name: Dennis Ritchie Your name is Dennis. Even though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the name string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ taking-string-input-space-c-3-different-methods
Taking String input with space in C (4 Different Methods) - GeeksforGeeks
July 23, 2025 - We can take string input in C using scanf("%s", str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c โ€บ c-program-to-read-string-with-spaces-using-scanf-function.aspx
C program to read string with spaces using scanf() function
In both cases variable name stored only "Alex"; so, this is clear if we read a string by using "%s" format specifier, string will be terminated when white space found. The format specifier "%[^\n]" tells to the compiler that read the characters until "\n" is not found. ... #include <stdio.h> int main() { char name[30]; printf("Enter name: "); scanf("%[^\n]",name); printf("Name is: %s\n",name); return 0; }
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
A scanf() String Trick | C For Dummies Blog
Iโ€™m not a fan of the scanf() function, despite how handy it can be. Itโ€™s a great teaching tool, but for a program that accepts string input, you need to use fgets() instead. Still, scanf() can be used to read a string, providing that you know the whitespace requirements beforehand.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-read-a-string-into-an-array-using-scanf-in-the-C-programming-language
How to read a string into an array using scanf in the C programming language - Quora
Answer (1 of 3): You can do something like this: [code]char buf[16]; scanf("%s", buf); [/code]But this will trample the stack if you enter more than 15 characters. For example: [code]#include int main(int argc, char **argv) { char buf[16]; int ...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_stdio_scanf.php
C stdio scanf() Function
The format parameter is a string that describes the format of the data that is expected. If the user input does not match the format then the function stops reading at the point where the first mismatch occurs.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ string input question - scanf, fgets or gets
r/C_Programming on Reddit: String input question - scanf, fgets or gets
December 6, 2023 -

I've seen many forms to get a string input from the user, and wanted to know the difference between them, which is the best, or if it's just about preference.

In my computer science classes the teacher told us to read string using scanf(), like so:

scanf("\n%[^\n]", string);

But I've seen people using the fgets(), like so:

fgets(string, size, stdin);

I've also seen people talking about the gets() function, but in a negative light.

Is there a better option? or is it down to preference? what's the difference between them?

๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-read-data-using-scanf-in-c
How to read data using scanf() in C
This is done to store the value at the memory location of the variable. ... Note: In C, a string is the address of the character buffer which is why we do not need & with the variable.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ scanf
scanf() in C Language (Syntax, Examples, How to Use?)
August 29, 2025 - Understand what scanf() is in C Language, its syntax, and how to use it effectively with examples. Learn to read user input and improve your C programming.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Using scanf() to Build a String โ€“ Part II | C For Dummies Blog
Trying to salve my frustration with the scanf() function, I decided in last weekโ€™s Lesson to try to use scanf() to build a string. Because the function terminates standard input at the first whitespace character (space, tab, newline), the strings input must be stored and the string built ...