system("cls"); not working in C language - c

system("cls") is no working in C language . I added conio.h header file but it always say cls not found. I am using xcode. But same code executes perfectly on visual studio.

That's because system("cls"); has nothing to do with the c language.
The conio.h header is as far as I know an old MS-DOS header, you can't use it portably. The system() function executes external programs from within a c program, cls is an MS-DOS program to clear the text buffer of a MS-DOS console.
In your picture, it's clear that you are not executing the program in an MS-DOS console so it wont work.
Using external programs is almost always a bad idea, except if those programs are guaranteed to be installed with your's1. The reason is that any program that relies on other programs being available in the target environment, will fail when the external programs aren't.
I understand that it's easy to see a lot of code using non standard tricks like system("cls"), but if you find good learning resources that will not be the case. Try to study each and every function you learn and determine whether or not it's a standard function and a good practice to use it the way you see it.
1TeX distributions work like that, they are just several programs that exchange text, following very closely the UNIX Philosophy. But they are all distributed together.

You are trying to run cls command (to clear the screen) on the console using the system(). cls command only exists on DOS or command prompt on Windows.
system("cls");
If your program is running on Bash on Linux or MacOSX, you can try clear.
system("clear");

It's not working because the system() is a library function of stdlib.
You need to use #include<stdlib.h> in order to use system("cls") in C.

Related

Is there an equivalent of getch() function in Mac?

I am not able to find the equivalent header file for conio.h (for C programs) in Mac.
I was wondering if there is any option for getch() function in Mac?
I want to use it such that user can give options and program will go forward without pressing enter.
You probably want to look at ncurses. It has a lot in common with conio. You can get its documentation with "man ncurses".

Is it possible to add a getchar(); equivalent to a .o file?

I created a simple C program a while ago. It's a simple command-line generator that takes some number, prints the results and stops. I always ran it in the editor's command line enviroment that automatically paused after the program ran, so I omitted adding a getchar() at the end.
I now regret this, because I managed to lose the source. All I have now is the complied .o and .exe file, and the latter - of course - exits immediately after it prints the output, so it's unusable. It wasn't that long, about 100 lines, but I'd like to avoid rewriting it. (Also, I might even learn something new from this way.)
Now I have very basic knowledge of C, and about zero on computer-degree x86 assembly (though I learnt the basics of 8086-assembly for microcontrollers, it won't be that helpful now I guess), so I'm kinda stuck here. Can I either add that getchar() like pausing function to the complied code, or is there any way I can make that .exe stop before exiting while still keeping it standalone?
The program will run on a Windows 10 system.
I would write some sort of batch script in which you call your program and then just run pause, which waits for you to hit a key before it continues.
wrapper.bat:
yourprogram.exe
pause
Of course you can disassemble your executable into raw x86 assembly code, then look up the code for a simple getchar() on Windows, add that and reassemble. However, it would probably be less time consuming to rewrite the program, depending on how complex it was or just create a wrapper batch-script.
It's possible to hijack .o file, you can even do it with .exe, .dll, ... but it's not simple and requires a lot of know-how. What I would suggest is to use some sort of decompiler to try to restore the original source code, make the change and compile it again. You can find suggestions of decompilers in this old answer.

how to write unix "time" like utility

I am new to unix and learning to write some c programs that we can execute using gcc compiler in ubuntu. question:I need to write something similar to this: "time ls" where time should be replaced by my program. I know how to write c program for this, however, I cannot understand how unix will figure out what to execute if I replace time with my utility lets say "mytime" for instance? Some background for this will really help
Read some good Linux programming book, perhaps ALP - a bit old, but freely downloadable.
Read also intro(2) & syscalls(2).
For time related stuff, start with time(7). It explains that there are several notions of time. Then consider time(2), gettimeofday(2), getrusage(2), clock_gettime(2), times(2), localtime(3), strftime(3) etc...
Notice also that time(1) is either a builtin command of your shell, or an external one in /usr/bin/time. So it is some free software, whose source code you could download and study.
I cannot understand how unix will figure out what to execute
Be aware of the PATH variable (see also environ(7)), used by shells and in execvp(3). You could set your PATH to suit your needs. You might also be interested by strace(1) to understand what system calls a command or a process is doing. Notice that shells are ordinary programs, and you can write your own one (and that is a very useful exercise). Most shells are free software whose source code you can study. sash is a very simple shell...

Running the Hello World C code from command prompt?

I have written a very simple C program to print "Hello World" in my Notepad text editor and saved it as test1.exe. I opened my cmd and ran the file test.exe and the received error is as follows:
The NTVDM CPU has encountered an Illegeal instruction. CS:0607
IP:0103 OP:63 6c 75 64 65 Choose 'Close' to terminate the application.
You need to save the file as test.c and then compile it to test.exe. The exact details of how you compile it will depend on what C compiler you have installed, but for cygwin or MinGW it would be:
$ gcc -Wall test.c -o test.exe
If you don't have a compiler installed yet and just want to quickly try running a small C program then a further alternative is to use a site such as codepad.org or ideone.com where you can type (or paste) your code and run it online.
A few things to help you learn C in an easier manner:
First, you say you wrote it in Notepad. Bad choice for learning C. Notepad does not support Syntax highlighting.
Second, C needs to be compiled, on windows you have a few choice for compiler, the first would be MinGW which is Free.
Third, and IDE that is MinGW aware and C syntax aware is also needed. Geany is simple enough but is very smart and full of sweets.
Finally, see the tutorial here, how to get them all working: geany+mingw on windows.
One more thing, totally unrelated to C, or maybe it does.
I bluntly assume that C is your first programming experience, or that you are still doing first steps in programming.
C as a first language is VERY BAD. It is not forgiving, and most compilers pass things, but the code will crash, not letting you know what you did wrong.
Consider learing other languages first, a few good choices would be:
Python
Lua
Ruby
If you need a compiler and environment for C, I'd go with Visual Studio Express instead of the other suggestions. It's much more comfortable and offers a fuller Windowsy feel. And it's also free.

Call command line from C program

I am writing an command line application in C and from within the program, I would like to call other command-line applications. For example (and only as a clear example!), say the I would like to write a C app that calls the java compiler. For one, how would I do this? Is there a C function that directly calls the command line of something? For two, how would I take the normal text output that the java compiler displays and display it through the c program?
I'm sorry if this sounds a bit crazy, but I am very new to C and I have never done anything like this before. I would love to start writing a few of these utility style apps. If it matters any, I am currently running Windows, but would prefer a platform-independent method if possible, as I do use Linux from time to time.
You might look into system. I think you can use it in Windows as well as UNIX/Linux systems.

Resources