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 months ago.
Improve this question
I'm practicing some simple code. When my int number is 12 the output is -743998012 for some reason even with different numbers too.
here is my input code
#include<stdio.h>
int main()
{
int favnum;
printf("Enter number: ");
scanf("%d", &favnum);
printf("My favorite number is %d.", &favnum);
return 0;
}
I try to change variables like float or double didn't work
As said by user3121023, you need to remove the & in printf("My favorite number is %d.", &favnum);.
This is because when you add it, it means that you're not passing its value, but its address in the memory (the pointer where the variable favnum is stored). That's why in the line before you use & : you want to store the input of the console in the right memory location, where the variable favnum is.
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 last month.
Improve this question
#include<stdio.h>
#include<conio.h>
int main(){
int marks[3];
printf("enter number for first array");
scanf("%d",marks[0]);
printf("enter number for second array");
scanf("%d",marks[1]);
printf("enter number for third array");
scanf("%d",marks[2]);
printf("the value of first array is %d",marks[0]);
printf("the value of first array is %d",marks[1]);
printf("the value of first array is %d",marks[2]);
return 0;
}
i was expecting that it will print value of matrix but it keeps saying this progarm has unexpectly closed down
scanf() expects you to pass the address of the variable to fill, eg:
scanf("%d",&marks[0]);
& is the address-of operator which provides that.
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 last year.
Improve this question
Hi I am new to c and trying to build just a simple print array however I just get what it wants to give me. Here is my code:
#include <stdio.h>
int main(){
int n[5]={5,10,15,20,25};
int i;
printf("displaying integers:");
for ( i=0; i<5; i++)
{
printf("%d \n", &n[i]);
}
return 0;
}
And the output is:
displaying integers:6422280
6422284
6422288
6422292
6422296
Any help would be great I tried creating it as an enter integers and get an output but regardless of input it gave me extremely large numbers. which is why I'll be happy if it just prints. Sorry if it's an obvious one but I've tried 5 different ways all with similar/basically identical results.
What you get printed are address locations of items stored in array.
If you want to print values of items in array, you should not use the address-of operator [ & ]. Try it this way:
printf("%d \n", n[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 2 years ago.
Improve this question
int main()
{
int i= 0;
printf("i value is %d\n",i);
scanf("%c", &i); // I am giving an input of 255 here
printf("i after scan %d\n",i); // This prints 50. How???
return 0;
}
Can someone explain how does the printf statement give 50? I have a little-endian machine.
Your program won't even compile as I is undeclared. I am assuming that it is a typo. Since you are scanning %c it will read only one character which is 2 from 255. Now 2 has ascii code of 50 which is being printed.
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 5 years ago.
Improve this question
I recently started coding in c. I was wondering how you can repeat/loop a task as many times as the user wants (by input).
int a,i;
scanf("%d", a);
for(i=0; i<a; i++){...}
This is the code I came up with, but it doesn't work. It's an infinite loop.
You need a & to scan into.
int a,i;
scanf("%d", &a);
for(i=0; i<a; i++){...}
Read a bit more about scanning integers here.
How to scanf only integer?
Change scanf("%d", a) to scanf("%d", &a)
The code didn't work for you because you failed to store the value entered by the user.
Before modifying your code as my suggestion try to print the value of a in your code, you'll get it, why it's not working..
The problem is in line:
scanf("%d", a);
You have to use:
scanf("%d", &a);
& sign represents address, you want to store input value on the address of variable a.
Read more about ampersand here:
When should I use ampersand with scanf()
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);