C .exe "stopped working" error [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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.
Improve this question
This C program compiles just fine, but when I run it, after it asks for the first input, I get a windows error saying "hw.exe has stopped working".
#include <stdio.h>
#include <math.h>
int main(void)
{
int valA, valB, valC;
double result;
printf("Enter the first side of the triangle.");
scanf("%d", valA);
printf("Enter the second side of the triangle.");
scanf("%d", valB);
valC = (valA * valA) + (valB * valB);
result = sqrt(valC);
printf("The square root of %d is: %f\n", valC, result);
return 0;
}
I am EXTREMELY new to C, so I'm sure that I am missing something super simple. Any help is appreciated. Thanks!

You have to use the & (address-of) operator when using scanf. For instance,
scanf("%d", valA);
Should be
scanf("%d", &valA);
This is because scanf expects the argument to be a pointer to the variable you're reading from input, not the variable itself. A pointer is the location of a variable in memory. scanf uses this information to change the value of the argument you provide.
Therefore, when you pass a value to scanf, it uses it as a pointer, regardless of what you actually passed as C is relatively type-unsafe. When you use an unitialized variable where scanf expects a pointer, scanf tries to write to a random location in memory, causing undefined behavior.

You need to pass an address to scanf:
scanf("%d", &valA);
You must pass the address of valA, and then scanf will write into the variable at that address. Your code passes the value of valA.
And likewise you make the same change for the other call to scanf.

Related

I face a problem in C. Input is wrong-- its say Segmentation fault [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 last year.
Improve this question
#include <stdio.h>
#include <string.h>
struct student_details{
char name[34];
int roll;
char section;
};
int main(){
struct student_details student[5];
for (int i = 0; i < 5; i++)
{
printf("Your full name = ");
scanf("%s", student[i].name);
printf("Your roll = ");
scanf("%d", student[i].roll);
}
return 0;
}
I think something is wrong with my code anyone please fix this.
When I run this code, it's Shows an error. after running this code this code take 1 time input and second input is skipped.
The scanf function expects to accept a format string and then pointers to the data you want to scan into. student[i].name is an array, which in this case decays into a pointer to its first element. This works.
Note: This array only contains 34 characters. With the null terminator, you want to use a width specifier with scanf to limit the input and prevent a buffer overflow. "%33s"
When you try to read the roll:
scanf("%d", student[i].roll);
student[i].roll is an int, not a pointer. But pointers are numbers, so this will compile. Your compiler should warn you about it, though. But, then the program tries to dereference this value it thinks is a pointer, and a segmentation fault occurs.
What you want to do is pass the address of student[i].roll.
scanf("%d", &student[i].roll);

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

Scanning and printing char variables 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 4 years ago.
Improve this question
Why is printing x is always equal to zero?
#include <stdio.h>
int main()
{
unsigned char num, x, nnum, y = 0;
printf("please enter the number of bits u want to clear \n");
scanf("%d%d", &x, &y);
printf("%d%d", x, y);
return 0;
}
I have tried to put them separately but after taking a variable from y the x becomes a zero.
I know that they will store eventually in the stack and every char is 1 byte, so when printing them
they should both have the value that I put in it.
In your code,
scanf("%d%d",&x,&y);
is undefined behavior. The conversion specifier and the supplied arguments do not match. With scanf() family, %d expects an argument of type pointer to a signed integer, whereas you are supplying a pointer to an unsigned char.
You need to use
scanf("%hhu%hhu",&x,&y);
The format specifier for a char is %c. Using %d leads to undefined behavior (overwrites the next variable on the stack).
If you want an 8bit unsigned integer then use uint8_t (stdint.h) and the SCNu8 and PRIu8 macros from inttypes.h.
The data type is char, so basically the scanf for the char is %c. Either you will seperate variables of it. If your x is a character then you would need to seperate into the next line. The scanf for integer is a %d.

Char type not taking an input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
For some reason I can not write to the ch char type from the scanf_s I am using in visual studio 2013 I have tried spacing the "%c" both ways " %c" and tried scanf and scanf_s. I am lost.
// Alphabetic Pyramid Program - The Egyptians must have used C!
#include <stdio.h>
int main(void)
{
char ch;
printf("Enter the letter that will be the foundations of your Aphabetic Pyramid:\n"); // User is promted to enter a letter
scanf_s(" %c", &ch); // User inputs Foundational_letter
printf("The code for %c is %d!\n", ch, ch);
getchar();
getchar();
return 0;
}
scanf_s expects two arguments for the %c specifier: the receiving address and the size, which is 1:
scanf_s(" %c", &ch, 1);
By design, all safe versions of scanf (those ending with _s) need some parameter specifying how much buffer space you reserved for receiving the input when it is of types string (%s) and char (%c). i.e. for %s you would specify the buffer address and its reserved size.
If you dont apply these rules, expect undefined behavior.

C-variables won't be added [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
I have started learning C and I want to make an addition program.
I have written the following code:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
int a,b,c;
scanf("Give a,b",&a,&b);
c=a + b;
printf("A+b=",c);
printf("\n\n\n");
return 0;
}
It is supposed to take 2 numbers from the users and show their sum.
However the only output I get after the user writes 2 numbers is A+b=
Any ideas?
You failed to include a format specifier for your integer variable c. Without this printf doesn't know what arguments to expect following the format string, what their types are, or how they are to be printed. Change:
printf("A+b=",c);
to:
printf("A+b=%d",c);
Note that a good compiler with warnings enabled (e.g. gcc -Wall ...) would have pointed out this simple mistake for you at compile-time.
Also your scanf usage is wrong - change:
scanf("Give a,b",&a,&b);
to:
printf("Give a,b");
scanf("%d %d",&a,&b);
You're missing the %d conversion specifier in the printf() call:
printf("a+b=%d\n", c);
Without that, printf() doesn't know it's getting a second argument and won't do anything with it.
Of course, that c could just be a + b, there's no need to store the sum in a separate variable just to print it.
Also, you need specifiers in scanf(), it should be:
scanf("%d %d", &a, &b);
The first argument to scanf() is not a prompt that's printed, it's describing the expected input.
Last, you should check the return value of scanf() to make sure it's 2 before relying on a and b having valid values. I/O can fail, you need to make sure your program does the right thing if that happens.
scanf is a function used to take in input from the user. You are expecting it to print something, ain't you? You need printf instead:
printf("Give a,b\n"); //\n at the end is good
Now, use scanf to scan in the input:
scanf("%d %d",&a,&b);
You are also missing the %d format specifier in the last printf as other answers have mentioned.
scanf() is an input C function (scan with formatting) that uses format specifiers (%datatype ex: %d or %i = int, which is what you want, or %c = char) to store a data entry in given format, provided by the stdin (keyboard buffer) up to and including a null (\0) termination char(in the case of a string);
The Ampersand (&) in C is used to designate/return the memory address (where variable is stored), as opposed to the value of the variable.
scanf("Give a,b",&a,&b);
c=a + b;
printf("A+b=",c);
The issue with above first line of code is that you forgot to include the format specifiers (%d or %i would both work in this case, as I assume you are adding integers and not floats/etc).
Thus the solution to your first problem is an easy one:
scanf("%d %d",&a,&b);
Also, it seems that you are attempting to combine an input and output in one line.
Printf, like scanf, can use format specifiers, or it can contain only a string of characters, and prints to the stdout (console).
What you want to do is include a prompt asking the user for input before storing said input. You add the \n "newline" character at the end to ensure to provide spacing between other outputs and inputs, among many other reasons when you get into char arrays[], aka strings. But for now, this should do the trick:
printf("Give a,b\n");
scanf("%d %d",&a,&b);
Hope this helps both with the current assignment and with general/C programming concepts!

Resources