02:43 Strings are not a data type in C programming, but they can be represented using character arrays. 05:26 A string in C is an array of characters terminated by a null character. 08:09 C programming uses character arrays to represent strings. 10:52 In C programming, strings are null-terminated character arrays. 13:35 Using arrays to model strings in C and printing them using %s or putsstr 16:18 Explanation of character arrays in C 19:01 Understanding string handling in C 21:37 Different ways to print strings in C
str[5] can only store 5 characters. The strinng (or character array) "hello" has 5 characters so there's no space for the null character(\0). For those who are wondering that by using double quotes ("") the compiler will automatically add a null character(\0) should remember that altough it's true but the compiler will need an extra space in that array to store the null character(\0) which is limited in the character array str. Hope I am right...
The array str is declared with a size of 5 (char str[5];), which means it can store only 4 characters plus the null terminator ('\0'). However, the string "hello" has 5 characters.
Please note that the gets function is considered unsafe and has been deprecated in newer versions of the C standard due to potential buffer overflow vulnerabilities. SO better use fgets( str , sizeof(str), stdin);
@@harshitsingh2101 Yes ! You are right I was wrong that time. Thank you I thought earlier it will give error because of null character but the real reason is assignment operator.
@@rohitgiri9179 null character will still be needed even if we use double quotes and so space to allocate it should also be there. It's just that we won't need to write \0 at the end. Double quotes by default takes a \0 at the end.
@@soniamalik4929 kyonki usme humne string ka size define kiya hai 5 ,agar vo 5 voha na hota to vaise hi hota jaise tum bata rahi ho compiler bhi automatically lagata hai \0 but uske liye bhi space honi chaiye na isliye
@@vikassaini7485 Yes, bro I just checked. If you write str[5], It's actually not giving an error but instead of hello some unusual letters are getting appended with "hello". But str[6] gives no errors and unsatisfied results.
@@soniamalik4929 yup compiler do this own. But tell me to stay you require a room space ri8, is it matters whether you created the room or the builder created for u?????///
str[5] is an array that can store upto 5 characters only. h e l l o is 5 characters, since the 6th character is a null character the array size has t be 6. Hence, str[6] should be used for storing h e l l o.
char str[5]; str="hello"; str is a constant pointer to str[0] and its value is not modifiable; if str[0] is modified first then this constant pointer can be modified
@@yashrawat747 Galat hai bhai, dekho jo str hai wo yaha par array pointer ki terah kaam kar raha hai agar tumne str = "hello" likha matlab tum str[0] ke address ko equal kar rahe ho "hello" ke toh compiler tuhme wo karne nhi deta instead do char *str[(this value doesn't matter)]; *str = "hello";
Star[5] liya gaya hai but hame star[6] lena chahiye q ki hello ke sath null character ko bhi count karte hai Correct form char str [6]; str = "hello"; Esa hona chahiye
The str[5] represents an array which only stores 5 character and hence to make it a proper string we have to increase the length by 1 to store the null value too. That will make it a perfect string and hence then it will print harry and run without creating an issue
Str[5] holds 5 character which is Harry. Although it requires proper size for storing value. Like above example normally string array takes extra size,We need to intalize array (string length + null character),it will tell you string end and does not take any blank value.
As "hello"={'h', 'e', 'l', 'l', 'o', '\0'} exceeds the stack by 1 character and by definition given above is char srt[5], of strength 5. That's why the compiler will through error.
Quiz solution: Since the array length = string length , therefore, the compiler will get no space to add ''\0' in the end and as a result it will not be considered as an string.
The correct answer to your question is that we cant assign a string to the base adress of an array. and in the given question "arr" represents the base address of an array.
Char str [5]; Str="hallo"; Bas ek kaam karna hai 5 ki jagah hume 6 likh dena hai null.\0 ke liye Hame apne charcter se space ko chhota nahi likhna chaho to bda kitna bhi likh sakte ho magar null \0 ke liye jagah deni padegi
Firstly, ANSWER of your question is ( because when we right any word in double-quotes that time compiler automatically applies the NULL character ( '\0' ) ; so, above your line is never create any issues. Ok ! Then 2nd thing is , when you explaining the "printstr" function that time you say " i will tell you ki why iam used %c in printf . But after sometime you forgot that...🙄🙁 But its okay. 🤩🙌👌👍 You are AWESOME guy.. No doubt..👍👍
char str[5]; str = "hello"; This will create problem because we have length of hello word 5 and in str we can store only 5 character but for string we need length of character+1 in array created
9:40 char str[5] can store 5 elements only. And str="hello" has 5 characters but we can't store the null character('\0') within str[5]. Thus it is not a valid string.
complete explanation: As when compiler convert "hello" into {'h' , 'e', 'l', 'l',' o', '\0, } and when it stores in array[5] , then the compiler can't do so bcoz this array is of length- 5 only .
Char str[6] hona chahiye valid output ke liye Char str[5] sirf 5 length ka word print karega Aur valid output ke liye len + 1 hona bahut jyada jaruri hai
the compiler adds the null character by itself as we have declared it as a string hence the size would imply that 4 characters for the string and the last character will be reserved for the '\0'. Therefore, we should always give one extra size while declaring the string variable.
Quick quiz answer: Even if the compiler adds a \0 automatically due to presence of double quotes but the array here doesn't have space to accommodate the null character hence it will create problem.
array can store 6 characters because indexing starts of 0. 0 to 5 total 6 numbers are there. The real problem is that str stores the base address of the array.We cannot store string in that.
in double quotes( " " ) compiler will add a null character to variable hence it will need an extra space is required. Due to this reason line will create an problem. So the size of the string should be at least { length + 1}
str[5] is valid for the string length of 4 character string because of one null character not for 5 string character. For 5 string character we need str[6]
9:40 length of string will be 6 due to 1 null character /0 and other 5 character of hello and instead of that only 5 is issued so that will create problems.
Timestamp - 9:40 //Quick Quiz The character array str[5] cannot store the string "hello" because the array's size and the string are the same and there is no extra space for the null character to be stored in the character array. Thus, this would create a problem.
9:38 Bcoz sir array size is 5 and bcoz we have put double quotation therefore there's no space for it. Hence the string will be printed with garbage values ..So the space should be increased .
Sir as you said and we all know element of an array started from 0 so char str[5] is correct according to me cause it has 6 spaces so we can include '\0' too in that if i write "harry". I'm confused sir, coreect me if im wrong.
When we declare an array with any index number then its mean its locations is one less of it For example str[5] have 0,1,2,3,4 locations so we have to declare another location for null
Because str[5], can store only 5 character not the extra null character (that is - length of array should be one greater than the element's characters value)
at 9:49 question the problem was invent because of we can allocate only 5 characters memory and whereas we need as 6 characters so we can repalce arr[6] so problem was solved😁
str is a pointer to the base element of the char array str [ ] , so we cannot store "hello" in it because it is not a char array. Besides , the value of str is not modifiable.
At 9:40 the length of the string has to be equal to 6. Because we have to store a null character which will require extra space of one byte along with characters of the string.
str[5] = "hello" is wrong because if we write it, It is a user defined string number which is str[5] = "hello" in this case so their is no space for null character in this array of string, So in order to make it write there are 3 ways First str[] = "hello"; Second str[6] = "hello"; third str[6] = { 'h', 'e' , 'l' , 'l' , 'o' }
str = "hello" consists of 5 characters and when it's checked by the C compiler gcc then it adds a null character in the end and so the size is 6 but it is initialized with char str[5]; so the second line will throw error as there's no problem in initializing the array but the index is overlapping the declared size of the character array
// Online C compiler to run C program online #include int main(){ char malik[5]="Atif "; printf("By using printf "); printf("%s ",malik); char anothermalik[10]; printf("Enter another name : "); gets(anothermalik); printf("By using gets and puts : "); puts(anothermalik); }
The word "hello" will create problem because it is a 5-letter word. In the code we have assigned the maximum value that the array can accommodate is up to 5. But while using string we have to denote '/0' in the array code to signify that the string ends here and not to print the garbage value further. So, array required to accommodate given string has to be number of characters in string +1 which is used to denote null character. Therefore, in the code shown, after accommodating the 5 characters of "hello" in the array, there will be no room for denoting null character which will create the problem in the code.
String 5 can store only 5 digits and the "hello" itself has 5 digits and because of the semicolon it will also add \o(null character) but there is no space to store the \o character.ok
As we need 1 extra space for null character to make it valid string.When we want Hello to print it needs six unit spaces as null character must be include for valid string.
02:43 Strings are not a data type in C programming, but they can be represented using character arrays.
05:26 A string in C is an array of characters terminated by a null character.
08:09 C programming uses character arrays to represent strings.
10:52 In C programming, strings are null-terminated character arrays.
13:35 Using arrays to model strings in C and printing them using %s or putsstr
16:18 Explanation of character arrays in C
19:01 Understanding string handling in C
21:37 Different ways to print strings in C
str[5] can only store 5 characters. The strinng (or character array) "hello" has 5 characters so there's no space for the null character(\0).
For those who are wondering that by using double quotes ("") the compiler will automatically add a null character(\0) should remember that altough it's true but the compiler will need an extra space in that array to store the null character(\0) which is limited in the character array str.
Hope I am right...
The array str is declared with a size of 5 (char str[5];), which means it can store only 4 characters plus the null terminator ('\0'). However, the string "hello" has 5 characters.
yes Right Answer 🙂🙂🙂🙂🙂🙂
Right
Please note that the gets function is considered unsafe and has been deprecated in newer versions of the C standard due to potential buffer overflow vulnerabilities. SO better use fgets( str , sizeof(str), stdin);
(edited)
9:24 Because it's a C program and you can't assign a string into an array of characters like this.
str = "hello"; ❌
str[6] = "hello"; ✔️
No you are very wrong dear
@@harshitsingh2101 Yes ! You are right I was wrong that time. Thank you
I thought earlier it will give error because of null character but the real reason is assignment operator.
You are wrong....if array size 6 then it is correct
@@akn3021
Brother, index starts from 0
Still u are wrong 😂
Str[5 ] can store only 5 characters while the string ''hello'' itself has 5 character. hence , there is no space for \0(null character) .
O(n+1)
no bro if we are using double quote("") then no need of null character
Bhai aapka knowledge toh kamaal ka hai😂
@@rohitgiri9179 null character will still be needed even if we use double quotes and so space to allocate it should also be there. It's just that we won't need to write \0 at the end. Double quotes by default takes a \0 at the end.
@@AniketSingh-be5gk 🤣🤣
str[5],str="hello" we are unable to store this "hello" into str[5] array. the reason is "\0" we are not adding null character into this array.
But...harry ne bola tha ki "hello" wale method m compiler khud hi create krega null character?plz answer
@@soniamalik4929 kyonki usme humne string ka size define kiya hai 5 ,agar vo 5 voha na hota to vaise hi hota jaise tum bata rahi ho compiler bhi automatically lagata hai \0 but uske liye bhi space honi chaiye na isliye
@@liltekos4498 Have you run this code on your computer?
This code is showing me error instead of writing str[6]
@@vikassaini7485 Yes, bro I just checked. If you write str[5], It's actually not giving an error but instead of hello some unusual letters are getting appended with "hello". But str[6] gives no errors and unsatisfied results.
@@soniamalik4929 yup compiler do this own. But tell me to stay you require a room space ri8, is it matters whether you created the room or the builder created for u?????///
How could someone be this much nice..I mean teaching for free all the concepts and that too in understanble form...applaudssss🎉
str[5] is an array that can store upto 5 characters only. h e l l o is 5 characters, since the 6th character is a null character the array size has t be 6. Hence, str[6] should be used for storing h e l l o.
The str[5] is a character array that can store 5 elements, but to store 'harry' 6 characters are required.
Because of null character
@@bijendraagrawal2411 '\0'..
hello not harry😅
@@aryanagarwal7903 🤣🤣🤣🤣🤣🤣
@@aryanagarwal7903 baat toh smjh gyaa naa
9:46
Because in this type of syntax the compiler automatically takes the \0 which requires an extra memory location so that's why it should be str[6]
char str[5];
str="hello";
str is a constant pointer to str[0] and its value is not modifiable; if str[0] is modified first then this constant pointer can be modified
bro can u pls explain this further?
@@sirkartik bhai hello 5 characters se bna hh
abb hmey ek aur space chaiye h jhan pr hum \0 issey store kr payen
@@yashrawat747 Thanks :) for replying bro
@@yashrawat747 Galat hai bhai, dekho jo str hai wo yaha par array pointer ki terah kaam kar raha hai agar tumne str = "hello" likha matlab tum str[0] ke address ko equal kar rahe ho "hello" ke toh compiler tuhme wo karne nhi deta instead do
char *str[(this value doesn't matter)];
*str = "hello";
The size of array of string should be greater than 1 from actual size of array to store null character (\0) ie. Ln+1
Star[5] liya gaya hai but hame star[6] lena chahiye q ki hello ke sath null character ko bhi count karte hai
Correct form
char str [6];
str = "hello";
Esa hona chahiye
The str[5] represents an array which only stores 5 character and hence to make it a proper string we have to increase the length by 1 to store the null value too. That will make it a perfect string and hence then it will print harry and run without creating an issue
Char str[5]="hello"; ✓
Str = "hello"; ×
*You should initialize the value with data type*
This is also wrong
9:50 str[5] can only store 5 char that is h,e,l,l,o so there is no place left for null.
So we have to give str[6]
Same doubt
Harry wrote str[5] instead of std[ 6]
@@suhanigupta.3120so if you do it str[6] it will run as there will be 1 place for /0
This is the right way to insert
#include
int main()
{
char str[] = "Hello";
printf("%s", str);
return 0;
}
Quiz answer : Because there is no space to accomodate the null character.
Str[5] holds 5 character which is Harry. Although it requires proper size for storing value. Like above example normally string array takes extra size,We need to intalize array (string length + null character),it will tell you string end and does not take any blank value.
As "hello"={'h', 'e', 'l', 'l', 'o', '\0'} exceeds the stack by 1 character and by definition given above is char srt[5], of strength 5. That's why the compiler will through error.
char str[5];
str ="harry"; we cant assign an array like this coz its a pointer and that is constant always
Harry bro Mene aap ka 15 hours ka c_language ka pura video dekha he
Esh tutorial me aap bohot fast pass rhe ho
Quiz solution:
Since the array length = string length , therefore, the compiler will get no space to add ''\0' in the end and as a result it will not be considered as an string.
Have tried many videos but your ones are short and simple and to the point. Thank you for making such a quality content harry bhai
The correct answer to your question is that we cant assign a string to the base adress of an array. and in the given question "arr" represents the base address of an array.
because \0 mandatory h & uske liye length 6 chahiye
#include
int main()
{
char str[6] = "PAVAN";
for(int i=0; i
Char str [5];
Str="hallo";
Bas ek kaam karna hai 5 ki jagah hume 6 likh dena hai null.\0 ke liye
Hame apne charcter se space ko chhota nahi likhna chaho to bda kitna bhi likh sakte ho magar null \0 ke liye jagah deni padegi
Firstly, ANSWER of your question is ( because when we right any word in double-quotes that time compiler automatically applies the NULL character ( '\0' ) ; so, above your line is never create any issues. Ok !
Then 2nd thing is , when you explaining the "printstr" function that time you say " i will tell you ki why iam used %c in printf .
But after sometime you forgot that...🙄🙁
But its okay. 🤩🙌👌👍
You are AWESOME guy..
No doubt..👍👍
9:45 initialization and declaration must happen in the same line for strings.
hamare pas '\0' store kane kae liye jagha hi nahi hai or jab tak \0nahi ata to string terminate nahi hongi
char str[5];
str = "hello";
This will create problem because we have length of hello word 5 and in str we can store only 5 character but for string we need length of character+1 in array created
9:40 char str[5] can store 5 elements only. And str="hello" has 5 characters but we can't store the null character('\0') within str[5]. Thus it is not a valid string.
complete explanation:
As when compiler convert "hello" into {'h' , 'e', 'l', 'l',' o', '\0, } and when it stores in array[5] , then the compiler can't do so bcoz this array is of length- 5 only .
9:41 This will create a problem because 'hello' will occupy all the space leaving none for the null '\0' character
Char str[6] hona chahiye valid output ke liye
Char str[5] sirf 5 length ka word print karega
Aur valid output ke liye len + 1 hona bahut jyada jaruri hai
str[5] initialise from 0 to 4, which only can store upto 5 char so null char cann't be added by compiler.
Length of array+1
But there char str[5];
str="hello";
hello is 5 character
Null character is needed to terminate
Good bro
"String ek data type mene nahi bataya or hota bhi nahi hai" that initial line is amazing to start
Outstanding way to teach 🥰👏👏
this lecture really help me a lot bhaiya to learn about strings very clearly. thanks a lot bhaiya for your videos and appreciable effort.
Very nice explanation! ❤👍
because array str is delared with a size of 5 .
the compiler adds the null character by itself as we have declared it as a string hence the size would imply that 4 characters for the string and the last character will be reserved for the '\0'.
Therefore, we should always give one extra size while declaring the string variable.
harry bhai made strings easy. Thank you harry bhai
Quick quiz answer:
Even if the compiler adds a \0 automatically due to presence of double quotes but the array here doesn't have space to accommodate the null character hence it will create problem.
array can store 6 characters because indexing starts of 0. 0 to 5 total 6 numbers are there.
The real problem is that str stores the base address of the array.We cannot store string in that.
Sir mast question hai!
The real problem is that
str is a pointer which stores the base address of character array. we cannot assign a string to it.
Thank you sir ❤️
we supposed to enter str[6]; to solve the problem, bz string size is char+1 for enter"\0"....
9:39
Because size is not sufficient for string as it needs to include '\0' as a null character..!!
You are a very good RUclips channel I like your channel😀😀😀😀
best coding channel ever
Str 5 can store only 5 character and the word "hello " is already consist of 5 so there is not a null character space 😊
The str[5] is a character array that can store 5 elements, but to store 'harry' 6 characters are required(null character required).
Char str[5] can store only 4 elements with 1 null character for printing "hello" it should be like Char str[6].
in double quotes( " " ) compiler will add a null character to variable hence it will need an extra space is required. Due to this reason line will create an problem.
So the size of the string should be at least { length + 1}
str[5] is valid for the string length of 4 character string because of one null character not for 5 string character. For 5 string character we need str[6]
Str(5) array is an character string that can store 5 elements but for store Harry 6 character are required
Quick quiz ans.= the above line will create problem becaz we need str[ 6] to store hello string and 5 +1 space is needed to print NULL char.
9.52 Because str has only 5 memory locations but we need 6 location to store Hello and one \0
Str[6] will be correct because of hello=5 char, " " = 1 char ,total =6 char
Means,
char str[6]={'h','e','l','l','o','/0'};
🙃
I hope you understand 😀
Because the length of string is 5 and the length of char name is also 5. It has not a sufficient space for NULL character.
9:40 length of string will be 6 due to 1 null character /0 and other 5 character of hello and instead of that only 5 is issued so that will create problems.
Timestamp - 9:40
//Quick Quiz
The character array str[5] cannot store the string "hello" because the array's size and the string are the same and there is no extra space for the null character to be stored in the character array. Thus, this would create a problem.
9:38 Bcoz sir array size is 5 and bcoz we have put double quotation therefore there's no space for it. Hence the string will be printed with garbage values ..So the space should be increased .
9:39 because it requires character length plus 1 extra array
Sir as you said and we all know element of an array started from 0 so char str[5] is correct according to me cause it has 6 spaces so we can include '\0' too in that if i write "harry".
I'm confused sir, coreect me if im wrong.
When we declare an array with any index number then its mean its locations is one less of it
For example str[5] have 0,1,2,3,4 locations so we have to declare another location for null
9:42 mein len+1 size hona cahiye to accomodate \0 but the size of that array is len not len+1
The correct method will be
char str[6];
str="hello";
Null character bhii add Krna hota h isliye apko 6 lena h length +1 fir hojyga
Super learning And lecture thanks you harry bhai 😍
quick quiz answer: because there would be no space to store the null value and thus it would not be as a string to the compiler
Because str[5], can store only 5 character not the extra null character (that is - length of array should be one greater than the element's characters value)
String one dimension array
So represent by
Char str[5];
Str[5]="hello";
at 9:49 question the problem was invent because of we can allocate only 5 characters memory and whereas we need as 6 characters so we can repalce arr[6] so problem was solved😁
str is a pointer to the base element of the char array str [ ] , so we cannot store "hello" in it because it is not a char array. Besides , the value of str is not modifiable.
At 9:40 the length of the string has to be equal to 6. Because we have to store a null character which will require extra space of one byte along with characters of the string.
Nahh, the array length is already of 6 : str[0], str[1], str[2], str[3], str[4], str[5].Count it !!
@@learnwithpaidi8185 no
@@ananya696singh how ?? Just see the array size is equal to 6 ..!!
@@learnwithpaidi8185 bhai yrr kya? saaf saaf likha hai ki array ka size 5 hai toh phir ye problem create toh krega hi
@@ananya696singh aree behen , array ka space 0 se count krte hai na..0 se 5 tak count kre tho 6 slots memory create hoga na..
str[5] = "hello" is wrong because if we write it, It is a user defined string number which is str[5] = "hello" in this case so their is no space for null character in this array of string, So in order to make it write there are 3 ways
First
str[] = "hello";
Second
str[6] = "hello";
third
str[6] = { 'h', 'e' , 'l' , 'l' , 'o' }
9:52 the str only have 5 spaces and the word is also of 5 characters so there would be no space left for null character.
awsm harry bhai...
str = "hello" consists of 5 characters and when it's checked by the C compiler gcc then it adds a null character in the end and so the size is 6 but it is initialized with char str[5]; so the second line will throw error as there's no problem in initializing the array but the index is overlapping the declared size of the character array
// Online C compiler to run C program online
#include
int main(){
char malik[5]="Atif
";
printf("By using printf
");
printf("%s
",malik);
char anothermalik[10];
printf("Enter another name :
");
gets(anothermalik);
printf("By using gets and puts :
");
puts(anothermalik);
}
Because array take an input name of array along with each index number
In quick quiz:
Since string is of 5 characters, therefore character array should be of 6 characters for accomodating null character.
In this selfish world there is harry giving such precious knowledge for free and main is easy to understand even for me ( branch mechanical )
Char array ka size 6 dena hoga as 1 extra for a null character 😊
Best teacher ❤️❤️
because null character ke liye space nahi hai and null character hona must hai
sahi bola na harry bhai
9:40 char arr[] "as to provide a null (\0) character there has to be a extra space to store it, otherwise the compiler can't terminate the string."
The Str[5] stores only 5 characters but does not contain null character. That's why above line will completely pronounce error in line.
The word "hello" will create problem because it is a 5-letter word. In the code we have assigned the maximum value that the array can accommodate is up to 5. But while using string we have to denote '/0' in the array code to signify that the string ends here and not to print the garbage value further. So, array required to accommodate given string has to be number of characters in string +1 which is used to denote null character. Therefore, in the code shown, after accommodating the 5 characters of "hello" in the array, there will be no room for denoting null character which will create the problem in the code.
Lower of area str( ) values of prinrd
String 5 can store only 5 digits and the "hello" itself has 5 digits and because of the semicolon it will also add \o(null character) but there is no space to store the \o character.ok
Great 👍👍👍👍👍
because as you said to strong a value in string we have to assign string as length of string +1
As we need 1 extra space for null character to make it valid string.When we want Hello to print it needs six unit spaces as null character must be include for valid string.
9:50 Because we did not give space for '\0'.
9:53 Problems will arise because we need to make an array of length 6 to store null character
Nicee love it .....sir😁😋😋😋😎😎😍