Why is 'r' and "r" different in C? Thank you already🤗 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is 'r' and "r" different in C? Thank you already🤗

10th Jun 2021, 6:14 AM
Rishi
Rishi - avatar
23 Answers
+ 3
The difference between 'r' and "r" is that the 'r' is especially quoted in a single quote which shows it is a character type data which is char. Like //Character Datatype char a = 'r'; putchar(a); //Character Array Datatype char *a = "r"; printf(a); the "r" is especially quoted in a double quote which shows it is a character Array data type that is string as we know that string is the Array of characters with null character.
9th Jul 2022, 2:33 PM
AMRIT KUMAR
AMRIT KUMAR - avatar
+ 9
First is a char and the second is a string
10th Jun 2021, 12:08 PM
Alex
+ 3
You can check this lesson: https://www.sololearn.com/learning/2912/
10th Jun 2021, 7:18 AM
Cmurio
Cmurio - avatar
+ 3
JESUS EDUARDO CANUL KOYOC I'm unable to access the link. If I agree through sololearn application, it never opens. If I do with browser, it asks me which language I wanna learn. Please share some other way to access that lesson
10th Jun 2021, 9:35 AM
Rishi
Rishi - avatar
+ 3
'r' is a single character which is written in single quotes. It can be declared as: char rchar = 'r'; Whereas "r" is a string, And it can be declared as: string rstring = "r"; Thank you...
11th Jun 2021, 7:38 AM
Akshat Sharma
+ 2
Why does "r" and 'r' work differently in fopen() function in C? In fact 'r' doesn't even work
11th Jun 2021, 4:08 AM
Rishi
Rishi - avatar
+ 2
Bot I think you misunderstood him. He said that "r" is for character array with AT LEAST 1 character (that's null character). When he said that the minimum memory demand is at least 2 bytes he's referring to this example: "r".
11th Jun 2021, 4:40 AM
Lofty
Lofty - avatar
+ 2
Bot "So" - that way he concludes that the minimum memory demand is at least 2 bytes in this example. First he gave general information and then he referred to that particular example "r". It is minimum, size of char is minimum 1 byte but it could be bigger. https://en.m.wikipedia.org/wiki/C_data_types
11th Jun 2021, 4:53 AM
Lofty
Lofty - avatar
+ 2
Dear Rishi, According to the rules of C programming language, there are string constant which is written in the double quotes and they are the sequence of characters which ends or terminate with the null character. char a[5] = {'A', 'm', 'a', 'n', '\0'}; char a[5] = "Aman"; Here, the null character is automatically added at the end. So, the rule of string constant says that always use double quotes in string. Now, discuss on the character constant. Character is a single value enclosed in 'and having respected ASCII (American standard code for information interchange) code. In character constant, we use single quote. Because it holds only one character. Therefore, 'r' is the character whereas "r" is the string. This is the distinguish between 'r' and "r". Thanks for Asking Question Have a nice day 🎉. CMDian
11th Jun 2021, 8:29 AM
AMRIT KUMAR
AMRIT KUMAR - avatar
+ 1
Rishi same here, I can't access external links, simply doesn't work. Haven't try on pc
10th Jun 2021, 10:08 PM
Lofty
Lofty - avatar
+ 1
Rishi Any single character in C enclosed with single quotes is of type <char> while anything enclosed in double quotes is of type <char_arr>. C doesn't have a string data-type. Whenever you use double quotes, a char array is created internally in the programming language. The <char> type can also be considered as a short-short-int type.
11th Jun 2021, 3:17 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Jan Markus sorry sir but a string can be of length 0, so the minimum memory demand is at p least 1 character (the null byte) : https://code.sololearn.com/cqw9CaBdDI0y/?ref=app
11th Jun 2021, 4:24 AM
Bot
Bot - avatar
+ 1
Rishi that's because fopen expects an array of chars(or string), not a char
11th Jun 2021, 4:26 AM
Bot
Bot - avatar
+ 1
Quanti sorry but he said "so" which means that he is referring to strings in general. and also if he was referring to "r" then he would not say "minimum" caz its size is exactly 2, it doesnt vary
11th Jun 2021, 4:44 AM
Bot
Bot - avatar
+ 1
Quanti oh sorry...nvm i take my words back🤖
11th Jun 2021, 5:01 AM
Bot
Bot - avatar
+ 1
Ex Char x= 'r' ;
11th Jun 2021, 8:42 PM
mohamed alkasem
mohamed alkasem - avatar
+ 1
In the given code, 'r' is a character constant whereas "r" is a string constant.
12th Jun 2021, 3:49 AM
Krishna Mohan Upadhyay
Krishna Mohan Upadhyay - avatar
+ 1
They are different because of the ' and " I don't know about c but I know that cpp and c are kind of the same ' is for char ,which is character " is for string , which you need #include <string.h> or <ioatream> in this case , use 'r' because it is char and it takes only 1 cell in storage , unlike string which takes 4
12th Jun 2021, 5:36 AM
Amir Hussain Oroujlou
Amir Hussain Oroujlou - avatar
0
Thank you all 🤗
11th Jun 2021, 8:57 AM
Rishi
Rishi - avatar
12th Jun 2021, 2:26 AM
Rishi
Rishi - avatar