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.
๐ŸŒ
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.)
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ c_standard_library โ€บ c standard library - scanf function
C Standard Library - scanf Function
August 29, 2012 - Following is the C library syntax of the scanf() function โˆ’ ... format: This is a C string that contains a format string. The format string specifies the type of input expected and is composed of conversion specifications starting with a % ...
๐ŸŒ
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 - This tells the function to read a string and store it in the variable str. However, the scanf() function stops reading a string when it encounters a white space. To read a full line of text, including white spaces, you can use the fgets() function instead.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_user_input.php
C User Input
Note that you must include the ... John Doe // Hello John Doe Run example ยป ยท Use the scanf() function to get a single word as input, and use fgets() for multiple words....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ scanf-in-c
scanf in C - GeeksforGeeks
%ld to accept input of long integers %lld to accept input of long long integers %f to accept input of real number. %c to accept input of character types. %s to accept input of a string...
Published ย  October 13, 2025
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-use-scanf-to-read-a-string
How to use scanf to read a string - Quora
Answer (1 of 4): โ€œHow do you use scanf to read a string?โ€ As Joseph Newcomer stated in his answer, it is not recommended to use scanf for *anything*, especially to read a string due to the simple fact that if one does not specify a maximum length, then there is a distinct risk of a buffer overfl...
๐ŸŒ
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 and error checking to prevent unexpected program behavior and security vulnerabilities ยท The basic syntax of the scanf() function is 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. ... format: A string ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ strings input and output functions in c
Strings Input and Output functions in C
November 16, 2023 - The scanf() function is probably the easiest way to string input in C. We just need the access specifier %s and the pointer to the variable where we want the string to be stored, and it becomes very easy to input the string in C.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
A scanf() String Trick | C For Dummies Blog
The format for desired input is specified as the functionโ€™s first argument, a string. That string typically contains a single placeholder character, such as %d for an int value. After the formatting string, scanf()โ€˜s second argument is pointer variable; the & operator is prefixed to the variable if it&#8217;s not already a pointer.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-read-data-using-scanf-in-c
How to read data using scanf() in C
The scanf() function requires the address of the variable, not the actual variable itself. 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 ...
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ how-to-take-string-input-in-c
How to take string input in C?
September 7, 2021 - It will take four words in four different character arrays and displays on the output screen. We have seen that scanf with %s or %ws can read only strings without whitespaces.
๐ŸŒ
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
We have to read a character from ... then we should read a temporary character which may available in the input buffer) I am using a statement scanf("%c",&temp); before reading the string (which is going to be read after an integer input)....
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ c-scanf-read-string
Read String using Scanf() from User in C
October 20, 2021 - In the following example, we read a string input by the user in standard input to a variable name using scanf() function. ... #include <stdio.h> int main() { char name[30]; printf("Enter your name : "); //read input from user to "name" variable ...
๐ŸŒ
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 ...
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-strings
Strings in C (With Examples)
Thus, the name in scanf() already points to the address of the first element in the string, which is why we don't need to use &. You can use the fgets() function to read a line of string.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 136165-using-scanf-function-strings.html
Using scanf function with strings
int main(void) { char strings[9][50]; // Allocate space for 9 words, up to 49 characters each (plus a '\0' terminator) int i; for(i=0;i < 9;++i) { puts("Please enter a word:"); fgets(strings[i], 50, stdin); } return 0; } One thing to remember about fgets() is that it will leave the '\n' character at the end of the string from when the user pressed ENTER (assuming the buffer size was big enough to capture the entire input). If you understand what you're doing, you're not learning anything. ... Im just starting to learn about strings. Right now I'm just trying to prompt the user to input multiple words and then use the scanf function.