Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
It is said that,in c,b++; is equal to b=b+1; if this is the fact test++ in my code why generate a compile time error.
test+1 is working well but test++ is not working.but why?
#include<stdio.h>
int main(void)
{
char test[80]="This is a test";
int a=13;
for(;a>=0;a--)
{
printf("%c",*(test++);
}
}
The ++ and -- operators are not defined for arrays.
v++; would be the same as v = v + 1;. Assumed v was typed an array this would imply assigning to an array, which is not defined.
char test[80] = "This is a test";
char *p = test;
for(int a = 0; a < 14; a++)
{
printf("%c", *(p++));
}
Well, for one thing, b++ is not the same as b=b+1.
But even if it were -- I think you'll find you get a similar error if you try test = test + 1.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Somehow my program outputs the same size no matter how long the array gets, do you know what i did wrong?
char charArray[] = "STRING";
int size = sizeof(charArray) / 2 - 1;
printf("%d", size);
Output: 3
(i have to create a program which finds a string in another string thats why i am substracting 1 at the end to find the length of the word i want to find)
If you just want to get the length of your string, you could use strlen from the string library, of implement your own one:
size_t my_strlen(const char *str)
{
size_t i = 0;
while (str[i] != '\0')
i++;
return (i);
}
with this function, my_strlen("STRING") will return 6.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
why doesn't this program print anything?
int main(){
int a = getchar()-'0';
getchar();
int b = getchar()-'0';
int vsota = 0;
vs = (a+b)%10;
putchar(vs);
printf("\n");
}
I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.
putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.
Also, fix this:
int vsota = 0;
vs = (a+b)%10;
That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.
because you try to output non printable character
int main(){
int a = '1'-'0';
int b = '6'-'0';
int vsota = 0;
vsota = (a+b)%10;
putchar(vsota + '0');
printf("\n");
}
https://godbolt.org/z/NAyrNn
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am expecting a to be 5, making the comparison expression evaluate to true and thus print:
A = 5, B = 3, C = 6
main()
{
int i,a,b=3,c=6,s;
for(i=1;i<=1000;i++){
if(a*a+b*b==c*c){
printf("A = %d B = %d C = %d\n",a,b,c);
}
a = i;
}
}
Where is a initialized? I am seeing i being set, as well as b and c, but I'm not seeing any assignment to a.
The solution is to assign the current value of i to a: a=i prior to the if statement. You are assigning the value after the fact. Alternatively, evaluate i, not a.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not finding the correct output of this program.It giving run time error.
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
}
It's division by zero. Since you are using a post-decrement in your loop counter c, it is becoming 0 in the last iteration.
Now that you know the reason for the run time error from the answer by #EugeneSh, here's how you can fix it.
do {
no /= c;
} while(--c); // Use pre-increment instead of post-increment.
In addition the all these answer above I just want to say it's better to check whether a number is zero before division -
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
if(c!=0){
no /= c;
}
} while(c--);
printf ("%d\n", no);
return 0;
}
This will prevent these kind of runtime error.
Hope it will helps.
Thanks a lot.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}