How does sscanf separate a string into different variables? - c

Can someone explain how %25[^;] and %*[^ABCDF] are used in the following code?
char name[26], class[8];
int score;
char grade;
char record[50] = "Ross, Bob; MAC1234 85 A";
sscanf(record, "%25[ˆ;] %*c %6s %d %*[ˆABCDF] %c", name, class, &score, &grade);

Related

How would I get the last printf statement here to print both the output of x and y the user inputs?

(I am very new to C by btw)
My current code
#include <stdio.h>
char main()
{
char x;
printf("Please enter your first initial:",x);
scanf("%c",&x);
char string[100];
char y;
printf("Please enter your last name:",y);
scanf("%s",&y);
printf("Hello I am %c %s. Nice to meet you.",x,y);
return 0;
}
You can't use a char value with %s scanf formatting. That reads a null-terminated string, and char doesn't have a place for the null terminator.
Read the last name into the string variable, and use that.
char string[100];
printf("Please enter your last name:)";
scanf("%99s", string);
printf("Hello I am %c %s. Nice to meet you.", x, string);

Issues with sscanf in C

I am writing a program that could read a text with csv format and I am having this issue where the sscanf only parses the whole thing as one string when it is separated by ','.
For example, a code snippet below
char str[100] = "Alex,2933,89,";
char name[50] = "";
int id;
double mark;
sscanf(str, "%s,%d,%lf,", name, &id, &mark);
printf("%s\n", name);
printf("%d\n", id);
printf("%f\n", mark);
Output was:
Alex,2933,89,
896
0.000000
Which is clearly not the expected output.
But when str is edited to str = "Alex 2933 89 ", the code is giving me the correct output.
The working code:
char str[100] = "Alex 2933 89 ";
char name[50] = "";
int id;
double mark;
sscanf(str, "%s %d %lf ", name, &id, &mark);
printf("%s\n", name);
printf("%d\n", id);
printf("%f\n", mark);
Correct Output:
Alex
2933
89.000000
Can I know how do I fix this?
%s stops at white space, the fact that you put a , in the format string does not change this behavior. You should use the %[^,] conversion specification.
Change the code this way:
char str[100] = "Alex,2933,89,";
char name[50] = "";
int id;
double mark;
if (sscanf(str, "%49[^,],%d,%lf,", name, &id, &mark) == 3) {
printf("%s\n", name);
printf("%d\n", id);
printf("%f\n", mark);
}
Note however that %[^,] cannot parse empty fields. If the CSV line may contain empty fields, such as ,23,89.0, sscanf() will fail to convert name because no character matches the %[^,] specification. If you have such lines in the source file, you should parse the string fields manually with strchr() or strcspn().

Trying to make a structure, won't let me input a string

I'm trying to make a structure with the three variables int age, int siblings, and char[] hometown but it's not letting me insert the hometown string when the program is run. The integers work properly but it'll just skip right over the array and leave it blank. I've tried using gets and fgets but nothing seems to be working.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct person{
int age;
int s;
char hometown[20];
}p;
printf("Age: ");
scanf("%d",&p.age);
printf("Siblings: ");
scanf("%d",&p.s);
printf("Hometown: \n");
fgets(p.hometown, 20, stdin);
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n",p.age,p.s,p.hometown);
}
The local variable might already contains garbage.
Try to memset before you use for string,
So that proper null will be terminated.
Try to acquire your input with following scan(%s, p.hometown);
For strings no need of & for collecting the string.
If you still face the issue, please let me know.
This also works for town names that contains a space and is protected against buffer overflow too.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HOMETOWN_SIZE 20
int main(){
struct person {
int age;
int s;
char hometown[HOMETOWN_SIZE + 1]; //+ 1 for terminating null character
} p;
printf("Age: ");
scanf("%d", &p.age);
printf("Siblings: ");
scanf("%d", &p.s);
printf("Hometown: \n");
getchar(); //just for consume new line from previous scanf
fgets(p.hometown, HOMETOWN_SIZE + 1, stdin); //fgets reads n-1 characters
//don't want new line in hometown name
if (p.hometown[strlen(p.hometown) - 1] == '\n')
p.hometown[strlen(p.hometown) - 1] = '\0';
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n", p.age, p.s, p.hometown);
return 0;
}
Try flushing the Buffer memory before taking character array input
like this
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct person{
int age;
int s;
char hometown[20];
}p;
printf("Age: ");
scanf("%d",&p.age);
printf("Siblings: ");
scanf("%d",&p.s);
printf("Hometown: \n");
fflush(stdin);
fgets(p.hometown, 20, stdin);
printf("Age \t Siblings \t Hometown\n");
printf("%d \t %d \t %s\n",p.age,p.s,p.hometown);
}

