sscanf unexpected results - c

#include<stdio.h>
#include<string.h>
int main()
{
char buffer[32];
char c;
int i;
printf("input: ");
fgets(buffer, 32, stdin);
printf("items filled: %d\n", sscanf("%c%d\n", &c, &i));
printf("%c%d\n", c, i);
return 0;
}
When typing a character followed by a number "f7", im expecting "f" to go into variable c, and "7" to go into variable i. For some reason, sscanf() fails to fill both, and I'm getting their initial garbage values. What am I doing wrong?

Actual sscanf() signature is this :
int sscanf(const char *str, const char *format, ...);
check your signature of sscanf() you have used wrong signature
It should be
sscanf(buffer,"%c%d\n", &c, &i);

You're not using buffer in the sscanf() call, so they're not getting filled.
int i;
char c;
char buffer[32];
fgets(buffer, 32, stdin);
sscanf(buffer, "%c%d", &c, &i)
^
you're missing this part

Do not use sscanf. The correct way to do the parsing you are trying to do is
#include <stdlib.h>
#include <stdio.h>
// ...
char *endptr, buffer[32];
char c;
int i;
// ...
fgets(buffer, 32, stdin);
c = buffer[0];
i = strtol(buffer+1, &endptr, 10);
if (endptr == buffer+1 || (*endptr != '\0' && *endptr != '\n')) {
puts("invalid input");
return 1;
} else {
printf("%c%d\n", c, i);
return 0;
}

sscanf("%c%d\n", &c, &i) is never told to look in the buffer for c and i, it looks like you are making this call incorrectly.

Related

How to print leading spaces before char array in C?

#include <stdio.h>
int main() {
char a[30];
char b[30];
scanf("%[^\n]s",a);
scanf(" %[^\n]s",b);
printf("%s \n",a);
printf("%s",b);
return 0;
}
Input :
hai
hello
Output :
hai
hello
But I am expecting
hai
hello
How to print leading spaces before hello?
Note that %[…] (a scan set) is a complete conversion specification. The s after it in your code never matches anything, but you can't spot that. The newline is left in the input. You'd have to arrange to read that before using the second scan set — and a space (or newline) in the format string is not the answer to that. Replacing the s with %*c would do the job. But you're probably best off not using scanf() at this point; use fgets() instead.
Using scanf()
#include <stdio.h>
int main(void)
{
char a[30];
char b[30];
if (scanf("%29[^\n]%*c", a) == 1 &&
scanf("%29[^\n]%*c", b) == 1)
{
printf("[%s]\n", a);
printf("[%s]\n", b);
}
return 0;
}
Input:
hai
hello
Output:
[ hai]
[ hello]
Using fgets()
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[30];
char b[30];
if (fgets(a, sizeof(a), stdin) != 0 &&
fgets(b, sizeof(b), stdin) != 0)
{
a[strcspn(a, "\n")] = '\0';
b[strcspn(b, "\n")] = '\0';
printf("[%s]\n", a);
printf("[%s]\n", b);
}
return 0;
}
For the same input, this produces the same output.
You may try fgets:
#include <stdio.h>
int main() {
char a[30];
char b[30];
fgets(a, 30, stdin);
fgets(b, 30, stdin);
printf("%s \n",a);
printf("%s",b);
return 0;
}

Segmentation fault when reading floats from terminal input

After confirming input Segmentation fault pops up and I can't see why. I was told to use fgets and sscanf in loop to read an undefined number of floats from terminal input and this is what i came up with..
Code
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define EMPTY 3.141592
#define BUFFERSIZE 100
void removeSubstring(char *s,const char *toremove){
while( (s=strstr(s,toremove)) )
memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
}
int main(){
int x=0;
int y=0;
float a[BUFFERSIZE] = {EMPTY};
char buffer[BUFFERSIZE] = {EMPTY};
char *pos;
char *start = 0;
int space = ' ';
printf("Input: ");
fgets(buffer, BUFFERSIZE, stdin);
while(x< BUFFERSIZE){
sscanf(buffer, "%f ", &a[x]);
pos = strchr(buffer, space);
removeSubstring(start, pos);
x++;
}
printf("Saved input: ");
while(y<BUFFERSIZE && a[y] != EMPTY){
printf("%.2f", a[y]);
y++;
}
printf("\n");
return 0;
}
Edit: Increased both array sizes and removed feof
Almost certainly the problem is in the line pos = strchr(buffer, space);. If the input does not contain a space character, then pos is set equal to NULL. Passing a NULL to strstr() will likely result in a SEGV.

Simple password-prompt in C

