Newbie compiler problems [closed] - c

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.

Related

How to clear terminal in C without using #include? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to clear terminal in C without using #include.
Here is my code:
int printf(const char *format, ...);
int main()
{
printf("%c2J", 27);
}
You likely won't be able to clear a terminal without using functions in the standard library, but probably the closest thing would be to use ANSI escape sequences, which might work depending on your terminal.
You'll have to output \x1b[2J (probably followed by \x1b[H). These are the ANSI terminal escape sequences for clearing the screen and repositioning the cursor in the upper left "home" position, respectively.
#include <stdio.h>
int main(void) {
printf("\x1b[2J\x1b[H");
return 0;
}
Please note that even if you specify the function prototype for printf(), as you have done above, you are still using the standard library and not making your own. I can only think of one reason you'd ever want to do that: to make a quine fit on one line.
If you want to write your own function you should not be using the standatd library. Invlude does not matter as .h files are not libraries. You should compile the code with -nostdlib option and write your own function.
In your code you simply use the standard library function - not your own one.

Visual Studio - C programming [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Total NOOB here.
I've noticed that under Visual Studio common C commands are a bit different,
the only example I can think of right now is that VS insists on using scanf_s instead of scanf, but I'm sure there are other quirks.
what I'm asking is, should I be learning, or even working with C on VS and why?
Thanks
MSVC does not insist but only issues warnings, to provoke you into using their own non-standard functions. The warning message from MSVC tells you what to do:
warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use
_CRT_SECURE_NO_WARNINGS
which means putting #define _CRT_SECURE_NO_WARNINGS before any #include files.
I put the following lines before any #include
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <stdio.h> // etcetera
Anyway MSVC does not comply with C standards.

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.

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