Read a string as an input using scanf

I am new to C language and I am trying read a character and a string (a sentence; max-length 25) from a user.
Not sure what I am doing wrong in the following lines of code, its giving me an error "Segment Fault".
#include <stdio.h>
int main(){
char * str[25];
char car;
printf("Enter a character: ");
car = getchar();
printf("Enter a sentence: ");
scanf("%[^\n]s", &str);
printf("\nThe sentence is %s, and the character is %s\n", str, car);
return 0;
}
Thanks!
You have to make four changes:
Change
char * str[25];
to
char str[25];
as you want an array of 25 chars, not an array of 25 pointers to char.
Change
char car;
to
int car;
as getchar() returns an int, not a char.
Change
scanf("%[^\n]s", &str);
to
scanf( "%24[^\n]", str);
which tells scanf to
Ignore all whitespace characters, if any.
Scan a maximum of 24 characters (+1 for the Nul-terminator '\0') or until a \n and store it in str.
Change
printf("\nThe sentence is %s, and the character is %s\n", str, car);
to
printf("\nThe sentence is %s, and the character is %c\n", str, car);
as the correct format specifier for a char is %c, not %s.
str is an array of 25 pointers to char, not an array of char. So change its declaration to
char str[25];
And you cannot use scanf to read sentences--it stops reading at the first whitespace, so use fgets to read the sentence instead.
And in your last printf, you need the %c specifier to print characters, not %s.
You also need to flush the standard input, because there is a '\n' remaining in stdin, so you need to throw those characters out.
The revised program is now
#include <stdio.h>
void flush();
int main()
{
char str[25], car;
printf("Enter a character\n");
car = getchar();
flush();
printf("Enter a sentence\n");
fgets(str, 25, stdin);
printf("\nThe sentence is %s, and the character is %c\n", str, car);
return 0;
}
void flush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
}
// This is minimal change to your code to work
#include <stdio.h>
int main(){
char car,str[25];
printf("Enter a character: ");
car = getchar();
printf("Enter a sentence: ");
scanf("%s", str);
printf("\nThe sentence is %s, and the character is %c\n", str, car);
return 0;
}

Just first alphabet is showing in the output.[char type]

When I give the input then only first alphabet is showing.
I want to print the complete name which is I just entered.
#include <stdio.h>
int main()
{
char name;
char grades;
int i;
printf("Name of the Student:");
scanf("%c",&name);
printf("Name your Just entered is : %c",name);
return 0;
}
I agree with the others - but add some error checking and ensure no buffer overruns i.e
#include <stdio.h>
int main() {
char name[101];
printf("Name of the student:");
if (scanf("%100s", &name) == 1) {
printf("Name you just entered: %s\n", name);
return 0;
} else {
printf("Unable to read name of student\n";
return -1;
}
}
EDIT
As you have edited the question so that it does not have the same meaning as before I will leave my previous solution here.
But what you want is to use fgets - this allows for white space in the name
ie.
#include <stdio.h>
int main()
{
char name[100];
printf("Name of student:");
fflush(stdout);
fgets(name, 100, stdin);
printf("Students name is %s\n", name);
return 0;
}
Replace char name; with char name[100];. This will define name as array of chars, because you handled with it as single character.
For scanf replace it with scanf("%s",&name[0]);, and printf with printf("Name your Just entered is : %s",name);. %s means string, so it will scan whole string, not just single character. In scanf &name[0] points to beginning of array.
You need to scanf into an array, rather than into a single character:
#include <stdio.h>
int main() {
char name[100];
printf("Name of the student:");
scanf("%s", &name);
printf("Name you just entered: %s\n", name);
}
You are trying to store a array of characters(string) in a character. So only the first character is taken.To rectify this initialize the name as:
char name[40];
take input as :
scanf("%s",name);
and print as:
printf("name is %s",name);
name is a char and scanf will only catch one character when you use %c. You can use a char array to store the name instead :
char name[40];
/* edit the size for your need */
Also edit your scanf and printf to use a %s
You are reading (and printing) a single char using %c. If you want to handle stirngs, you should use a char[] and handle it with %s:
#include <stdio.h>
int main()
{
char name[100]; /* Assume a name is no longer than 100 chars */
char grades;
int i;
printf("Name of the Student: ");
scanf("%s",&name);
printf("Name your Just entered is : %s",name);
return 0;
}

Resources