Why getting random outputs, in place of input taken in C? - c

This is C. So trying to put multiple inputs into this array and keep getting numbers like this for when output the input 6356740 "6356744 6356748 6356752"
#include <stdio.h>
#include <stdlib.h>
int main()
{
int quiz[4];
printf("Enter four quiz grades: ");
scanf("%d %d %d %d", &quiz[1], &quiz[2], &quiz[3], &quiz[4]);
printf("%d %d %d %d", &quiz[1], &quiz[2], &quiz[3], &quiz[4]);
}

An array with K elements is indexed from 0 to K-1.
(This is mentioned in any introduction to arrays.)
Reading or writing quiz[4] has undefined behaviour.
You should not pass pointers to printf if you want to print integers.
int main()
{
int quiz[4];
printf("Enter four quiz grades: ");
scanf("%d %d %d %d", &quiz[0], &quiz[1], &quiz[2], &quiz[3]);
printf("%d %d %d %d", quiz[0], quiz[1], quiz[2], quiz[3]);
return 0;
}

You defined your array as
int quiz[4] which means the first reference is quiz[0], the second is quiz[1], the third is quiz[2] and the last is quiz[3].
In your code you are referencing the 2nd, 3rd, 4th and undefined 5th entry. This is important as you may be corrupting the data in your program which can caused undefined results.
The numbers you are seeing are the integer values of the addresses pointing to the ints in your array. Notice they increase by 4 which is the size of an int.
Also, your printf is printing the addresses of the int and not the ints themselves. The line should read
printf(ā€œ%d %d %d %dā€, quiz[0], quiz[1], quiz[2], quiz[3]);

If the grades being input are numbers, that '%d' in all four places is okay. But if you are entering alphabets, than you should be using '%s' instead.
And as Hogstrom and molbdnilo said, the first entry of a Array is 0, than second is 1, and so on. Hence you have to use &quiz[0] than &quiz[1] .. and so on.

Related

Infinite for loop? ()in c

