Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying my best to understand the memchr function but having some issues with some simple output. I'm getting extra characters at the end of *newchar.
#include <stdio.h>
#include <string.h>
int main() {
char plus[6] = "12+123";
char *newchar = (char*) memchr(plus,43,3);
printf("%s",newchar);
}
output:
+123( '
I expected to get "+123," why does it give me the extra characters? I noticed the output is consistent which confuses me earlier, it doesn't seem like these were grabbed from somewhere random in memory but were caused by the memchr function.
char plus[6] = "12+123";
You've defined an array of size 6, and initialized it with 6 characters. You haven't left enough room for the NUL terminator. There is garbage at the end of your string, and printf doesn't know when to stop printing it.
Do this instead, allowing the string to be appropriately sized automatically:
char plus[] = "12+123";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm experiencing a problem in the resolution of an exercise. I need to read N strings from file, but I can only read the first. How can I fix it?
#include <stdio.h>
int main() {
/* variable declarations */
FILE *fp;
char vet[100];
fp = fopen("file.txt","r"); /* open file with N strings */
while(!feof(fp)) {
fgets(vet, 100, fp);
vet[100]='\0';
printf("%s\n", vet);
}
}
vet[100]='\0' this will generate error in runtime, also you don't need this line of code because fgets will handle the end of the string itself.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
When I am trying to print or write data in following way its automatically concatenating more than one string into one.
Following is the structure holding data:
typedef struct Data{
int id;
char fname[10];
char lname[11];
char address[27];
char area[18];
char city[21];
char cityCode[3];
char pinCode[5];
char contact1[13];
char contact2[13];
char email[33];
}Data;
Data d;
When i try to print this in following manner
printf("\nData is as follow:\n");
printf("\nID:%d",d.id);
printf("\nfname:%s",d.fname);
printf("\nlname:%s",d.lname);
printf("\naddress:%s",d.address);
printf("\narea:%s",d.area);
printf("\nCity:%s",d.city);
printf("\nCityCode:%s",d.cityCode);
printf("\npinCode:%s",d.pinCode);
printf("\ncontact1:%s",d.contact1);
printf("\ncontact2:%s",d.contact2);
printf("\nemail:%s",d.email);
This is what i got
Data is as follow:
ID:1
fname:abc
lname:xyz
address: 6649 N test lane
area:Test City
City:testtest
CityCode:XYZ12345 123-456-6789789-456-6123
pinCode:12345 123-456-6789789-456-6123
contact1: 123-456-6789789-456-6123
contact2:789-456-6123
email:ttest#gmail.com
2
Same thing happened when I tried to write it on file using fprintf().
1,abc,xyz 6649 N test lane,Test City,testtest,XYZ12345 123-456-6789789-456-6123,12345 123-456-6789789-456-6123, 123-456-6789789-456-6123,789-456-6123,ttest#gmail.com
2
My main aim is to write the data on file separated by (,) and the data is
1,abc,xyz, 6649 N test lane,Test City,testtest,XYZ,12345, 123-456-6789,789-456-6123,ttest#gmail.com
I'm just guessing here, but if we take a look at the cityCode member, it is an array of three characters. To be a "string" of three characters, it needs space for four characters, to include the terminating '\0' character.
Make sure all arrays have space for, and have, the string terminator character.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
//I wanted to separate last digit i.e."2" as an int from "hello.mp4;2" Here is the code:
int main()
{
char str[30];
int separate = 0;
strcpy( str, "hello.mp4;2" );
sscanf(str, "%*[^;]%d", &separate);
printf("%d\n",separate);
return 0;
}
and it is not woriking...
Change the sscanf to:
sscanf(str, "%*[^;];%d", &separate);
I.e., you need to match the semicolon ; after the string that excludes it. The portion in the square brackets matches the string that precedes the semicolon, leaving ;2. So you then need to match the semicolon ; before trying to match the 2.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon