This question already has answers here:
Why does scanf require &?
(7 answers)
When should I use ampersand with scanf()
(3 answers)
Closed 4 years ago.
I'm new to C language and having this problem alot, the code compiles with no problems yet still get this message: Segmentation fault
when trying to execute my code on UNIX terminal.
This is the code:
///MAIN FUNCTION///
int main() {
printf("==========================J.U.S.T=======================$
printf("Enter your option number:\n1- working with an existing f$
char choice[1];
scanf("%c",choice);
if(choice[0] == '1'){
printf("first choice");
}
else if(choice[0] =='2' ){
printf("Enter the following data one by one:");
char BookT[50],AUTHORn[50];long int ISBN;
printf("Book Title:");
scanf("%s",BookT);
// sleep(5);
printf("Author name:");
scanf("%s",AUTHORn);
// sleep(5);
printf("Book number:");
scanf("%d",ISBN);
// sleep(5);
printf("%s\n%s\n%d\n",BookT,AUTHORn,ISBN);
}
else printf("Wrong choice try again!!");
return 0;
}
I'm using Kali linux to compile the code.
As you probably have noticed, the segmentation fault happens at the line
scanf("%d",ISBN);
scanf requires a pointer to save the result in, but you are passing it the value of the variable ISBN. In order to save the result in the ISBN variable you should send it a pointer to the variable instead: scanf("%d", &ISBN);
(arrays already work (more or less) the same way as pointers, so there is no need to use & when reading strings)
You should add an ampersand and proper format specifier to scanf scanf("%ld",&ISBN);
Just change the line scanf("%d", ISBN); by scanf("%d", &ISBN); , scan requires a pointer and you are trying to save the result without pointer, so the program will just segfault. But if you give him the adress of the variable, he can reach it and save the result in it.
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
I'm still learning the basics of C right now and I wanted to build a simple 'enter y to loop the program' that I've seen in a lot of console programs and wrote the following code to test it out. When I do execute it however, it does work as intended once after which the program just exits. It would help me a lot if anyone told me what I'm doing wrong here :
int main()
{
char l;
do
{
printf("Loop successful.\n");
fflush(stdin); //I heard this has to be used because scanf keeps the enter key in buffer or something like that
scanf_s("%c", &l);
} while (l == 'y');
}
I also get a "Missing integer argument to 'scanf(_s)' that corresponds to conversion specifier '2'" warning and I don't seem to understand what I'm being warned against.
fflush(stdin); //I heard this has to be used because scanf keeps the enter key in
That's wrong, fflushing stdin is undefined behaviour, what you need to do is consume the newlines lefted by the previous scan, just switch from
scanf_s("%c", &l);
to
scanf_s(" %c", &l); // Notice the space before %
This question already has answers here:
Access violation writing location 0x00000000. reading int from keyboard
(5 answers)
Closed 4 years ago.
suppose I have the following code:
int number;
scanf("%d", number);
printf("%d", number);
I am entering a number, say 10, as the input, but I am not getting it to print anything. If I change my code so that it scans the input as a string it works fine:
char number[2];
scanf("%s", number);
printf("%s", number);
Am I doing something wrong? And if so, what? I am doing all my work at https://www.onlinegdb.com/online_c_compiler if that changes anything
You need to pass the address of number to scanf: scanf("%d", &number);
The reason that it works when you use a string is because a string in c is really just a pointer to a char, and printfknows that, for a string, it needs to dereference the pointer to get to the chars of the string.
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 5 years ago.
Just starting to learn C/C++ in my CS 221 class.
Here's the start of my code:
int main()
{
int id;
char name[100];
double wages;
printf("Enter employee's id: ");
scanf("%4i", id);
printf("Enter employee's full name: ");
scanf("%99[^\n]",name);
printf("Enter gross salary for %s :",name);
return 0;
}
Having trouble wrapping my head around char arrays, stdin and buffers in c. Why does the above code skip the user imputing a name? do I need fflush in there? couldn't really get that to work either.
Not sure why you use
scanf("%4i", id);
But try:
scanf("%d", &id );
This fixes the issue of %4i format, and replaces it with %d for "Decimal integer" format.
This also fixes the issue of , id); because written like this, will not allow id to be stored, and would definitely crash.
If you have issues with numbers you are entering crashing from being too big, maybe check the boundaries of your data type int which will be platform specific.
Also change:
scanf("%99[^\n]",name);
to:
scanf("%s\n",name);
This takes the data entered by the user through scanf and interprets it as a "String of characters" and places it in name properly.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
In following c code scanf is not working.
When program is run, it execute upto the second printf line and after it skips the scanf("%c",&sex); and directly execute next printf().
Why this so happen?
I run this code on different c compilers, but the output is same.
#include<stdio.h>
void main()
{
char mar,sex;
int age,flag=0;
printf("Married [Y/N]:");
scanf("%c",&mar);
printf("Sex [M/F] :");
scanf("%c",&sex); //**This not working**
printf("Age :"); //**execution directly jumped here**
scanf("%d",&age);
if(mar=='y')
flag=1;
else if(sex=='m'&& age>=30)
flag=1;
else if(sex=='f'&& age>=25)
flag=1;
else
{
}
if(flag)
{
printf("Congratulations!!!! You are Egligible..");
else
printf("Sorry... You are not egligible..");
getch();
}
//Output
Married [Y/N]:y
Sex [M/F] :Age :23
Congratulations!!!! You are Egligible..
The problem is with the new line character that you press after you enter values (Y/N) for the previous scanf. The new line character is taken as an input and the program proceeds with the next one. Try to use flushall(); before next read (that is scanf) this will solve your problem. You can also use space before the format specifier to solve this, that will escape the newline character.
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 6 years ago.
I'm a newbie learning to program in C, and I am currently working on creating a program that takes a name input from a user then prints that name back on to the screen. When the program prints out the name in the else if block, what I get is $ ". I want to know why it happens and how I might correct this problem. My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int main() {
char * ans; // yes/no answer
char * name; //name variable
printf("Welcome to the program. Would you like to begin? (y/n)\n");
scanf("%s", ans);
if (strcmp(ans, "y") != 0) {
printf("Have a good day!\n");
return 0;
}
else if (strcmp(ans, "y") == 0)
printf(" %s\n", ans);
printf("Okay, input your name:\n");
scanf("%s", name);
printf(" %s", name);// I get $ " rather than the name.
return 0;
}
You are using scanf() to read characters from the user, but haven't allocated any memory to hold those characters. This gives undefined behavior, as "anything can happen" when you break the rules like that.
Make those uninitialized pointers into arrays instead:
char ans[128];
char name[128];
This gives you 128 characters of space for each string (one of which will be used by the terminator).