Scanf not behaving as expected - c

I am trying to use scanf() to input values to a structure using pointers.Can you help me to understand why my code is not working
This is my code:
#include<stdio.h>
struct student
{
int no;
float marks;
}st[2],*s;
main()
{
printf("enter the values");
for(s=st;s<st+2;s++)
{
scanf("%d%d",&s->no,&s->marks);
}
for(s=st;s<st+2;s++)
{
printff("%d\t%d\t",s->no,s->marks);
}
}
in this code scanf is not working properly,it is taking only the first value

You are using the wrong format specifier. %d is used for ints while %f is used for floats. Use
scanf("%d%f",&s->no,&s->marks);
and
printf("%d\t%f\t",s->no,s->marks);
instead as s->marks is a float, not an int. Using the wrong format specifier leads to Undefined Behavior.

#include<stdio.h>
struct student
{
int no;
float marks;
}st[2],*s;
main()
{
printf("enter the values");
for(s=st;s<st+2;s++)
{
scanf("%d%f",&s->no,&s->marks);
}
for(s=st;s<st+2;s++)
{
printf("%d\t%f\t",s->no,s->marks);
}
}
U have to use %f for float

In the structure you have taken two variable one is int type and other is float type but while doing scanf(taking input) you are doing %d for float whereas it should be %f for float .This is a very minor mistake but you should try to avoid it on your own by just looking at your code carefully :)
Happy coding

Related

trying to fill 2 colomns but keep getting "segmentation fault"

