I cannot print float variables when calling my functions. int variables print but floats won't print their proper value when passed to my function.
I tried to change the float to int and that worked
int main() {
int foo = 6;
call(foo);
}
void call(int bar) {
printf("%d", bar);
}
This worked and it does indeed print 6.
But, doing the same but with floats only prints out 0.00000:
int main() {
float foo = 6;
call(foo);
}
void call(float bar) {
printf("%f", bar);
}
How do I correctly call and then print float variables?
you need a forward declaration of call
void call(float integerrrr);
int main(){
float integerrrr=6;
call(integerrrr);
}
void call(float integerrrr){
printf("%f", integerrrr);
}
your compiler probably warned you about this
You could simply define call above main instead of below it. The compiler must have seen the declaration of functions when they are used, so a forward declaration like pm100 suggests is one way. Moving the whole definition above main is the another (that does not require a forward declaration).
#include <stdio.h>
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(void){
float integerrrr = 6;
call(integerrrr); // now the compiler knows about this function
}
INT type variables i can print but float just does not
If your program actually compiles as-is it will use an old (obsolete) rule that makes an implicit declaration of undeclared functions when they are used. The implicit declaration would be int call(); - which does not match your actual function. The program (even the one that seems to be working) therefore had undefined behavior.
the compiler of c work from top to bottom so line by line,
so u will have to call the first function void call() then your main function:
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(){
float integerrrr=6;
call(integerrrr);
}
Related
I want to pass a float number to another function.
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number)
}
fun(float star, float ste, int numbe)
{
//here I get "star = 0", "numbe = 10", but "ste = -1.084264e-19"
}
what is wrong here?
Thank you
It is most certainly possible to pass a float to a function, as long as you provide a prototype or move the definition ahead of the first use of the function.
A prototype (also called a forward declaration) looks like this:
void fun(float star, float ste, int number);
In larger projects, prototypes go into header files.
Note: do not forget to add void in front of the function definition as well. Otherwise, the compiler treats your function as returning an int.
If you omit the prototype, the compiler will default to using the old K&R C rules to decide how to pass arguments to your function. The result is the unusual behaviour you are seeing, and hopefully a compiler warning as well.
You need a correct function declaration before your function call:
Add:
void fun(float star, float ste, int numbe);
before your main declaration and also add void return type in your fun function definition.
you need to declare the function before main function.
Try this:
void fun(float star, float ste, int number);
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number);
}
I'm only new in C Programming and I'm trying to learn the language. but when I compile the code I made it show an error "FuncA was not declared in this scope". but I already try to declared the function below.
#include<stdio.h>
int main(){
int A = 1;
FuncA(A);
printf("%d\n");
}
int FuncA(int B){
B++;
return B++;
}
sorry for this question.
You need to put its declaration:
int FuncA(int B);
before main().
Alternatively, you can move main() after the function definition.
P.S.: As #JonathanLeffler commented, printf("%d\n") is undefined behavior:
If any argument is not the type expected by the corresponding conversion specifier, or if there are less arguments than required by format, the behavior is undefined. If there are more arguments than required by format, the extraneous arguments are evaluated and ignored.
You probably want this:
printf("%d\n", FuncA(A));
At the point when FuncA is called, it is not yet known by the compiler.
Move the function as shown:
int FuncA(int B){
B++;
return B++;
}
int main(){
int A = 1;
A = FuncA(A);
printf("%d\n", A);
}
Also note that you specify in printf to print an integer %d, but you dont pass one. My code above has this fixed.
Also you probably wanted to do something with the return value of FuncA, I assigned it to A.
just declare a function prototype in the global declaration:
int FuncA();
My purpose is to create an array in a function and then return it to the main function.
I was recommended to allocate the array on the heap in the function (otherwise popped from the stack as soon as its returned).
The code is shown below:
#include <stdlib.h>
int main() {
double *v1 = myFunction();
return 0;
}
double *myFunction() {
return malloc(10*sizeof(double));
}
When I compile this code with gcc I get the following error-message:
..\src\main3.c:38:9: error: conflicting types for 'myFunction'
What is wrong with this code? Why do I get this compilation error?
Provide myFunction()'s prototype before using it, that is before main() like so:
double * myFunction();
int main(void)
{
...
If not doing so, the compiler assumes type int for myFunction() when seeing it for the first time.
Than later it finds it to be declared as double * (being different from int) which then provokes the error.
Turning on all warnings would have pointed you to using a function without prototype. Use -Wall -Wextra -pedantic to turn on most of gcc's warnings.
Another issue is that declaring a function without any arguments specified leaves it open what to pass on calling the function:
double * myFunction();
If a function should be specfied to not have any arguments specify void as argument:
double * myFunction(void);
The same for how your code defines main(). It shall be:
int main(void);
or either be
int main(int argc, char ** argv);
1) Add prototype of myfunction at the starting like following or
double *myFunction();
int main() {
double *v1 = myFunction();
return 0;
}
double *myFunction() {
return malloc(10*sizeof(double));
}
2). or define the functionBody before it is called
double *myFunction() {
return malloc(10*sizeof(double));
}
int main() {
double *v1 = myFunction();
return 0;
}
However, first solution is preferable.
Your code is almost fine, there's only one little but blocking error, you are trying to use myFunction before the symbol is defined, all you have to do is :
#include <stdlib.h>
double *myFunction() { // myFunction should be declared before it's used
return malloc(10*sizeof(double));
}
int main() {
double *v1 = myFunction();
return 0;
}
The the open C book states "All identifiers in C need to be declared before they are used. This is true for functions as well as variables." the beauty in C is that you can do can do amazing stuff like this piece of code, but you should follow some guidelines, read the open C book!.
Cheers.
Try this..
return (double*) malloc(10*sizeof(double));
I am learning C and I am studying functions. So, I read that when I implement my own function I have to declare it before the main(). If I miss the declaration the compiler will get an error message.
As I was studying this example (finds if the number is a prime number),
#include <stdio.h>
void prime(); // Function prototype(declaration)
int main()
{
int num, i, flag;
num = input(); // No argument is passed to input()
for(i=2,flag=i; i<=num/2; ++i,flag=i)
{
flag = i;
if(num%i==0)
{
printf("%d is not prime\n", num);
++flag;
break;
}
}
if(flag==i)
printf("%d is prime\n", num);
return 0;
}
int input() /* Integer value is returned from input() to calling function */
{
int n;
printf("\nEnter positive enter to check: ");
scanf("%d", &n);
return n;
}
I noticed that a function prime() is declared, but in the main, a function, input(), is called and also the function input() is implemented at the bottom. Ok, I thought it was a mistake and I change the name from prime to input.
However if I delete the declaration and I don’t put any there, the program is compiled without errors and it runs smoothly. (I compile and run it on Ubuntu.)
Is it necessary to declare a void function with not arguments?
If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept any arguments (as Bartek noted in the comment).
For this function, the implicit declaration matches the real declaration, so you don't have problems. However, you should always be careful about this, and you should always prefer forward declarations instead of implicit ones (no matter if they are same or not). So, instead of just having forward declaration of the void prime() function (assuming that you will use it somewhere), you should also have a forward declaration of int input().
To see how can you pass any number of the arguments, consider this:
#include <stdio.h>
// Takes any number of the arguments
int foo();
// Doesn't takes any arguments
int bar(void)
{
printf("Hello from bar()!\n");
return 0;
}
int main()
{
// Both works
// However, this will print junk as you're not pushing
// Any arguments on the stack - but the compiler will assume you are
foo();
// This will print 1, 2, 3
foo(1, 2, 3);
// Works
bar();
// Doesn't work
// bar(1, 2, 3);
return 0;
}
// Definition
int foo(int i, int j, int k)
{
printf("%d %d %d\n", i, j, k);
return 0;
}
So, inside the definition of the function you're describing function arguments. However, declaration of the function is telling the compiler not to do any checks on the parameters.
Not declaring a prototype and relying on default argument/return type promotion is dangerous and was a part of old C. In C99 and onward it is illegal to call a function without first providing a declaration or definition of the function.
my question is, is it necessary to declare a void function with not arguments?
Yes. For this you have to put void in the function parenthesis.
void foo(void);
Declaring a function like
void foo();
means that it can take any number of arguments.
If prime is not used, then omit the declaration.
The code won't compile as C++, because the compiler would complain that function input is used but not declared. A C compiler might issue a warning, but C is more relaxed and does an implicit declaration of input as int input() which means that you can pass any value to input and input returns an int.
It is good style to always provide a function declaration before using the function. Only if you do this the compiler can see if you are passing too few, too many or wrongly typed arguments and how to correctly handle the return value (which might be short or char instead of int).
Anybody please elaborate these error:-
void main()
{
int a=5, b=60, func();
printf("\nI am in main-1");
int func(){
printf("\nI am in funct");
return 1;
}
func();
printf("\nI am in main-2");
}
The errors I get are:
In function 'main':
Line 8: error: static declaration of 'func' follows non-static
declaration
Line 4: error: previous declaration of 'func' was here
Line 3: warning: return type of 'main' is not 'int'
I think C allows nested class because the following code is working fine:
void outerfunc()
{
int func()
{
printf("\nI am in funct");
return 1;
}
func();
}
void main()
{
printf("\nI am in main-1");
outerfunc();
printf("\nI am in main-2");
}
You are using an extension of the GNU C Compiler which allows the declarations of nested functions. The error comes from the fact, that forward declarations of nested functions under GCC's extension need to be prepended with the auto keyword.
int a=20,b=11;
int main()
{
int a=5, b=60;
auto int func(); // <--------- here
func(); // <- call it
printf("\nI am in main-1");
int func(){
printf("\nI am in funct");
return 1;
}
printf("\nI am in main-2");
return 0;
}
See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html for more details.
ANSI C doesn't allow nested function definition. And your main function should return int.
Nested functions are not allowed in standard C/C++. Simply (forward) declare the func() inside main() if you want to define it later on.
int main()
{
int a=5, b=60, func();
printf("\nI am in main-1");
int func(); // <---- declare inside main()
printf("\nI am in main-2");
}
int func(){ // <---- define later
printf("\nI am in funct");
return 1;
}
What you are taking about is a GCC specific feature, its never been a "proper" C feature (i.e. part of the ANSI C specification).
If you want to use this feature then I believe what you are after is this:
#include <stdio.h>
int a = 20, b = 11;
int main( int argc, char* argv[] )
{
int a = 5, b = 60;
auto int func( void );
printf("\nI am in main-1");
int func( void )
{
printf("\nI am in funct");
return 1;
}
printf("\nI am in main-2");
return func();
}
The reason why your previous code didn't work is because nested functions have no linkage:
A nested function always has no linkage. Declaring one with extern or static is erroneous. If you need to declare the nested function before its definition, use auto (which is otherwise meaningless for function declarations).
The above sample uses the auto keyword thusly. I've also taken the liberty of fixing your main declaration :-)
Nested functions are a gcc-specific extension; they are not universally supported.
As far as the warning about main, the standard signatures for main are
int main(void)
int main(int argc, char **argv) // or equivalent
An implementation may provide additional signatures (some compilers allow a third parameter for environment variables), but those additional signatures must be documented by the implementation; IOW, void main() is only a valid signature for main if your compiler documentation explicitly lists it as such.
When in doubt, use one of the standard signatures above.
You haven't defined func before calling it.
Relates back to the original line.
You aren't returning int.
It works if you remove the declaration of func() in the int variable declarations.
C++ does not allow functions to be included inside other functions.
Attempting to do so in VS 2010 gives:
'funct' : local function definitions are illegal
You need to move that function and it's declaration outside of main.