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

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.

Related

Understanding how C scanf input works [duplicate]

This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 2 months ago.
I have a sample weird code like this:
#include<stdio.h>
int main(){
int t;
scanf("%d", &t);
while(t--){
char s[1];
scanf("%s", s);
}
}
I have test that if I have t from input that > 1 ( like t = 3) then put a single character to s, then the program ends, while t hasn't downed to 0 yet. Could anyone explain for me what happens here. Thanks for your help!
Change char s[1]; to char s[100]; or other length to allow at least as many characters as you will enter plus one for the terminating null character.
When scanf processes a %s, it appends a terminating null character to the end of the characters it matches to the %s. Since you declared s as char s[1];, there is no reserved room for the terminating null character, and scanf attempts to write beyond the reserved space.
Then the behavior of your program is not defined by the C standard.

I am trying to write a program that stops with a specific string input [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 1 year ago.
I am just starting to learn C coding ,I am trying to write a program that keeps scanning names and once it scans a specific name lets say for example "John" it stops Please correct me
#include <stdio.h>
int main()
{
char name[20];
printf("enter a name:");
for(;;)
{
scanf("%s",name);
if (name='John')
{
break;
}
}
You can't compare two string pointers with != or ==.
you need to use strcmp, for example:
while (strcmp(check,input) != 0)
strcmp compares the string pointed to, by str1 to the string pointed to by str2.

Reading multiple character arrays in C using scanf [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Loop skips a scanf statement after the first time
(1 answer)
Closed 4 years ago.
char a[100],b[100],c[100];
scanf("%[^\n]",a);
printf("%s",a);
scanf("%[^\n]",b);
printf("%s",b);
The compiler seems to be reading the first read but skips the second read. Why is this happening?
Because of un handled Enter
Use fgets()
Try this :-
char a[100], b[100], c[100];
fgets(a, 100, stdin);
printf("%s", a);
fgets(b, 100, stdin);
printf("%s", b);

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