I am an absolute beginner in C, and I wrote this code in codeblocks and built it, it had no errors. The program is, we input two integers and display, sum, subtraction, multiplication, and division of no1 and no2.
Here's the code :
#include <stdio.h>
int main ()
{
int no1,no2,sum,sub,multi,div;
printf("Enter your first number");
scanf("%d", &no1);
printf("Enter second number");
scanf("%d", no2);
sum=(no1+no2);
sub=(no1-no2);
multi=(no1*no2);
div= (no1/no2);
printf ("%d + %d = %d \n",no1,no2,sum);
printf ("%d - %d = %d \n",no1,no2,sub);
printf ("%d * %d = %d \n",no1,no2,multi);
printf ("%d / %d = %d \n",no1,no2,div);
return 0 ;
}
I got 0 errors but when I ran it, cmd opens, and then I input values for no1 and no2 then the program crashes and gives the message windows will look into the issue.
Look closely at the this snippet: scanf("%d", no2);
Say you declare a variable named foo.
int foo;
This variable occupies some memory. It occupies four bytes of memory (because an int is four bytes wide).
Now let's declare another variable.
int *foo_ptr = &foo;
foo_ptr is declared as a pointer to int. We have initialized it to point to the foo variable.
As I said, foo occupies some memory. Its location in memory is called its address. The char '&' is the “address-of" operator.
This operator returns the address of an variable. In our case foo, thus foo_ptr now point to the address memory of the foo variable.
Think of every variable as a box. foo is a box that is sizeof(int) bytes in size. The location of this box is its address. When you access the address, you actually access the contents of the box it points to.
You missed '&' here...
printf("Enter second number");
scanf("%d", &no2);
You forgot to put '&' at the second scanf :)
Related
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.
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 *
Hi I just started C as my first programming language.
I was playing around a bit with scanf() and found something strange.
#include <stdio.h>
#include <Windows.h>
int main()
{
int x;
printf( " Type any number : " );
scanf( "%d", &x );
printf( "You entered %d.\n", &x );
system("pause");
return 0;
}
It always shows me a result of 7 or 8 digit number.
How is it possible?
You printf x's address instead of value. Fix like this:
printf("You entered %d.\n", x);
scanf wants the address where to store the result, printf can take the value itself.
You are printing out the address of x. Remove the '&' before it in the printf and you should see the right thing.
Putting an & before a variable gets a pointer to it - this is necessary in the scanf because it must change the value, but since printf only uses the value, no pointer is needed (except for strings, which are always pointers).
I was teaching the C programming language to a friend and we came up with something I could not explain. This is the code we wrote:
#include <stdio.h>
int main(void)
{
char num1;
char num2;
printf("%s", "Enter the first number: ");
scanf("%d", &num1);
printf("%s%d\n", "The number entered is:", num1);
printf("%s", "Enter the second number: ");
scanf("%d", &num2);
printf("%s%d\n", "The number entered is:", num2);
printf("%s%d\n", "The first number entered was:", num1); /* This was done for testing */
printf("%s%d\n", "The sum is:", num1+num2);
return 0;
}
The weird thing is that we tried to do 5 + 6 and we expected to get 11 but instead got 6, I added a line to see what's going on with the first number and it becomes 0 after the second number is read.
I am aware that the variables should be an int (in fact the original code was like that and worked) but my understanding is that a char is a small integer so I thought it would be 'safe' to use if we were adding small numbers.
The code was tested and compiled on a Linux machine with cc and on a Windows machine with cl. The output was the same. On the Windows machine the program throw an error after the addition.
I would like an explanation on why this code is not working as I expected. Thanks beforehand.
You cannot pass a pointer to a different datatype to scanf. scanf will write to memory assuming you gave it a pointer to what it expected (e.g. int for %d), and will exhibit wonderful undefined behaviour if you give it a pointer to a different datatype.
Here, what is most likely happening is that scanf is overwriting e.g. 4 bytes on your stack when your chars only take up 1 byte, so scanf will just be happily writing right over some other variable on your stack.
a char is a small integer so I thought it would be 'safe' to use it if we were adding small numbers.
That is correct, char is a small integral type , and it's OK to use it in integer arithmetic(although char may be signed or unsigned which may causes the result unexpected).
But the problem is, a pointer to char can NOT be used in a place where a pointer to int is expected. And this is the case for scanf("%d", &num1);, the second parameter is expected to a of type int *.
This is probably a very elementary problem, but I cannot find the answer anywhere, and this is the first time I've had the problem after several weeks of programming in C. In essence, if I write some code looking something like this:
int size;
scanf("%d", &size);
printf("size is %d", &size);
If I input, say, size = 2, the program will print back out something along the lines of 133692 or a similar number. Why is this? What have I done wrong here?
Try
printf("size is %d", size);
& gives you the memory location (address) of a variable.
printf("size is %d", &size);
So, the above will print the memory location(address) of size, not the value stored in size.
You have to do:
printf("size is %d", size);
instead. This prints the value of the int object size.
But
printf("size is %d", &size);
is undefined behavior.
You are printing the address of size, i.e., &size.
Just pass a plain size.
Remove the & in the printf statement as &size --> prints the address.
printf("size is %d", size);
The calling conventions are different in printf, and scanf.
Printf's arguments are typically by value (no &), but scanf has to have pointers. (&)
(You can't put a new value to e.g. 1.0, but you can print it...)