I'm trying to make a simple user and password autentication in C.
I was told to never use gets() when getting input and I should use fgets() instead.
But I'm not sure of how the fgets() works or why is giving me this input.
Here is the code.
#include <stdio.h>
#include <string.h>
int login(char *user, char *passwd){
int enter = 0;
char p[6];
char u[6];
printf("User: ");
fgets(u, sizeof u, stdin);
printf("Pass: ");
fgets(p, sizeof p, stdin);
printf("%s\n", p);
if (strcmp(user, u) == 0 && strcmp(passwd, p) == 0){
enter = 1;
}
return entrar;
}
int main(){
char user[] = "admin";
char passwd[] = "12345";
if (login(user, passwd)){
puts("--ACCESS GRANTED--");
}
else{
puts("--Wrong pass or user--");
}
return 0;
}
Ouput
User: admin
Pass:
--Wrong pass or user--
It doesn't even let me enter the password after I press enter.
The problem here is the size of your char[], if you set it to 6, 'admin' will overflow, as reading with gets will read some extra characters.
Try with a bigger string, I'm sure you can afford it, say:
char u[10];
char p[10];
And that is still a quite stingy ;)
well.. you had two problems
one as suggested here, you should change the size of u and p to 10, the other is, the fgets also fetches the new line \n that you need to remove before comparing the strings.
so the complete answer would be:
#include <stdio.h>
#include <string.h>
int login(char *user, char *passwd){
int enter = 0;
char p[10];
char u[10];
char *pos;
printf("User: ");
fgets(u, sizeof u, stdin);
if ((pos=strchr(u, '\n')) != NULL) {
*pos = '\0';
}
printf("'%s'\n", u);
printf("Pass: ");
fgets(p, sizeof p, stdin);
if ((pos=strchr(p, '\n')) != NULL) {
*pos = '\0';
}
printf("'%s'\n", p);
if (strcmp(user, u) == 0 && strcmp(passwd, p) == 0){
enter = 1;
}
return enter;
}
int main(){
char user[] = "admin";
char passwd[] = "12345";
if (login(user, passwd)){
puts("--ACCESS GRANTED--");
}
else{
puts("--Wrong pass or user--");
}
return 0;
}

Remove leading zeros error

I am trying to remove the leading zeros of the number user enters so 000002 will turn into 2
However, I am getting an error saying Segmentation fault (core dumped)
#include <stdio.h>
#include <string.h>
int main()
{
char *str;
scanf("%c", *str);
int n;
if( ( n = strspn(str, "0" ) ) != 0 && str[n] != '\0' ) {
printf("String without leading zeros is %s \n", &str[n]);
} else {
printf("No leading zeros in %c \n", str);
}
return 0;
}
Change %c to %s and *str to str.
You don't need to use the * when scanning.
Besides the errors pointed out in the previous answers, scanf("%d") will take care of removing leading zeroes for you. Do the test:
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
printf("%d\n",a);
return 0;
}
And if you absolutely need a string, just convert with sprintf:
#include <stdio.h>
int main()
{
char str[256];
int a;
scanf("%d", a);
sprintf(str, "%d", a);
puts(str);
return 0;
}
remove * from this:
scanf("%c", *str);
and change %c to %s because you are scanning a string not character
As #user3291093 has said, you need to change %c to %s since you are reading a string/char array and not a single character.
Also you need to malloc an area to store the array. For example:
char *str = NULL;
str = malloc(100*sizeof(char));
scanf("%s", str);
The compiler should have warned about this problem. Insure warnings are enabled.
char *str;
scanf("%c", *str); // "%c" does not match `*str` for scanf()
Suspect OP wanted something like
char str[100];
if (scanf("%99s", str) == 1) Continue_Along_the_Happy_Path();
An alternative fgets() solution.
char str[100];
fgets(str, sizeof str, stdin);
char *p = buffer;
while (*p == '0') p++;
if (p != buffer) {
printf("String without leading zeros is %s", p);
} else {
printf("No leading zeros in %s", str);
}

is there an any way to discard '\n'?

I am trying to to record response by the user(using getchar()). I am having issues with '\n' sticking in buffer. If I use fgets(char* buf, .. , ..), '\n' again goes into buf and you have to include '\n' at the end of the test string. when using string.h functions (like strcmp()). Is there any clean way of writing code for such purposes.
#include<stdio.h>
#include<string.h>
int main()
{
char buf[100];
fgets(buf, 3, stdin);
puts(buf);
int i = strcmp("p\n", buf);
printf("%d", i);
//if (!strcmp("CLock to random\n", buf))
//{
//puts("sucess");
//}
char c;
c = getchar();
putchar(c);
return 0;
}
Now I want to record response(single character 'p'). If I use getchar(), in place of fgets(), program skips second getchar()( c = '\n'). If I use the current code, i have to include \n in strcmp() every time.
If you want to discard the \n:
char buf[0x1000];
fgets(buf, sizeof(buf), stdin);
char *p = strchr(buf, '\n');
if (p) *p = 0;
#include <stdio.h>
#include <string.h>
char* chomp(char* str){
size_t len = strlen(str);
if(len>0 && str[len-1] == '\n')
str[len-1] = '\0';
return str;
}
int main(void){
char buf[128];
fgets(buf, sizeof(buf), stdin);
printf("<%s>\n", buf); //include newline
printf("<%s>\n", chomp(buf));//drop tail newline
printf("<%s>\n", chomp(buf));//NC
return 0;
}

Resources