still new to algorithms in general, started with c, but stuck with the error segmentation fault couldn't really know whats the exact problem.
#include <stdio.h>
int main()
{
int i,nb;
float T[nb][2];
printf("how many mesures?\n");
scanf("%d",&nb);
for(i=0;i<nb;i++)
{
printf("whats the temperature T%d ?\n",i);
scanf("%f",&T[i][0]);
printf("whats the humidity H%d ?\n",i);
scanf("%f",&T[i][1]);
}
return 0;
}
This declaration of a variable length array
{
int i,nb;
float T[nb][2];
has undefined behavior because the variable nb is not initialized and has an indeterminate value. You have to declare the array after assigning a positive value to the variable nb.
int main()
{
int i,nb;
printf("how many mesures?\n");
scanf("%d",&nb);
float T[nb][2];
//...

C Insert int and doubles inside structure

I have a problem with a small project, I have to create a structure containing a person's data,I have to do data entry via a function using pointers.
I don't understand why when I try to enter the weight the program ends with this error:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
I made an example of the code that is giving me problems:
typedef struct{
char name[DIM];
char surname[DIM];
int height;
float weight;
}Record;
int main() {
Record subj;
insRecord(&subj);
}
void insRecord(Record *subj){
printf("\nName ");
scanf("%64[^\n]s", subj->name);
cleanBuffer();
printf("\nSurname ");
scanf("%64[^\n]s", subj->surname);
cleanBuffer();
printf("\nEight ");
scanf("%3s", subj->height);
cleanBuffer();
printf("\nWeight ");
scanf("%6s", subj->weight);
cleanBuffer();
}
int cleanBuffer(){
int cont= 0;
char c;
do{
cont++;
c = getchar();
}while(c != '\n');
return cont;
}
(I only wrote the main functions).
Furthermore, compiler is giving warnings on height and weight scanf() statements:
Format specifies type 'char *' but the argument has type 'int' (for height)
Format specifies type 'char *' but the argument has type 'double' (for weight)
Could you tell me how can I solve?
PS the project is divided into several files (3 files).
In main.c I wrote the insRecord() function, in struct.h I wrote the Record structure, in struct.c I wrote cleanBuffer() function.
You are reading a string (%s) and try to store that in a float. If you use the flag %f for weight and %d for height it will probably work better. Remember that you should provide a pointer to the float and int that you want to store the data in, you are not doing that in your example.
Have a look at the scanf documentation:
https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
Good luck

Program crashes when reading first data point

I want to see what student have the best mark in the class.
So I make this ,but i don't know where I am wrong
struct Date {
char name[31];
float mark;
};
struct Date * Read(unsigned int n,struct Date *d){
int i;
for(i=0;i<n;i++){
getchar();
fgets(d[i].name, 31, stdin);
scanf("%f",d[i].mark);
}
return d;
}
int main(){
unsigned int n;
struct Date *d;
scanf("%u",&n);
d = (struct Date*) malloc(n*sizeof(struct Date));
d=Read(n,d);
free(date);
return 0;
}
after i read the mark the program crash.
Can someone help me and explain what to change?
Thanks a lot.
The crash is most likely due to this:
scanf("%f",d[i].mark);
You should pass the address as argument to read a float value. It should be:
scanf("%f", &d[i].mark);
Technically, this is undefined behaviour..
Compile with all warnings enabled. gcc warns even without any specific options:
warning: format %f expects argument of type âfloat *â, but argument 2 has type double [-Wformat=]
scanf("%f",d[i].mark);

Why this program stops unexpectedly in the middle of the input

i have written this code for a question on codechef (A4)....when i give the input:
2
4 2
This program stops unexpectedly without taking further input ....can some please point out the mistake in the code?
#include <stdio.h>
#include<math.h>
void number(long int a,int b)
{
int c;
c=b;
int first[c],last[c],e=1,i;
long int d;
d=pow(a,a);
for(i=(c-1);i>=0;i--)
{
last[i]=fmod(d,pow(10,e));
e++;
}
e=1;
while(d>pow(10,(b-1)))
d/=10;
for(i=(c-1);i>=0;i--)
{
first[i]=fmod(d,pow(10,e));
e++;
}
for(i=0;i<c;i++)
printf("%d",first[i]);
printf(" ");
for(i=0;i<c;i++)
printf("%d",last[i]);
printf("\n");
}
int main()
{ int T;
scanf("%d",&T);
while(T--)
{ long int a;
int b;
scanf("%ld %d",a,b);
number(a,b);
}
return 0;
}
scanf("%ld %d",&a,&b);
Using uninitialized variables lead to UB. You should use &a and &b to scan variables
In your code you have
scanf("&ld %d",a,b);
It means you're trying to input integers to the memory locations of values of a and b. For and example let value of a = 1234566466 (long int), and b = 1234 (int). Accordingly 1234 is a memory location which is at the start of the RAM. Tn that area System files are loaded. So you are going to change system behaviour. That is not allowed.
Further when the complier allocate some memory space to your program, you can access only the memory which is inside your memory segment directly. But above statement trying to access another segment.
That's why you get segmentatin fault.
You are passing an integer to a function that expects a pointer, for scanf the "%d" and "%ld" specifiers expect int * and long int * respectively, and you pass int and long int, so when trying to access the integers as if they were memory addresses the segmentation fault occurs.
The correct way to call scanf would be as Gopi said
scanf("%ld %d", &a, &b);
there you pass a and b addresses instead of their values.

C Programming Struct

#include<stdio.h>
#include<string.h>
struct s {
char ch[20];
float a;
};
int main()
{
struct s p[10];
int i;
for(i=0;i<10;i++)
{
scanf("%s%f",p[i].ch,p[i].a);
}
}
What is wrong with this code?
Its giving runtime error.
What's the problem?
Most of the errors come from this line.
scanf("%s%f",p[i].ch,p[i].a);
You should use the address of p[i].a, and also restrict the numbers of chars to write in p[i].ch.
scanf( "%19s%f", p[i].ch, &p[i].a );
I haven't touched C code for a while but shouldn't it be something like
scanf("%s%f",p[i].ch,&(p[i].a));
(You have to give the memory address of the variables to the scanf function.)
At the line:
scanf("%s%f", p[i].ch, p[i].a);
You are using p[i].a as a float* (pointer), while it's a float. You're invoking undefined behavior. You probably wanted to do it like this:
scanf("%s%f", p[i].ch, &p[i].a);
Change your code like this:
#include <stdio.h>
#include <string.h>
struct s {
char ch[20];
float a;
};
int main(){
struct s p[10];
int i;
for(i=0;i<10;i++){
scanf("%s%f",p[i].ch, &p[i].a);
}
}
Note that variable a is a float type; you need to pass its memory address when using scanf.
I think the problem is in the p[i].a parameter;
use &p[i].a instead.

Resources