Strings in C are represented as arrays of characters.

char *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.

char 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.

char 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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strings-in-c
Strings in C - GeeksforGeeks
3 weeks ago - 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
New to C. If C does not have strings, then what exactly is printf doing?
C does have strings. It doesn't have a "string data type", but it has strings. A string object in C is not a string because the type system says it's a string, it's a string because of the nature of the bytes that make up the object. Specifically, a C string is "a contiguous sequence of characters terminated by and including the first null character". That definition makes no reference to types at all. This might sound a bit pedantic, but it's actually pretty important. Let's say you have an object declared as follows: char str[100]; Does this object identify a string or not? We cannot answer this unless we know the value being stored in the object. The value might be a string, or it might not be. The type system does not tell us. Somebody just asked me "what is a character in C"... but then they deleted the question. I suspect it was going to lead on to "isn't char a data type?" Yes, char is a data type. But in C a character is given the somewhat more abstract definition "member of a set of elements used for the organization, control, or representation of data", as well as the practical definition of a value stored in a single byte. Essentially I see the notion of a character as being a description of a value, not the type of that value. Characters are often stored in char objects, but they can also be stored in other type objects, like unsigned char and int. For the "character as an element of a string" sense, however, one must think of these characters as being in contiguous bytes in memory. One might typically use a char pointer or an array of char to denote such an object, since they allow you to directly address each character in the string individually. A void pointer would not let you do this so easily, for instance, even though it could just as well point to the first character of a string. More on reddit.com
🌐 r/C_Programming
34
66
January 26, 2022
Does c have strings
You’re just having an argument over what the definition of “string” is. Your argument has nothing to do with C. It’s what people are talking about when they say “arguing over semantics”. These arguments, where you both agree about what the truth is (you agree how C works), but you disagree over the semantics of the words you use to describe C (you disagree about what a “string” is). The C standard is not going to decide this argument for you. Neither is the dictionary. More on reddit.com
🌐 r/cprogramming
57
10
November 3, 2024
People also ask

What is a string in C?
A string in C is a collection of characters stored in a character array and ended with a special null character ('\0') that indicates where the string finishes.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › strings
Strings in C Programming (Define, Declare, Initialize, Examples)
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
🌐
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().
🌐
W3Schools
w3schools.com › c › c_strings.php
C Strings
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... 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.
🌐
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:
🌐
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 ...
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › strings
Strings in C Programming (Define, Declare, Initialize, Examples)
March 23, 2026 - Learn how to define, declare, and initialize strings in C programming with clear examples. Understand the basics of working with strings in this tutorial.
🌐
Tutorial Gateway
tutorialgateway.org › c-string
C String
October 10, 2018 - The syntax of a string declaration in C Programming is as follows. For Example, char full_name[50];. Here, full_name is the name, and the size equals 50. It means this allows a maximum of 49 characters.
🌐
Quora
quora.com › How-do-you-declare-a-string-variable-in-C
How to declare a string variable in C - Quora
In c language you have to use “char” keyword as there is no “string” keyword word in it. ... In c it is prefferable to declare a string like in Ex 2 as there is less memory waste because memory is allocated at run time.
🌐
Guru99
guru99.com › home › c programming › strings in c: how to declare & initialize a string variables in c
Strings in C: How to Declare & Initialize a String Variables in C
August 8, 2024 - char first_name[15] = "ANTHONY"; char first_name[15] = {'A','N','T','H','O','N','Y','\0'}; // NULL character '\0' is required at end in this declaration char string1 [6] = "hello";/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */ char string2 [ ] = "world"; /* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */ char string3[6] = {'h', 'e', 'l', 'l', 'o', '\0'} ; /*Declaration as set of characters ,Size 6*/ In string3, the NULL character must be added explicitly, and the characters are enclosed in single quotation marks. ‘C’ also allows us to initialize a string variable without defining the size of the character array.
🌐
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]
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_strings.htm
Strings in C
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.
🌐
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.
🌐
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.
🌐
Systems Encyclopedia
systems-encyclopedia.cs.illinois.edu › articles › c-strings
Strings in C - Systems Encyclopedia
While C does allow string literals, strings in C are strictly represented as character arrays terminated with a null byte (\0 or NUL). ... special case of character arrays; not all character arrays are considered strings, but any contiguous ...
🌐
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.