int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId, nOfWorkingDay\n");
scanf("%s %s %d %d",&name,&surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
I get a strange output. For example, when I enter:
birol genç 30 35
the output is:
birol gen┼ 30 35
What is the problem here?
scanf("%s %s %d %d",&name,&surname,&studentId,&nOfWorkingDay); should be
scanf("%s %s %d %d", name, surname,&studentId,&nOfWorkingDay);
ie, remove the & before name and surname which are already addresses of the character strings.
int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId,nOfWorkingDay\n");
scanf("%s %s %d %d",name,surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
#include <stdio.h>
int main(){
int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId,nOfWorkingDay\n");
scanf("%s %s %d %d",name,surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
}
description:
scanf("%s",firstname);
The %s placeholder is used to read in text, but only until the first white space character is encountered. So a space or a tab or the Enter key terminates the string. (That sucks.) Also, firstname is a char array, so it doesn’t need the & operator in the scanf() function.
Related
The program tells to take input name and dob from friend and display it using structures.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
struct friend {
char name[40];
int dd, mm, y;
} f;
printf("enter details");
scanf("%c %d %d %d", &f.name, &f.dd, &f.mm, &f.y);
printf("name is %c and dob is %d/%d/%d", f.name, f.dd, f.mm, f.y);
}
OUTPUT:
enter details dasd 10 10 2004
name is £ and dob is 0/35/11408852
the output is giving this garbage value why?
You should correct these issues:
to read a word, you should use %s in scanf or more precisely %39s to read a word of up to 39 characters and a null terminator. %c reads a single character.
to check that input was complete, compare the return value of scanf to 4, the expected number of successful conversions. As coded, scanf() will fail to convert the DoB if the name has more than 1 letter, hence the members dd, mm and y remain uninitialized, a potential explanation for the garbage output.
to print the string in the name array, use %s in printf. %c expects an int and you pass an array or char, which is passed as a pointer to its first element. This has undefined behavior, another explanation for garbage output.
Here is a modified version:
#include <stdio.h>
int main() {
struct friend {
char name[40];
int dd, mm, yy;
} f;
printf("enter details: ");
if (scanf("%39s %d %d %d", f.name, &f.dd, &f.mm, &f.yy) == 4) {
printf("name is %s and dob is %d/%d/%d\n", f.name, f.dd, f.mm, f.yy);
} else {
printf("invalid or missing input\n");
}
return 0;
}
I start to study C language, and after a short break, I start to study it again and I'm in stuck with the new updates. This code doesn't works. I can't understand how to do now. If I change from scanf to scanf_s it's doesn't work anyway. I also try to change a declaration of the type. Is someone here who can explain how can I change this code and what to use now, scanf or scanf_s, and on what occasions to use them. Thanks in advance.
#include <stdio.h>
#define N 256
typedef struct
{
char name[N];
char surname[N];
char street[N];
char city[N];
char district[3];
int n_house;
int day, month, year;
} person;
main()
{
person s;
printf("ask name\n");
scanf("%s", s.name);
printf("ask sur\n");
scanf("%s", s.surname);
printf("ask wh h lives\n");
scanf("%s %s %s %d", s.street, s.city, s.district, &s.n_house);
printf("ask bd\n");
scanf("%d/%d/%d", &s.day, &s.month, &s.year);
printf("personal data of the person : \n");
printf("%s %s\n Nato il %d %d %d \n Vive in %s %s %s %d", s.name, s.surname, s.day, s.month, s.year, s.street, s.city, s.district, s.n_house);
}
The error is C4996 scanf. This function or variable may be unsafe. Consider using scanf_s instead. To disable depreciation, use _CTR_SECURE_NO_WARNINGS.
I compiled your program with some little modifications:
#include <stdio.h>
#define N 256
typedef struct
{
char name[N];
char surname[N];
char street[N];
char city[N];
char district[3];
int n_house;
int day, month, year;
} person;
void main(void)
{
person s;
printf("ask name\n");
scanf("%s", s.name);
printf("ask sur\n");
scanf("%s", s.surname);
printf("ask wh h lives\n");
scanf("%s %s %s %d", s.street, s.city, s.district, &s.n_house);
printf("ask bd\n");
scanf("%d/%d/%d", &s.day, &s.month, &s.year);
printf("personal data of the person : \n");
printf("%s %s\n Nato il %d %d %d\nVive in %s %s %s %d\n",
s.name, s.surname,
s.day, s.month, s.year,
s.street, s.city, s.district, s.n_house);
return;
}
And works with the input:
ask name
george
ask sur
mac
ask wh h lives
barcelona sskk ksl 123
ask bd
12/13/1111
personal data of the person :
george mac
Nato il 12 13 1111
Vive in barcelona sskk ksl 123
Is this the kind of input that you are trying ?
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);
}
Im a beginner in C. Im using scanf function in my c program and its getting stuck and the program moves ahead only when i type exit.
The part of the code in which Im facing problem is:
while(fread(&emr,recsize_emr,sizeof(emr)/recsize_emr,fp)==1)
{
if(emr.emr_id==emr_id)
{
printf("%s %s %s %d %d %s %s %f \n\n",emr.fname,emr.lname,emr.company_name,emr.emr_id,emr.agt_id,emr.pol_start_date,emr.pol_end_date,emr.amount);
printf("Enter New First Name, Last Name, Company Name, EmployerID, AgentID, Policy Start Date, Policy End Date, Amount : ");
scanf("%s %s %s %d %d %s %s %f \n",emr.fname,emr.lname,emr.company_name,&emr.emr_id,&emr.agt_id,emr.pol_start_date,emr.pol_end_date,&emr.amount);
fseek(fp,-recsize_emr,SEEK_CUR);
fwrite(&emr,recsize_emr,sizeof(emr)/recsize_emr,fp);
break;
}
}
In
scanf("%s %s %s %d %d %s %s %f \n");
the final " \n" consumes all whitespace after the float has been read. That means the scanf only returns after it received a non-whitespace character after the float - for that character to reach the program, you typically need to enter a newline after it.
If you remove the final whitespace from the format string,
scanf("%s %s %s %d %d %s %s %f");
the scanf returns as soon as it finished reading the float. Then it will leave the newline - or whatever character follows the float in the input` in the input buffer, so it is probably necessary to clear the input buffer before further scans.
/* It is not entering data into the third scanf() statement .*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
struct book
{
char name;
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%c %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%c %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%c %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("\n%c %d %f",a1.name,a1.pages,a1.price);
printf("\n%c %d %f",a2.name,a2.pages,a2.price);
printf("\n%c %d %f",a3.name,a3.pages,a3.price);
getch();
}
You want to use strings, not single characters:
int main(void)
{
struct book
{
char name[100];
int pages;
float price;
};
struct book a1,a2,a3,a4;
printf("Enter data into 3 books\n");
scanf("%s %d %f",&a1.name,&a1.pages,&a1.price);
scanf("%s %d %f",&a2.name,&a2.pages,&a2.price);
scanf("%s %d %f",&a3.name,&a3.pages,&a3.price);
printf(" you entered:\n");
printf("%s %d %f\n",a1.name,a1.pages,a1.price);
printf("%s %d %f\n",a2.name,a2.pages,a2.price);
printf("%s %d %f\n",a3.name,a3.pages,a3.price);
return 0;
}
But note this is prone to buffer overflows, and won't deal correctly with book names that contain spaces.
you are wanting a string as a name, while you are giving a %c specifier for the input which expects a character.
so either use %s for a string input.
or better use some string function like gets()
gets (a1.name);
scanf ( %d %f",&a1.pages,&a1.price);
And again to remind that you must be careful with size of string(char array) to avoid stack overflows.
Thanks
Alok.Kr.