C power function and scanf [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So basically, I need to get an input and square it. some reason the pow() function isn't working.
#include <stdio.h>
#include <math.h>
int main (){
int in, ans;
scans("%d", &in);
ans = pow(in, 2);
printf("answer is: %d", ans);
return 0;
}
The error is :
undefined pow or something like that

This type of error occur when you forgot to link your programme to the math library .
use -lm for linking to math library.
gcc yourfilename.c -lm
to use the math library

I think you have compiled on Linux platform. So, use -lm option with command.
gcc yourfile.c -lm

You are using scans instead of scanf. It should scanf("%d",&in);

Related

Works with one compiler, but not with other [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to code a very simple program using C language.
I'm using Visual Studio and CodeBlocks to make it compiled.
But this code:
#include <stdio.h>
#include <stdlib.h>
int main() {
float a;
printf("Give a number: \n");
scanf("%f", &a);
float b = a * 2;
printf("The result is: %f", b);
}
It works using CodeBlocks, but not with Visual Studio.
Could you please give me a hint?
Severity Code Description Project File Line Suppression State
Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Variables c:\users\PinkP\documents\visual studio 2015\projects\variables\variables.c 32
scanf("%f", &a); is considered unsafe by the Visual Studio compiler and they rather cheekily schedule the function for deprecation. (Although ISO have no such plans.) Really they want you to use scanf_s instead, as that's less likely to mess up your memory.
You can disable this warning by following the compiler message: i.e. #define _CRT_SECURE_NO_WARNINGS.
The irony here is the compiler permits your non-standard main() function without a twitch! Do consider using the more portable
int main()
which has been required since and including C99.
Visual Studio 2015 is stricter about deprecated functions than 2010 was.
Rather than just advising you that you need to do something about your use of scanf, as Visual Studio 2010 did, it now FORCES you to do something about it.
You need to switch to scanf_s or use _CRT_SECURE_NO_WARNINGS.

how do I solve build and run error in this code of C using CodeBlocks? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to programming world and started with C and i want to build and run this code but it says error or multiple main function, the code is:
#include <stdio.h>
int main()
{
int age;
printf("I got %d%% on my C exam\n",98);
return 0;
}
You probably created a default project with an existing C source file containing a main, then you probably added a new file to type your code that contains a main, and then the IDE complains when compiling because you have two mains. Remove the default file from the project.

No sound with \a in cygwin [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm doing some coding using cygwin with c programming. There is no sound with \a. Do I have to install some packages? There is no problem with cl compiler.
#include <stdio.h>
int main()
{
printf("\a");
return 0;
}
with the following setting
gcc test.c -o fun.exe
I am going to guess that it has something to do with the terminal emulator you are using. I am using mintty. When I edit the options, I am able to specify the behavior of "Bell".
When uncheck "Sound" I don't hear anything. When I check "Sound", I hear a beep.

Newbie compiler problems [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm really new in C and I have the Code Blocks compiler. When compiling a really simple program. Sometimes it works but sometimes it doesn't. I'm guessing it has something to do with the things I select when starting a new project. I usually select a console application C language and I have no idea what to select next does anyone know whats going on and how to fix it? This is my code:
#include <stdio>
int main()
{
printf ("hello world !");
getchar ();
return 0;
}
#include <stdio.h> and not #include <stdio>.
Header files in C are saved with .h file extension.
headers in C, by convention, end with .h.
so it should be #include <stdio.h> instead of #include <stdio>.
However, if you are using a C++ compiler, with your file being called foo.cpp, then you can use #include <cstdio> and add using namespace std; to use getchar() function.

C program without a main function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any way to write a C program without a main function? If so, how can that be achieved?
C defines the entry point in a hosted environment to be main. In a "freestanding" environment, however, the entry point can have some other name. That's about the only latitude the language (at least officially) allows in that particular respect.
Yes, you can.
_start function is the entry point of a C program which makes a call to main().
Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main(), a process executes a bulk of code to "clean up the room for execution".
_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.
You can override _start and tell the compiler to not to look for main() by using "-nostartfiles" option.
#include <stdio.h> //for using printf()
_start()
{
printf("Hello world!!\n");
_exit(0);
}
To Compile : gcc -nostartfiles code.c -o a.out
Also look at http://linuxgazette.net/issue84/hawk.html for more basic information.
The following linker abuse
char main[] = { /* Machine code for your target implementation */ };
will work on some platforms.
No. C is totally based off of the assumption that you start the program in main(). Anyway, why would you want this? This would make inconsistencies for other programmers reading your code.
Maybe this could work:
http://www.gohacking.com/2008/03/c-program-without-main-function.html
An alternative is to write a C program and look at the Assembly output:
http://users.aber.ac.uk/auj/voidmain.shtml
More information about what happens before main() is called can be found here (How Initialization Functions Are Handled):
http://gcc.gnu.org/onlinedocs/gccint/Initialization.html

Resources