Strings in C are represented as arrays of characters.

Copychar *p = "String";

You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.

Copychar p2[] = "String";

You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.

Copychar p3[5] = "String";

You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.

char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).

Reference

Answer from ob_dev on Stack Overflow
🌐
W3Schools
w3schools.com › c › c_strings.php
C Strings
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... Strings are used for storing text/characters. For example, "Hello World" is a string of characters. Unlike many other programming languages, C does not have a String type to easily create string variables.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strings-in-c
Strings in C - GeeksforGeeks
November 14, 2025 - char str[] = "Geeks"; This line declares a character array str and initializes it with the string "Geeks". Internally, this creates an array like: { 'G', 'e', 'e', 'k', 's', '\0'} The null character '\0' is automatically added at the end to ...
Discussions

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.
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. More on reddit.com
🌐 r/C_Programming
28
0
April 18, 2021
How do strings work in C
First: C has no type for strings. It does not, simply out, have strings. Print functions expect a sequence of contiguous chars with a null (0x00) at the end. The same for strings length and other “string” functions. Second: the two first sentences do the same, they declare a variable as a pointer to a sequence of chars and in the same line it assigns a constant value where each element is one of the chars of the string and add a 0 at the end. So you can do string1[4] and string2[3]. Primitive value array pointers can be seen as an array or as a pointer indistinctly More on reddit.com
🌐 r/C_Programming
46
51
October 15, 2025
[Caesar]How to declare an empty string array in C?
First, you should realize that the caesar and vigenere problems don't require you to create an encrypted string for later printing. You can print the result of encrypting each character as you go. Your declaration of c_txt is trying to create an array of i strings. What you could do is compute the length (call it p_len, for example) of the p_txt string, and then define an array whose element type is char and whose dimension is p_len + 1. The extra element is to allow for a null character ('\0') after the encrypted string. That null terminating character is needed if you intend to print the string using printf. More on reddit.com
🌐 r/cs50
2
3
April 26, 2014
(C) If I declare a string array without using malloc(), do I need to still free() it? Example below.
You only need to free() data which is dynamically allocated using something like malloc() or mmap(). If you give a constant size like you said, either the data is statically allocated by the compiler in the .data segment (this is what happens if it's global or a static local variable) or it is allocated on the stack (regular local variables). In the former case, it lasts the whole program because that's the point. In the latter case, the memory is freed automatically for you when the function returns and the local variable leaves scope. More on reddit.com
🌐 r/learnprogramming
5
4
August 30, 2018
People also ask

Can you declare a string without initializing it in C?
Yes, you can declare a string without initializing it. For example: char str[50];. This creates an uninitialized string buffer, which must be filled before use.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › c string declaration
Strings in C: How to Declare & Initialize a String Variables in C
How to initialize a string in C?
You can initialize a string in C at declaration using double quotes, like char city[] = "Delhi";. This automatically adds the null character at the end of the array.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › c string declaration
Strings in C: How to Declare & Initialize a String Variables in C
What is the difference between declaration and initialization of strings in C?
Declaration reserves memory for a string, while initialization assigns a value. You can declare with char str[10]; and initialize with strcpy(str, "Hello"); or use direct initialization like char str[] = "Hello";.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › c string declaration
Strings in C: How to Declare & Initialize a String Variables in C
🌐
Codecademy
codecademy.com › docs › strings
C | Strings | Codecademy
April 21, 2025 - The following declaration and initialization create a string of “Howdy”: char message[6] = {'H', 'o', 'w', 'd', 'y', '\0'}; ... Even though “Howdy” has only 5 characters, message has 6 characters due to the null character at the end ...
🌐
Unstop
unstop.com › home › blog › strings in c | initialization and string functions (+examples)
Strings In C | Initialization and String Functions (+Examples)
May 30, 2025 - We use the character data type to declare a string in C, and the syntax is the same as mentioned above. The process involves declaring an array of characters with a size that reflects how long you want your ...
🌐
Scaler
scaler.com › home › topics › c strings - declaring strings in c
C Strings | Declaring Strings in C - Scaler Topics
December 21, 2023 - Highlights: There are four methods of initializing a string in C: Assigning a string literal with size. Assigning a string literal without size. Assigning character by character with size. Assigning character by character without size. Character arrays cannot be assigned a string literal with the '=' operator once they have been declared. This will cause a compilation error since the assignment operations are not supported once strings are declared. To overcome this, we can use the following two methods:
Find elsewhere
🌐
Quora
quora.com › How-do-you-declare-a-string-variable-in-C
How to declare a string variable in C - Quora
Answer (1 of 3): Strictly speaking there is no native string type in C. C developers decided to manage strings as an array of chars ended with a 0. Now, here is the thing, there is a big difference between a string variable and a constant string. That difference is called reserved space. I wil...
🌐
Programiz
programiz.com › c-programming › c-strings
Strings in C (With Examples)
In this tutorial, you'll learn about strings in C programming. You'll learn to declare them, initialize them and use them for various I/O operations with the help of examples.
🌐
Quora
quora.com › How-do-I-declare-a-string-in-C
How to declare a string in C++ - Quora
Answer (1 of 2): [code]#include int main() { //Declare a string: std::string x; //... return 0; } [/code]
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › c string declaration
Strings in C: How to Declare & Initialize a String Variables in C
July 15, 2025 - Learn how to declare and initialize strings in C. Explore syntax, input/output functions, pointer usage, and manipulation with functions like strlen().
🌐
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.
🌐
LabEx
labex.io › questions › how-to-declare-an-array-of-strings-in-c-136081
How to Declare an Array of Strings in C | LabEx
July 25, 2024 - ## Declaring an Array of Strings in C In the C programming language, you can declare an array of strings by creating an array of character pointers.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › strings
Strings in C Programming (Define, Declare, Initialize, Examples)
August 29, 2025 - Learn how to define, declare, and initialize strings in C programming with clear examples. Understand the basics of working with strings in C. Read now!
🌐
Reddit
reddit.com › r/c_programming › how do strings work in c
r/C_Programming on Reddit: How do strings work in C
October 15, 2025 -

There are multiple ways to create a string in C:

char* string1 = "hi";
char string2[] = "world";
printf("%s %s", string1, string2)

I have a lot of problems with this:

According to my understanding of [[Pointers]], string1 is a pointer and we're passing it to [[printf]] which expects actual values not references.

if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine

I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character

this doesn't work, because we are assigning a value to a pointer:

int* a;
a = 8

so why does this work:

char* str;
str = "hi"
Top answer
1 of 30
65
First: C has no type for strings. It does not, simply out, have strings. Print functions expect a sequence of contiguous chars with a null (0x00) at the end. The same for strings length and other “string” functions. Second: the two first sentences do the same, they declare a variable as a pointer to a sequence of chars and in the same line it assigns a constant value where each element is one of the chars of the string and add a 0 at the end. So you can do string1[4] and string2[3]. Primitive value array pointers can be seen as an array or as a pointer indistinctly
2 of 30
17
if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine Arrays decay to a pointer to their first element when passed as arguments to functions. I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character You increment the pointer to point to the index within the string. If string1 points to "hello", then string1 + 1 or ++string1 points to "ello". Alternatively, you can use &string[1] - the address of character 1 in the string (0-indexed). The subscripting syntax, [] for arrays is really just pointer arithmetic. this doesn't work, because we are assigning a value to a pointer: Because 8 is not stored anywhere in memory, it's just a constant. In this case you're setting the pointer to address 0x00000008 in memory, which is almost definitely not what you want. Normally you would want to say *a = 8 to set the value at the address of a to 8 - but you first need to allocate some memory to write to. so why does this work: Because string literals do have a location in memory - in the .text or .data section of the program. When you assign str = "hi", the compiler encodes "hi" into the compiled executable, and when the process runs this section gets loaded into memory. str then points to this location - which could either be a fixed location or a section-relative location.
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Strings.html
C/Strings
The reason is that many common string-processing tasks in C can be done very quickly by hand. For example, suppose we want to copy a string from one buffer to another. The library function strcpy declared in string.h will do this for us (and is usually the right thing to use), but if it didn't ...
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c strings in c programming
C Strings in C Programming
June 10, 2012 - Let us create a string "Hello". It comprises five char values. In C, the literal representation of a char type uses single quote symbols such as 'H'. These five alphabets put inside single quotes, followed by a null character represented by '\0' are assigned to an array of char types. The size of the array is five characters plus the null character six. char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; C lets you initialize an array without declaring the size, in which case the compiler automatically determines the array size.
🌐
W3Schools
w3schools.com › cpp › cpp_strings_cstyle.asp
C++ C-Style Strings
The name comes from the C language, which, unlike many other programming languages, does not have a string type for easily creating string variables. Instead, you must use the char type and create an array of characters to make a "string" in C.
🌐
Medium
medium.com › @muirujackson › char-pointer-to-string-in-c-aa4b59fdf289
Declaration of String in C. In C, char [] and char * are both used… | by Muiru Jackson | Medium
April 6, 2023 - In summary, char [] and char * are both used to represent strings in C, but they have different memory allocation, size, mutability, initialization, and function parameter passing behavior.
🌐
freeCodeCamp
freecodecamp.org › news › c-string-how-to-declare-strings-in-the-c-programming-language
C String – How to Declare Strings in the C Programming Language
October 6, 2021 - As you see, there is no built-in string or str (short for string) data type. From those types you just saw, the only way to use and present characters in C is by using the char data type.