the value of i resets after it reachers 7
#include <stdio.h>
int main(){
char marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
output
Problem is here:
scanf("%d", &marks[i]);
Specifier "%d" expects a pointer to int, not char. Usually it will write 4 bytes what is a typical size of int.
Therefore on 8th iteration the elements of marks at index from 7 to 10 are touched. However, marks[10] is outside of marks array (only indices 0-9) are valid. Undefined Behaviour is invoked and the program can do anything, from crashing to infinite looping or conjuring nasal deamons.
To fix the program change the type of marks to int:
int marks[10];
Note:
UB is invoked even on the first iteration because "%d" expects a pointer to int while type of &marks[0] is char*. This operation is undefined by C standard because int* and char* may differ in size and/or representation and/or alignment. However it is a unlikely case for modern CPUs.
You have declared the marks as a character array and tried to get input from user using %d which asks asks for an integer,
#include <stdio.h>
int main(){
int marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
Also I didn't understand the use of all so I couldn't find a solution for it. If you want to print a specific number then you have to specify it like you have done it for second or if you want to display the total you want to add a furthermore code to calculate the sum of elements in the array.

Finding sum of 2 variables from 1 statement in C

I recently started learning C and must create program that scanf two integer values from standard input separated by a space then printf the sum of these two integers. Must be able to accept negative values. I'm using repl.it to write code 1st then pasting in .c to compile.
Attempt:
#include <stdio.h>
int main(void) {
int j = 0;
printf("Enter 2 integers separated by space and press enter:\n");
while (scanf("%d", &j) == 1) {
printf("Here is the sum:"%d", j);
}
return 0;
}
Except this prints
Here is the sum: 1Here is the sum: 2
The output is wrong so what mistake did I make? Whats the correct method to get expected values?
(eg 1+2=3)
Your code is incorrect. It does not even compile due to "Here is the sum:"%d" ill formatted string. The program will repetitively scan an integer from standard input and print it as it was result of a sum.
Parsing two integers separated by a space can be easily done with scanf() function by using "%d %d" pattern. The function scanf() returns a number of successfully parsed arguments thus value of 2 is expected on correct input. Finally, add both number and print the result.
#include <stdio.h>
int main(void) {
int i, j;
if (scanf("%d %d", &i, &j) == 2) {
printf("Here is the sum: %d\n", i + j);
return 0;
}

Scanf_s reading wrong input

For this code below (in C)
int small_a, small_b;
printf("Please input two numbers\n");
scanf_s("%d %d", &small_a, &small_b);
printf("%d %d", &small_a, &small_b);
int test_2nd = small_a - small_b;
if (test_2nd < 0) {
printf("a is smaller %d", &small_a);
}
else {
printf("b is smaller %d", &small_b);
The values it prints when I write 4 and 2 is a huge six digit number (5504620 and 5504608 in this case) I don't understand where it goes wrong. stdio.h has been included as a header.
The problem here is in the print statement. In the code
printf("%d %d", &small_a, &small_b);
you don't need (want) to take (print) the address. Remove that &.
That said, this actually invokes undefined behavior. %d with printf() expects an argument of type int and you're essentially supplying an int *, causing the UB.
FWIW, to print an address (pointer), you need to use %p format specifier and cast the argument to void *

why am i getting a different number after declaring a variable?

i am new to C programming and i was trying to write a simple program that asks the user to re-arrange the numbers displayed on the screen but i encountered a problem, i would get a different number printed out on the screen instead of the value i assigned to the variable.why am getting a different number? Here is a screenshot of the problem i am having and my codethe image:
#include <stdio.h>
main()
{
int numOne, numTwo, numThree, ansOne, ansTwo, ansThree;
char name[20];
numOne=34521;
printf("\nWelcome to scrambled numbers Game");
printf("\n Please input your name to get started: ");
scanf("%s", name);
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
scanf("%d", &ansOne);
if(ansOne==12345)
{
printf("Congratulations %s you have won the first round", name);
}
else
{
printf("sorry %s you failed the first round", name);
}
}
This is happening because you are printing the adress of the variable numOne instead of numOne it self.
Try removing & operator from numOne in your printf.
Replace this:
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
By this:
printf("\nRe-arrange this numbers in ascending order %d :", numOne);
printf and scanf take different kinds of arguments: printf needs values to be displayed, and scanf needs pointer addresses to receive input data.
The &numOne argument to printf causes the program to show the memory address where numOne resides instead of the value inside the variable. You want to print numOne with no & operator.
On the other hand, &ansOne is correct for scanf.

how to convert char to int

I'm trying to write a short program were:
#include <stdio.h>
void main()
{
char=a,b,c;
printf("please place 3 numbers:\n");
scanf("%c%c%c", &a,&b,&c);
}
The exercise I'm trying to solve is how to change the char to int so if I write in a the number 3, I will get the number 3 Printed.
at this point I'm only getting the value.
I would appreciate any help.
The answer depends somewhat on what you can assume about the character set. If it's something like ASCII (or really, any character set that includes the digits in sequential order), you just need to offset the character value by the value of the character 0:
int aValue = a - '0';
I'm sure that C# provides better ways to do what you're trying to do, though. For example, see this question for some examples of converting strings to integer values.
First of all your syntax need some checking
You should know that you declare a variable this way (a char in this example):
char a;
If you want to declare multiple variables of the same type in a row you do :
char a, b, c;
If you want to assign a value to a declared variable :
a = '3';
Now to print a char using printf (man printf is a must read, more infos are in coreutils) :
printf("%c", a);
If you want to get the char from the command line, I recommand you to use getchar() (man getchar) instead of scanf because if suits better what you are trying to achieve and doesn't require you to use a syntax in scanf that I am sure you don't fully understand yet.
Your question is incredibly light on details, so here are several options:
#include <stdio.h>
int main()
{
char a,b,c;
printf("please place 3 numbers:\n");
scanf("%c%c%c", &a,&b,&c);
printf("Printing ints (auto-promotion): %d %d %d\n", a, b, c);
printf("Printing ints (explicit-promotion): %d %d %d\n", (int)a, (int)b, (int)c);
printf("Printing digits: %d %d %d\n", a-0x30, b-0x30, c-0x30);
return 0;
}
If the input is 123,
I expect the output to be:
Printing ints (auto-promotion): 49 50 51
Printing ints (explicit-promotion): 49 50 51
Printing digits: 1 2 3
Some things I fixed along the way.
main should return an int, not be void.
char=a,b,c; is a syntax error. You meant char a,b,c;
added a return 0; at the end of main.
You question is not quite understandable. Still I'll try to help. I think that what you want is to store an integer value in the char variable. You can do so by using the following code:
#include<stdio.h>
void main()
{
char a,b,c;
printf("Enter three numbers:\n");
scanf(" %c %c %c",&a,&b,&c); //notice the spaces between %c
}
Or if you want to enter a character and print its ASCII value, you can use the following code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a,b,c;
printf("Enter three characters:\n");
scanf(" %c %c %c",&a,&b,&c);
printf("Entered values: %d %d %d",a,b,c);
getch();
}

Resources