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 ?
Related
I have a problem idk why jump from scanf name to scanf age, why dont let me put the address?
I already try only with %s instead of %[^\n]s
#include <stdio.h>
struct contact
{
char name[40];
char address[80];
int age;
long phone;
};
typedef struct contact contact_L;
int main(void)
{
contact_L c1;
printf("Enter Name:");
fflush(stdin);
scanf("%[^\n]s",c1.name);
printf("Enter Address:");
fflush(stdin);
scanf("%[^\n]s",c1.address);
printf("Enter Age:");
fflush(stdin);
scanf("%d",&c1.age);
printf("Enter Phone:");
fflush(stdin);
scanf("%ld",&c1.phone);
printf("Name: %s \n",c1.name);
printf("Address: %s \n",c1.address);
printf("Age: %d\n",c1.age);
printf("Phone: %ld\n",c1.phone);
return (0);
}
The output look like this ( don't let me enter the address and jump to the age )
Enter Name:jose herrera
Enter Address:Enter Age:31
Enter Phone:4567890
//printf
Name: jose herrera
Address:
Age: 31
Phone: 4567890
try
scanf(" %s",c1.address);
While Taking address input. With a preceding space
I'm new to C and i currently studying about pointer and struct. But it seems like i have a problem when assigning value into my struct.
This is my code:
#include <stdio.h>
typedef struct
{
char name[30];
int age;
int birth;
}
student;
void record(student *sp);
int main(void)
{
student std1;
record(&std1);
printf("%i, %i %s\n", std1.birth, std1.age, std1.name);
}
void record(student *sp)
{
printf("Name: ");
scanf("%s", sp -> name);
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}
Run program:
./struct
Name: David Kohler
result:
Birth: Age: 0, 0 David
What i don't understand is when i'm going to assign name into sp->name it immediatly print an unexpected result like that. It's doesn't prompt to enter age and birth.
But when I ran like this, it works:
./struct
Name: Kohler
Birth: 1997
Age: 22
1997, 22 Kohler
So, what do you guys think happen? It seems like it doesn't took very well when i'm entering a full-long name like "David Kohler" instead just "Kohler".
What's the solution if i want to enter a full name? Do i need to use malloc? Thank you.
Format specifier %s skips spaces. You can use fgets() or modify your scanf() format specifier, as Jabberwocky pointed in the comments.
fgets:
void record(student *sp)
{
printf("Name: ");
fgets(sp->name,30,stdin);
strtok(sp->name,"\n"); /* Removing newline character,include string.h */
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}
Note that with fgets you also get a newline character in your buffer.
Scanf:
void record(student *sp)
{
printf("Name: ");
scanf("%29[^\n]", sp -> name); /* Added a characters limit so you dont overflow */
printf("Birth: ");
scanf("%i", &sp -> birth);
printf("Age: ");
scanf("%i", &sp -> age);
}
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);
}
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.
/* 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.