Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I don't know why cannot compile simple project below by dev.
error: Project1.exe has stopped working.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n,d;
while(1){
printf("enter number");
scanf("%d",n);
d=n%10;
while(d!=0){
n=n/10;
printf("%d",d);
d=n%10;
}
}
return 0;
}
For one, your project does compile, since you get a run time error.
The run time error occurs because you are not using scanf correctly. The arguments to scanf after the format string should be pointers to the variables.
I don't know which compiler you are using, but any fairly modern compiler would have given you compiler warnings about this, e.g. here's Clang's output:
apa.c:9:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",n);
~~ ^
Changing this into scanf("%d",&n); makes your program work.
https://linux.die.net/man/3/scanf
First, the error that you got is not a compile, it's a run time error.
Second, it's happen when the run arrive to the line of scanf. This function required the variavbles address to put the value in.
Replace this:
scanf("%d", n);
With this:
scanf("%d", &n);
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
Alright, so basically recently I started studying C as a hobby, and I wanted to create a small program.Everything works fine, but when I make the variable "Age" like this:
int myAge = "14";
printf("My age is%d\n", myAge);
It prints out 1642.
I have tried switching to
printf('My age is%i\n', age);
But it did the same.
I also tested changing the number to a string but it obviously failed, because this isn't Python.
Anybody can help?
Save time, enable all compiler warnings.
Perhaps receive a warning like:
warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
int myAge = "14"; sets myAge to the address of the string literal "14".
Instead, initialize with an int.
int myAge = 14;
This is a fixed version of your program. Just declaring the age variable as an integer is enough to fix it.
#include <stdio.h>
int main() {
int age = 14;
printf("My age is %d", age);
}
Friendly note:
C is not like Python. Arrays don't work the same, strings don't exist, and programming in C is fundamentally different in every way. I would advise that you definitely take a course rather than trying to teach yourself from scratch.
u should write int myAge = 14 , writting 14 in double quotes means its a string (thx HAL)
We use double quotes when we have string and single quote when we have character but 14 is an integer we should write int myAge=14; or if u want "14" u should write char myAge[2]="14"; then printf("%s",myAge);
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
So i'm trying to solve this codeforce problem https://codeforces.com/contest/431/problem/A
.Basically i input 4 integers(a[0]...a[3]) and an array of integers between 1 and 4 then i need to output the sum of the string values according to the 4 initial integers.(check the codeforce's exemples)
So my code did work on the 5 initial tests but i had a wrong output on the 6th test
enter image description here
Here's the code
#include <stdio.h>
int main()
{
int test=1;
long s,result=0;
long a[3];
int i,x;
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
if (a[i]==0)
test=0;
}
scanf("%d",&s);
while (s!=0)
{
if (test==0)
break;
x=s%10;
s=s/10;
result=a[x-1]+result;
}
printf("%d",result);
return 0;
}
Your help would be much appreciated.
There have several problems with your code. But the most severe problem for which you are getting wrong output because you are using "%d" format specifier for long values, but its "%ld" actually. Using %ld will solve the problem.
Leaving rest of the problems for you to find out. Happy coding!
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
int main()
{
int i,fact,a;
printf("Enter a number: ");
scanf("%d",&a);
for(i=1;i<=a;i++){fact=fact*i;}
printf("%d",fact);
return 0;
}
Image is output of Turbo C
The above code when compiled and run in Turbo C++ gives output 16048 for 5 while Visual Studio gives output 120 for 5 which is correct.
Please help me out with this... Thanks
you need to initialize fact:
int main()
{
int i,fact=1,a;
printf("Enter a number: ");
scanf("%d",&a);
for(i=1;i<=a;i++){fact=fact*i;}
printf("%d",fact);
return 0;
}
The variable fact is not initialized, which means that the program has undefined behaviour; perhsaps you are lucky for the case of the correct result.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I did the exact same thing in another part of code and got no error or any problem with it's way of working but in this part and so it gives me an error.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
I'm using a switch-case function for my code and this is how I call it in main function;
case 3:
{
triangle(area);
}
Feels weird but this works perfectly;
void square(int length)
{
printf("Enter the length of square:");
scanf("%d",&length);
int area;
area = length*length;
printf("\nRectangles area is %d.",area);
}
Like this;
case 1:
{
square(area);
}
I did the exact same thing in another part of code and got no error or
any problem with it's way of working but in this part and so it gives
me an error.
C does not guarantee to diagnose all incorrect code, neither at compile time nor at run time. Nevertheless, you should turn up your compiler's warning level. If afterward it anywhere accepts a call to your triangle() function with other than exactly two arguments and without emitting at least a warning of some kind, then throw it out and choose a better one.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
Note that your function does not actually use the value provided by the caller for either argument, nor does it in any way return any information to its caller, even if you call it with the correct number of arguments. If that's what you want then it would be better to declare it to accept no arguments, and to call it that way:
void triangle(void) {
int length;
int height;
// ...
Note that length and height are now strictly local variables. You call that variation like so:
triangle();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here?
Here's my program:
#include <stdio.h>
void main(void)
{
int loop_counter = -8;
int user_input = 9;
char c1 = '9';
char c2 = 43;
while(loop_counter != 21);
{
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
loop_counter = loop_counter + 1;
loop_counter++;
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
getchar();
}
printf("loop exit\n\n");
getchar();
}
The biggest problem is probably that you have not articulated what you are hoping the code will do or what you intended the code to do or what you think it does. I am assuming you just need some help so my attempt to do so is below.
Running this through the compiler and interpreting the error messages helps a little.
Right off the bat, the compiler doesn't like your call to main. The compiler offers a suggestion that will successfully fix this error, so just follow the advice (and don't forget to add a
return 0;
statement before you exit main.
Second warning the compiler generates is that you have a semicolon at the end of your while statement. It also tells you how to fix it. Follow the instructions and you should be able to generate an executable.
You will still, however, have problems at runtime. This gets back to what is your intent.
With errors above corrected, your loop_counter variable enters the while loop initialized to -8, increases by 1 on line 19 and again increases by 1 on line 20. A call is made to getchar() but no input is given. Also lines 7, 8, and 9 are not used by your program.
Hope this gives you some direction. :)
The biggest error is
while(loop_counter != 21);
where the trailing ; will make the loop infinite.