Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5};
int i;
for(i=0;i<5;i++)
{
printf("%d",a[6]);
}
return 0;
}
Que : Why a[6] is showing 344 value, why not zero. Where this value came from ?
C does not check if you are going out of bound for an array. If you do so, it is undefined behavior, then you will get garbage value, in worst case, you can get a seg fault also.
Because a[6] is somewhere in memory which you don't know what holds.
In your case its 344.
Your compiler won't tell you that you are out of bound.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Here is the code for copying string in C.
#include <stdio.h>
int main()
{
char s1[100], s2[100];
int i;
printf("Enter string s1:");
fgets(s1,sizeof(s1),stdin);
for(i=1;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("%s",s2);
}
However, when I enter "How are you?", the copied string become "#ow are you?"
Since arrays start at 0, your for loop should look like this
for(i=0;s1[i]!='\0';i++) {
// ^ 0, not 1
The for-loop must start at the index 0. So change your for loop to this:
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to use side effect operator in my expression which does not have just a variable. My program was compiled successfully but I got a runtime error "Segmentation fault"
Here is my code:
int main()
{
int x = 1;
printf(1 + (x++));
return 0;
}
C requires you to format the string, this way it knows what it should print. What you have in your example is nothing but memory addresses, which makes the C compiler confused.
int main()
{
int x = 1;
printf("%d\n", (1 + (x++)));
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <stdio.h>
int main (void)
{
int hist,geo,phy,chem,bio;
int credits=0;
printf("Enter marks in history : ");
scanf("%d",&hist);
if(hist>40)
credits =10;
else
printf("No credits awarded for history");
printf("Credits obtained is %d",&credits);
return(0);
}
when I run the code, and I get a value of 230586 for the variable 'Credits'. Please help. I am a beginner in C
&x is like asking a question "What is the address of variable x?" , that's why you get the strange number. In order to print the variable value, please pass credits instead of &credits to the printf function.
printf("Credits obtained is %d", credits);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How many times the while loop is executed in the below prog if short int is of 2 bytes?
main()
{
int j = 1;
while(j <= 255);
{
printf("%d",j);
j++;
}
return 0;
}
i think it should be 255 times but its not correct. Can anyone tell me why?
You have a semicolon at the end of your while-line. The while loop, consisting of the statement ;, executes "infinitely" many times.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to create a while loop in C, it says that build is successful, however it doesn't print out anything. I don't really see whats wrong, it doesn't show anything in the console.
int main()
{
int w = 0;
while (w >=100){
printf("w = %i" , w);
w++;
}
return 0;
}
You define w=0 and in the next line you write "while w is greater or equal than 100", which cannot work.
Try
while (w <= 100)