New to C and can't figure out these outputs [closed] - arrays

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]);

Related

its giving me random numbers [closed]

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.

I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user [closed]

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 11 months ago.
Improve this question
I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user.
//program to print numbers 0 to n where n is input from user
#include<stdio.h>
int main()
{
int i=0,num;
printf("Enter number: ");
scanf("%d",&num);
while(i<=num)
{
printf('%d',i);
i++;
}
return 0;
}
Im getting error saying expected const char
I tried to get solution over several websites
since im new to this language I'm facing trouble in such simple code
I tried running this code on several online compilers but everywhere I get the same issue
In line 11 in the printf statement you have used single quotes - '%d' which does is giving you problems here, change it to a "%d". Hope that helps.

My C code for 2d array creation and printing doesn't return any value [closed]

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 written a piece of c code which I intend to just print out the value of a 2d array
I'm very new to c so this answer might be really basic sorry.
my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.
heres my code
#include <stdio.h>
int main(void) {
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i2 = 0;
printf("hello");
for(int i = 0; i > 4; i++){
i2 = i;
printf("hi %d", array1[i][i2]);
}
}
returned value
hello
i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.
Thanks.
There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example).
The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.

Using %c in scanf and assigning the value to an int variable in C? [closed]

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.

My c program is not giving any result.pls help everyone [closed]

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
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

Resources