Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Some books claim that they used ansi c and use turbo c compiler to run these example. i tried to run these on linux but I found that these example are only for windows.
#include<stdio.h>
#include<conio.h>
/* #include<dos.h> */
int main()
{
int a;
clrscr();
scanf("%d", &a);
printf("%d",a);
getch();
return 0;
}
Can I call the above example ansi c? why or why not?
As #milevyo said, those functions are implemented by Borland's compilers. On Windows you can replace clrscr() with system("cls") and getch(); with _getch(); or, even better, getchar().
conio.h file directory is only supported by Borland C.You can use getchar() instead of getch().
If you have to use getch() anyhow then You can use curses.h file instead of conio.h. It gives mostly all the functionality of conio.h with getch().
If you don't get curses.h directory installed then you can download from here
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr(); /* start the curses mode */
int a;
scanf("%d", &a);
printf("%d",a);
getch();
endwin();
return 0;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
In foo.c
void location(char *path)
{
//to do
}
In main.c
int main()
{
char foopath[256];
location(foopath);
printf("%s\n",foopath);
}
Maybe it will show /lib/foo.so
I think I can use shell script such as ldd to get the path, but it seems not pretty.
I want to read a file at the same location as the foo.so. So I need the correct path.
You can use the "dl" library. Example of program which displays the name of the dynamic library file of the "fopen" symbol:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
int rc;
Dl_info info;
rc = dladdr(fopen, &info);
if (rc) {
printf("%s\n", info.dli_fname);
return 0;
}
return 1;
}
$ gcc example.c -l dl
$ ./a.out
/lib/x86_64-linux-gnu/libc.so.6
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 5 years ago.
Improve this question
This is my code:
/* backtrace_foo1.c */
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define BACKTRACE() \
do {\
void *array[20];\
size_t size;\
char **strings;\
size_t i;\
size = backtrace(array, 20);\
strings = backtrace_symbols(array, size);\
for (i = 0; i < size; i++) {\
printf ("%s\n", strings[i]);\
}\
free (strings);\
} while(0)
void func1()
{
BACKTRACE();
}
void func()
{
func1();
}
int main(int argc, char **argv)
{
func();
return 0;
}
I compiled it by gcc -g -rdynamic and got
./a.out(func1+0x1f) [0x400905]
./a.out(func+0xe) [0x40097a]
./a.out(main+0x19) [0x400996]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x318ae1ecdd]
./a.out() [0x4007f9]
Then i use addr2line -e ./a.out -f 0x4007f9, i got
_start
??:0
This is my platform
gcc version 5.3.0 (GCC)
Linux 3.10.0_1-0-0-8
I shouldn't really answer this, since you don't really have a question or a stated problem. But sometimes I'm feeling nice...
While the code you write will start execution with the main function, the actual starting point is somewhere before that. There is startup code that will initialize the stdio system (stdin, stdout etc.) and initialize other things. This startup code then calls your main function like any other function.
The "problem" is that the startup code is not really part of your code, it's often a precompiled object file that the frontend program links your program with. And that object file probably doesn't have any kind of debug information, so you can't get any location information about it.
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
I am using Sublime to program in C but when the code is compiled it keeps returning [Finished in 1.1s with exit code 10]. What does that mean? What is going on?
#include <stdio.h>
void main()
{
int x;
scanf("%d", &x);
printf("%d", x);
}
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
printf("%d", x);
return 0;
}
main( ) does not take void as a return type.In C programming language we use int as a return type of main .Exit codes are nothing but Windows System Error Codes .I think you are using windows Operating system and for windows Operating system
exit code 10 means :The environment is incorrect.
May be your installation process was not correct .
Reinstall it and run the program .
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Using gcc I can remove comments and unwanted blank lines, but I want to reduce a size of file also, is there any options in gcc or any other tool to do so
At present I do like this
gcc -fpreprocessed -dD -E -P source_code.c > source_code_comments_removed.c
Here is scenario assume that this is my source_code.c
#include <stdio.h>
main()
{
// declar variable i
int i=0;
/* multiline comment
for loop
demo stuff
*/
for(i=1; i<=5; i++)
{
// just print something
printf("Hello %d \n",i);
}
}
I want to minify like this, removed comments and blank lines
#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}
Note : I am on Linux please don't suggest any windows based solution
sed -rb 's/ {6}//g' main.c |
sed -rb 's/\/\/.*$//g' |
tr -d '\n' |
sed -rb 's/\/\*.*\*\///g' |
sed -rb 's/(#include.*>)/\1\n/g'
will give you:
#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}
However, as stated in the in the comments, this doesn't make much sense and will not reduce the size of your compiled object file!
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 7 years ago.
Improve this question
I'm completely new to programming. I'm actually learning from a Harvard class on iTunes U. When trying to code along side the instructor I've ran into a problem. I can't run my .c program in terminal. I've tried the sudo commands, and I've searched with Google and I can't seem to find an answer, probably because I'm so new to programming. It's probably something I've overlooked or I just don't understand yet.
#include <stdio.h>
int
main(void)
{
printf("temperature in f: ");
float f = GetFloat();
float c=f / 9.0 * (f-32);
Printf("%. if F = %. if c\n", f, c)
I'm using Sublime text editor on a MacBook with Mac OS X Yosemite (10.10.x).
Your compiler should have warn you about some errors:
// string.c
#include <stdio.h>
int main(void) // There must be a return statement
{
printf("temperature in f: ");
float f = GetFloat();
float c = f / 9.0 * (f-32);
printf("%f if F = %f if c\n", f, c); // Missing ';' is the most common syntax error
return 0; // Here is the return
} // Do not forget to close your brackets
When you do:
gcc string.c -o myprogram
It will tell you what is wrong in your program. Once you have fixed all the errors you can run the program with:
./myprogram
Understand that you cannot run a C-file: the .c contains human-readable instructions for the machine. You have to compile it, i.e. translate it into a language that your computer will understand : it is roughly your compiled myprogram (and you do not want to open it to look what it contains, it will burn your eyes :p).
Just adding to the below answer your compiler should throw the below error also:
undefined reference to `Printf'
check case of your printf()