Unable to print contents of an array [closed] - c

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 months ago.
Improve this question
This is my code
#include<stdio.h>
int main(){
int num[5];
printf("Enter the numbers: ");
scanf("%d %d %d %d %d", &num[0] &num[1] &num[2] &num[3] &num[4]);
printf("Your numbers are : %d %d %d %d %d \n",&num[0] &num[1]&num[2]&num[3]&num[4]);
}
This is the error I get:
cpractice3.c:7:33: error: invalid operands to binary expression ('int
*' and 'int') scanf("%d %d %d %d %d", &num[0] &num[1] &num[2] &num[3] &num[4]);
~~~~~~~ ^~~~~~~ cpractice3.c:8:55: error: invalid operands to binary expression ('int *' and 'int') printf("Your
numbers are : %d %d %d %d %d \n",&num[0] &num[1] &num[2] &num[3]
&num[4]);
I have googled and figured out how to print the contents of an array using for loop.
I have seen some similar questions here where the answers pointed to int being a pointer and not the actual value.
I am trying to write a simple code which will read the user input of multiple numbers at once and store them at specific index in the array and I don't want to use for loop.
Can anyone tell me what dumb mistake I am making.
Thanks for the help.

You're missing commas between parameters.
You're passing the addresses of variables to printf instead of their values.

pass the variables and add the commas
int main(void) {
int num[5];
printf("Enter the numbers: ");
scanf("%d %d %d %d %d", &num[0],&num[1],&num[2],&num[3],&num[4]);
printf("Your numbers are : %d %d %d %d %d \n",num[0] ,num[1],num[2],num[3],num[4]);
return 0;
}

Related

Why is my C program skipping over if statements? [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 3 years ago.
Improve this question
I am a new coder and have no idea what I am doing please help!
The code is reading and taking inputs just fine until it reaches the scanf(" %c", &i);then it skips to the Amount print seemingly ignoring my if statements.Is there somthing wrong with my use of scanF?
Here is the code:
#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D;
int t=1;
double b,d;
while (t=1){
printf("Enter the old balance:\n");
scanf(" %lf", &b);
printf(" %lf", b);
if(b>=0)
{
printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");
printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
scanf(" %c", &i);
if(i=F){
printf("Your ending balance is");
printf(" %lf", b);
printf("\n Program is ending!");
return 0;
}
if(i=W){
printf("Amount:");
scanf(" %f", &d);
b= b-d;
printf(" %f",b);}
}
if(b<0);
{
printf("The balance must be maintained above zero!\n");
}
}
return 0;
}
Because your comparison is wrong
if(i=F){ // this is assignment, not comparison
should be
if(i=='F'){ // note also it's comparison to character 'F'
In addition to wrong comparison operators, your way of comparing two chars is also wrong. If you want to check whether the input char i equals to 'F' , you should initialize the char variable F with character 'F', otherwise it would be just unintialized variable and comparison with that variable would be wrong.
You should add these lines just after you initialize F and W.
F = 'F';
W = 'W';
By the way, you can use another variable names like,
char var1 = 'F';
char var2 = 'W';

Why can't I add other character in my code about grading students? [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 3 years ago.
Improve this question
In the following code a,b,c,d,e are "subjects" (as in class) and the variable perc is the average of the marks for the "subjects".
I can't figure out what is wrong in the code, I can't write anything after pressing enter while writing mark of first subject.
//Calculating the grade of students
#include<stdio.h>
main()
{
int a,b,c,d,e ;
printf("Enter the marks of the following subjects \n") ;
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e ;
float perc ;
perc= (a+b+c+d+e)/5 ;
printf("Your percentage is %f \n", perc);
if (perc>90)
printf("You have A grade") ;
else if (perc>70)
printf("You have B grade") ;
else if (perc>50)
printf("You have C grade") ;
else if (perc>35)
printf("You have D grade") ;
else if (perc<35)
printf("Failed") ;
}
Would appreciate some help
The problem is the usage of scanf() in reading multiple int's
in order to do so you need to get read of the "," in the scanf()
Thus replacing:
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e;
In:
scanf("%d %d %d %d %d", &a,&b,&c,&d,&e);
and also divide by 5.0 instead of 5:
perc= (a+b+c+d+e)/5.0 ;
Now it will work as expected.
P.S. you also had problem with the placing of ")"
This is a syntax problem. Replace your scanf line with the following:
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
Your code has few errors,
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e;
It should be scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
And perc = (a+b+c+d+e)/5; should be perc = (a+b+c+d+e)/5.0;
You use non-whitespace characters , in the format string. According to the function description, each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal. Thus, your input format shall strictly follow the following pattern any-number, any-number, any-number, any-number, any-number and only after it printed you shall press the enter key. In other cases your program fails

%d in scanf not working. number 4223092 comes up [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 3 years ago.
Improve this question
Can anyone help me with this code? I don't see any problem, but somehow it doesn't work. When I typed my favorite number and press enter, the number 4223092 came up.
int target;
int after;
#include <stdio.h>
int main() {
printf("What is 6 x 4?: ");
scanf("%d", &target);
if (target == 24) {
printf("Correct!\n");
printf("By the way what is your favorite number?: ");
scanf("%d", &after);
printf("%d is my favorite number too!\n", &after);
} else {
printf("Wrong!\n");
printf("By the way what is your favorite number?: ");
scanf("%d", &after);
printf("%d is my favorite number too!\n", &after);
}
return 0;
}
In your code
printf("%d is my favorite number too!\n", &after);
you don't need the &. You want to print the value, not the address.
Just to let you know, in it's current form, passing a int * as argument to %d invokes undefined behaviour. So, you cannot justify the output in any ways.
%d expects an argument of type int, not int *. Passing an incompatible type of argument invokes the UB.
Quoting C11, chapter ยง7.21.6.1, P9
[...] If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
The printf function does not require the address of a variable to print to stdout. Just pass the name of the variable like so:
printf("%d is my favorite number too!\n", after);
The %d format specifier looks to display an integer value. If you pass something else, like an int * in your case, it will give you strange and unexpected results.
And to make this answer more complete, to print a pointer variable, use the %p format specifier.
The value 4223092 is the address of the "after" variable.
In C language you do not need a pointer to print a value. You only need the value of the variable. So whenever you are printing a variable just print the variable and not the pointer (address) of the variable.
So just change the following lines in your code
printf("%d is my favorite number too!\n", &after);
To
printf("%d is my favorite number too!\n", after);

Why does casting this double to an int give me 0? [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 7 years ago.
Improve this question
Sorry this has been confusing me for hours and nothing comes up on google about this. I'm programming in C, trying to convert decimal to binary, and I'm confused as to why this keeps giving me the right amount as input but the integer value is always 0.
even if I properly cast input as an int, it doesn't work (ie: int integer = (int) input;
main() {
double input;
scanf("%d", &input);
printf("input: %d \n", input); // this will print fine
int integer = input; // stores nums before decimal
printf("integer:%i\n", integer); // this always prints 0
Because you are using the wrong specifier to read -> scanf() or print -> printf() the double, do this
if (scanf("%lf", &input) == 1)
{
printf("input: %f\n", input);
printf("integer: %d\n", (int) input);
}
You need To Correct Format Specifiers.
main(){
double input;
scanf("%lf",&input);
printf("input: %lf \n", input);
int integer = input;
printf("integer:%i\n", integer); }
Some Format Specifiers
Or you can read from here

C, unknown conversion type character 0x20 in format [-Wformat=] error [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 7 years ago.
Improve this question
printf("\nThe largest dog is %c with a weight of %.2 kg\n", largetype, largest);
fprintf(outfile,"\The largest dog is %c with a weight of %.2f kg\n", largetype , largest);
printf("\nThe smallest dog is %c with a weight of %.2f kg\n", smalltype , smallest);
fprintf(outfile,"\nThe smallest dog is %c with a weight of %.2f kg\n", smalltype , smallest);
Hello,
So my C program is not compiling and it is showing me this error,
unknown conversion type character 0x20 in format [-Wformat=]
for the first 2 lines.
Does anyone know what to do with this error?
In your first row
printf("\nThe largest dog is %c with a weight of %.2 kg\n", largetype, largest);
You're using %.2, but you're not specifying the type, that's what's throwing the conversion error. You should use %.2f instead, just like in the row below.
printf("\nThe largest dog is %c with a weight of %.2f kg\n", largetype, largest);

Resources