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.
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 1 year ago.
Improve this question
I have a problem that this code doesn't end with output.
I appreciate if help me.
#include <stdio.h>
int main(){
int number,counter=0;
scanf("%d",&number);
while (number!=0){
number=number/10;
counter++;
}
printf("the number has %d digits",&counter);
return 0;
}
You should remove "&" from your printf statement.
& is for scanning not printing
the correct form is :
printf("the number has %d digits",counter);
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 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)
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 9 years ago.
Improve this question
I want to compare if two arrays are equal. I know i have to loop through both arrays and see if they match. But how do i determine the longest array to use as an end to the loop.
EDIT
if (intToRoman(roman_integer, result))
{
for(int i = 0; i < ???; i++ )
}
for example,
roman_integer[] = "MMM"
result[] = "MMMDCCLXXX"
use strlen function to find the length of greatest string, like
int greatestlength=arrlength(a);
if(greatestlength<arrlength(b))
{
greatestlength=arrlength(b);
}
use strlen inside of arrlength or directly or write your own code in arrlength whatever you want