float with array of structures? [duplicate] - c

This question already has answers here:
scanf: floating point format not linked [duplicate]
(3 answers)
Closed 8 years ago.
I am getting a runtime error while running the following code.
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal);
return 0;
}
Runtime error is floating point formats not linked.
some body please help me to run this program.

Add the following anywhere outside main after all the includes.
void dummy(float *a) {
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
You need not call this function

Related

Function to print the members of each struct in an array of structs in C [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
I'm trying to write a function in C that takes a struct array (of structs that hold employee information) as an argument and prints each member of each struct. For some reason my code isn't producing any output. Could anyone tell me where I am going wrong? Thanks.
Here it is:
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee
{
int number;
char name[20];
char department[15];
double salary;
}Employee;
void employeePrint(Employee arr[]);
main()
{
Employee e1 = {101,"John Smith\0","Accounting\0",54926.25};
Employee e2 = {102,"Jane Q. Public\0","Retail\0",54926.24};
Employee e3 = {103,"George Washington\0","Tech\0",70417.76};
Employee empArr[3] = {e1,e2,e3};
employeePrint(empArr);
}
void employeePrint(Employee arr[])
{
int i;
for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
{
printf("Number: %-4d\n",arr[i].number);
printf("Name: %-6s\n",arr[i].name);
printf("Department: %s\n",arr[i].department);
printf("Salary: $%-2.2lf",arr[i].salary);
printf("----------------");
}
}
The problems are here: void employeePrint(Employee arr[]) and for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i). When you pass an array to a function like that, it decomposes to a pointer. Pointers don't store the size of the original array anymore, so sizeof(arr) is giving you the length of a pointer, which is smaller than your struct, so the division rounds to 0. To fix the problem, pass the size as another parameter, or use some sort of sentinel to know when to stop looping.
replace:
void employeePrint(Employee arr[])
with:
void employeePrint(size_t len, Employee arr[len])
then pass the length of the array in the function call, it should produce what you are looking for.

error: conflicting types for 'components' [duplicate]

This question already has answers here:
conflicting types error when compiling c program using gcc
(3 answers)
cant understand how to return string from function in C
(2 answers)
Getting "conflicting types for function" in C, why?
(11 answers)
Closed 5 years ago.
Can someone point out what i am doing wrong, i am very new to c programming.
I keep getting conflicting types for 'components' error.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float Vo,ang;
printf("Enter Velocity:");
scanf("%f",&Vo);
printf("Enter Angle:");
scanf("%f",&ang);
printf("The x component is %f and the y component is %f",components(Vo,ang));
}
float components(float Vo, float ang,float Vx, float Vy)
{
Vx=Vo*cos(ang);
Vy=Vo*sin(ang);
return Vx,Vy;
}
I would appreciate any help.
Move the definition of the called function before the place where you call it.
Or provide a prototype before main(), i.e. insert
float components(float Vo, float ang,float Vx, float Vy);
before
int main()
That way the compiler does not have to guess the type involved in calling that function. Since the compiler guesses incorrectly, it later complains about the types not matching. Changing the order of what the compiler sees when fixes that problem.
Also,
components(Vo,ang)
among your values provided to printf only gives one value, while printf() with your format string expects two.
You need to write the two separate values into two variables, which then are given to printf. You can for example give two more parameters to the function, pointers to suitable variables.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void components(float Vo, float ang, float* outpVx, float* outpVy)
{
*outpVx=Vo*cos(ang);
*outpVy=Vo*sin(ang);
}
int main(void)
{
float Vo,ang;
float outVx=0.0;
float outvy=0.0;
printf("Enter Velocity:");
scanf("%f",&Vo);
printf("Enter Angle:");
scanf("%f",&ang);
components(Vo,ang, &outVx, &outVy);
printf("The x component is %f and the y component is %f", outVx, outVy);
return 0;
}

C programming structure [duplicate]

This question already has answers here:
What happens if I define a 0-size array in C/C++?
(8 answers)
Closed 5 years ago.
#include <stdio.h>
int main () {
struct Record {
int employeeNumber;
char employeeName;
float salary;
int yearsServiced;
} record[5];
struct record[0] = {46723, "Fattah", 4550.00, 8};
printf("TheEmployee number is %d", record[0].employeeNumber);
}
Why my program cannot run? please help. Thanks for advance.
struct record[0] declares an array of size 0. You intend to initialize the first element of the array and you confuse declaring and indexing:
struct Record myRecord[1] = {46723, "Fattah", 4550.00, 8};
This declares an array of size 1 and initializes the first element with the given values.
Firstly, by struct record[0] you are declaring an array of size 0, which you need to change to struct record[1].
And as far as your doubt regarding casting, you should read Struct initialization of the C/C++ programming language?
Lastly , I don't think it is the reason of your error but why are you trying to assign "Fattah" to a variable of type char.
You declared employeeName as char but you pass in a string. employeeName needs to be a pointer on char.
char *employeeName;
#include <stdio.h>
int main () {
struct Record {
int employeeNumber;
char *employeeName;
float salary;
int yearsServiced;
} record[5];
record[0].employeeNumber = 46723;
record[0].employeeName = "Fattah";
record[0].salary = 4550.00;
record[0].yearsServiced = 8;
printf("TheEmployee number is %d", record[0].employeeNumber);
}

Why the following code gives "hello5" as output in C? [duplicate]

This question already has answers here:
what is the return type of printf [closed]
(2 answers)
Closed 7 years ago.
Follwing code in C gives the output "hello5" ...how?
#include<stdio.h>
int main(){
int f = fun();
printf("%d",f);
return 0;
}
void fun(){
printf("hello");
}
What you see is undefined behavior.
The value f is never initialized and you are printing uninitialized variable which will lead to undefined behavior.
printf() returns number of characters successfully printed out so the count here is 5(hello) for printing hello.
You need to return this value if you want then you have defined behavior because you are initializing the variable f in main()
int func()
{
int j;
j = printf("hello");
return j;
}

Simple cube and square sum function won't run (C) [duplicate]

This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 4 years ago.
Learning C, trying to code a program that outputs the sum of the cube and square of an inputted number.
#include <stdio.h>
main()
{
int a;
scanf("%d",a);
printf("%d",cube(a)+sqr(a));
}
cube(int x)
{
return(x*x*x);
}
sqr(int arg)
{
return(arg*arg);
}
When I run the program it outputs some seemingly random string of numbers after I input a number. Any way to fix it without changing the usage of returns to assign variables?
scanf needs a pointer:
scanf("%d",&a);
instead of
scanf("%d",a);
int a;
scanf("%d",a);
^
must be &a. d conversion specifier for scanf expects a pointer to int argument.
You need to define a return type to your functions !
#include <stdio.h>
int main()
{
int a;
scanf("%d",a);
printf("%d",cube(a)+sqr(a));
return 0;
}
int cube(int x)
{
return(x*x*x);
}
int sqr(int arg)
{
return(arg*arg);
}
You have not given address of "a" in scanf function.
Please use:
scanf("%d", &a);

Resources