+ 1
why it prints nothing in output?? please clarify me this doubt
#include <stdio.h> #include<string.h> int main() { char p[20]; char *s = "string"; int length = strlen(s); int i; for (i = 1; i < length; i++) { Ā Ā Ā Ā Ā p[i] = s[length - i]; printf("%s",p); } return 0; }
19 Respostas
+ 2
why it is not working with *s ?? please clarify me and what is difference between char []s and char *s?
am not modify I'm Just storing it to another array and printing then how it crashes?
+ 2
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length -1 - i];
p[i] = '\0';
printf("%s",p);
+ 2
becoz you did not consider that array Index starts counting from ZERO.
+ 1
~ swim ~ I'm not changing the value of s but I'm storing it on another variable right but still why it is not modified
+ 1
~ swim ~ since from start it was there
+ 1
~ swim ~ in my case I'm not modifying s I'm reversing and storing on p only .. but why not printing
p[i]=s[length-i] ....I'm not modifying s ... I'm storing in p
+ 1
codemonkey I know the code fix ... but I want to know the reason why it is printing garbage value? when p[6]
+ 1
codemonkey
but I have given %s only...now it not giving garbage why
#include <stdio.h>
#include<string.h>
int main()
{
char p[6];
char *s = "string";
for (int i=0;i<5;i++)
{
p[i]=s[i];
}
printf("%s\n",p);
return 0;
}
0
~ swim ~ I want to know why it prints nothing... I don't want code fix
0
~ swim ~ but instead of char *s I'm giving char s[] but now it is printing in output.. how it works with char s[] and not working with char *s?
0
~ swim ~ so char *s cannot be copied to any other variables??
0
~ swim ~ please check the below code char *s can be copied to another variable but I don't know why it is putting some garbage value at last
int main()
{
char p[6];
char *s = "string";
for (int i=0;i<6;i++)
{
p[i]=s[i];
}
printf("%s\n",p);
return 0;
}
0
codemonkey yes we can copy but I'm not modifying *s ... I'm storing s[0] to p[0] but why it prints nothing??
I don't want to fix the code.... I want to know the reason why it prints nothing
0
codemonkey here also I'm not copying null but it's not printing garbage why??
#include <stdio.h>
#include<string.h>
int main()
{
char p[6];
char *s = "string";
for (int i=0;i<5;i++)
{
p[i]=s[i];
}
printf("%s\n",p);
return 0;
}
0
~ swim ~ in my case I'm not modifying s I'm reversing and storing on p only .. but why not printing
p[i]=s[length-i] ....I'm not modifying s ... I'm storing in p
0
~ swim ~ okay see the below scenario
..
I'm not storing anything in p[0] ... but it is printed as tring ... please explain this
you told if p[0] is 0 or null then it will not print anything for p
int main()
{
char p[7];
char *s = "string";
for (int i=1;i<7;i++)
{
p[i]=s[i];
}
printf("%s\n",p);
return 0;
}
0
As you use %s which is used for whole string, u can use %c as you are printing each character
Or
After the for loop:
End the string p[i] ='\0' ;
then print the string using %s
0
Hola buenas tengo un proyecto para hacer y pues me decidĆ por investigar una app y lo malo es que no se de progracion ni lenguajes para poder investigar el como fue su cĆ³digo fuente y desarrollo del cĆ³digo alguien que me pueda ayudar
0
As string is collection of characters, we use %s specifier to print whole string, but u r printing each character in string. So it not through the error