Why does the code stop executing after the scanf? [duplicate] - c

This question already has answers here:
NULL arg allowed to sscanf?
(6 answers)
pointer segmentation fault at scanf [duplicate]
(3 answers)
Closed 2 years ago.
float *salaire;
salaire = NULL;
scanf("%f", salaire);
printf("\nLe salaire : %f", *salaire);

You need to identify the place where you can store this float value and null means no space exist so you should allocate space for it. I recommend replace second line by :
salaire=malloc(sizeof(float));

Related

How to receive only the specifier value type and printing an appropriate message when not? [duplicate]

This question already has answers here:
scanf error handling C
(2 answers)
Closed 1 year ago.
scanf("%x", &hexa_decimal_num);
I am trying to deal with the case of entering something wrong like "ZZ" and printing "Error!".
int x = scanf("%x", &hexa_decimal_num);
if (x == 0)
{
printf ("Error!");
}

Should I always set char[] last value to '\0'? [duplicate]

This question already has answers here:
Null terminated string in C
(5 answers)
Closed 4 years ago.
Should I always set the last value of a char array to '\0'?
char search_for[80];
search text here
printf("Search for: ");
scanf("%79s", search_for);
search_for[strlen(search_for) - 1] = '\0';
This is a example from a C book.
When using scanf with the %s format it adds it for you, one huge logical error you have though is using strlen when you're not sure that your string is null terminated.

why is my c program scanning more inputs than the specified number? [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
problems with scanf("%d\n",&i) [duplicate]
(3 answers)
Closed 4 years ago.
If \n is not there the program accepts 5 and if \n is used in scanning then the programs asks for 6 inputs.
#include<stdio.h>
int main()
{
int marks[5];
int i;
for(i=0;i<5;i++)
{
scanf("%d\n",&marks[i]);
}
for(i=0;i<5;i++)
{
printf("the element at %d is %d\n",i,marks[i]);
}
return 0;
}

c: string entered not recognised [duplicate]

This question already has answers here:
Why is "a" != "a" in C?
(11 answers)
Closed 7 years ago.
Given below is a piece of code that does not do what I want
do
{
printf("inserisci un nome: ");
scanf("%29s", s);
} while (s!="*");
My aim is to exit from the cycle if the string entered is "*".
Why doesn't it work?
What should I modify?
Take a look at strcmp to compare strings, != will not do what you want.
In that case != will compare the variable s (a pointer to the first element of the array s) with the string "*". That is why it was not working properly.

How to stop a program from storing more than a single character into a char variable? [duplicate]

This question already has answers here:
How to prevent scanf causing a buffer overflow in C?
(6 answers)
Closed 8 years ago.
My program uses a scanf as such:
scanf ("%c", &symbol);
is there a way to print an error if the user enters in a string > one character? e.g "abc" as it messes with the program later on
Use a string buffer, fgets() into it, check if the second character is a \n.

Resources