Colored console output in Linux - c

I just started learning programming in C.
the first problem was to choose on which platform should I learn it, and I selected Ubuntu.
I found a GCC compiler to compile my projects, and it worked fine for me. I was running my compiled projects through Terminal.
But when I wanted to write a program which have to show a text on a colorful background, I understood that Terminal is not helping me. Actually I am learning from lessons written for programming on Windows, and they use there Borland C++ 5.2, but these lessons were written in 2007
So I am wondering, where I should run my compiled program in Ubuntu 10.10, to see the result properly?
Actually I tried to transfer to Windows, but Ubuntu on my computer works much faster, and it is easier I think to work in Ubuntu rather than Windows.

The simplest way to print color message without any toolkit is to use VT100 escape commands:
http://www.termsys.demon.co.uk/vtansi.htm#colors
So you can write something like this:
printf("\x1b[31mThis is red text\x1b[0m\n");
printf("\x1b[32mThis is green text\x1b[0m\n");
But this code works only for VT100-compatible terminals (almost all terminals of the Unix-system). The best way is to check TERM environment variable before colorizing your output.
Disclaimer: if you want to write code capable with all terminal types then you should use ncurses-like library.

The ncurses library does exactly what you are looking for. Using this library, you can choose to write any text, anywhere on the screen with any text/background color.
You can read more on it on Wikipedia. You can install it under ubuntu with:
$ sudo apt-get install libncurses5-dev

If you want to start with GUI programming in Linux, you will probably have to look into one of GUI toolkits, which is a library to write GUI elements, widgets, etc.
One of these toolkints is Gtk, which you can start to learn via Gtk tutorial. Another option is Qt which is probably easier to use, but that is based on C++.
I'm afraid that Borland C++ Windows courses are pretty much unusable for Linux GUI programming.
Or, if you just to want a colored terminal text, look into this question: stdlib and colored output in C

Related

Can you code a shell for Linux using Windows OS and Dev-C++ IDE? Do shells work the same way for both operating systems?

I'm studying computer engineering and we have a class called operating systems where they lecture us about how OSes handle stuff etc. This week we were given a homework which requests us to code a shell that works on Linux. The problem is, they tought us literally nothing on how to code a shell so we are supposed to do some research and figure it out.
I found this guide online and it seemed perfect to me: link
Code, with explanations, what else could I ask for.
Now, I'm using a Windows PC and I use Dev-C++ IDE with GCC compiler. Can I code a shell that works on Linux with my current setup or do I have to install Linux? Are there any major differences between how shells run on these OSes?
If you want to write a shell for Linux, you want to target GNU/Linux which is basically POSIX with some extensions.
Dev-C++ by default uses MinGW GCC as its compiler, providing a Win32 API.
Win32 and POSIX are completely and fundamentally different. A shell written for one will not even slightly resemble the other.
Instead, you have several options:
Dev-C++ can optionally use Cygwin GCC, providing a more GNU/Linux-like experience on Windows. You need to take great care not to rely on any Cygwin-specific functionality like how it automatically translates pathnames and line terminators. You'll still need access to a Linux install to verify that it works.
Windows 10 lets you install WSL, a more modern Linux compatibility layer for Windows. Dev-C++ doesn't explicitly states it support it, so you may need to edit and compile separately, and may lose debugging functionality. You'll still need access to a Linux install to verify that it works.
Just run Linux in a VM. The only thing to care about is getting your shell working.
I would suggest saving yourself a ton of trouble right now and just download/install an Ubuntu image in VirtualBox.

Run C programs from sublime 2.02

I want to compile and run C/C++ programs with sublimetext 2 itself.I dont want to use terminal to do so. What do I need to do for this??I am compeletely new to this so a lot of answers to similar questions did not help me as most of them were for windows.
sublime text 2 build system for C programming language should show you how to setup your build environment. Note: you need gcc etc. installed
Edit: you should although run your programm in a Terminal because SublimeText2 is an Editor and can not recieve user input for your programm. so you better just build it with ST2 and run it in Terminal or you just forget about ST2 and make your own Bash script that manages compiling and runs the programm.
I would use sublime text as an editor (which is what IDEs are). Then you'll need to have a Makefile for your program. And build it by typing make in a terminal (or by configuring your editor to run that command). BTW, you can type m a k e enter once in your day (and later use up in the same terminal, to reapeat the latest command).
Learning how to write simple Makefile-s is a reusable skill (see here for "why?"). See this and that and many other examples. Don't attach yourself to one particular editor (even if you may like one more than others; my preference goes to emacs).
You'll learn a big lot by studying (and improving) the source code of some free software. Look at sourceforge, github, etc.. to find some free software interesting to you, then download its source code, build it, and study it.
The tips and advices I gave here are still valid today, with a recent Ubuntu (e.g. 14.x)
BTW, your question looks like you might be confused; Sublime Text is an editor (perhaps glorified as an IDE). It does not compile anything. The C or C++ compiler on Ubuntu is GCC (using gcc command for compiling C code, and g++ command for compiling C++ code) or perhaps Clang/LLVM (using clang command for C code, and clang++ command for C++ code). That compiler is often started by make, and IDEs on Linux are often running make -or directly the compiler- command for you. You'll better be able to run these commands yourself.

How do I setup a C environment on Windows (and maybe Linux compatible, too)

Short version of question: How do I get started with C programming? Note that I am not asking for a tutorial on learning C language (I can learn that easy enough). I need to setup the environment (I hope I'm asking this question clearly). Here's what I mean:
For my math thesis, I need to write a program in C on Gentoo Linux, using a library called CVODE/SUNDIALS. There is nobody (it seems) in my department who can help me set this up - my professor has left the computer work 100% to me because I have some programming background and he's a math geek. But my experience is with scripting languages (think VBA) and not full, powerful programming languages where you have to link the compiler and libraries, etc. like C.
There is no development environment on the Linux cluster - or at least not that's friendly, and has a debugger - that I've found. So, what I need to figure out how to setup a C programming environment with CVODE library on my PC (Win 7 x64, at little to no cost.
I have found plenty of tutorials on programming in C. I looked up Eclipse, which I have a little bit of experience with, as a development environment, but it's instructions say you need to install a compiler, too.
What I would like is someone to tell me, in simple language that I can understand (which might be the most difficult part of this question) the big picture of what I need and what to do (and maybe even links to where I can find what I need) to set up a C environment with CVODE. If the information is Windows/Gentoo Linux cross platform, even better.
Thank you.
P.S. I did search the site and saw lots of "How do I setup" quesitons, but no C one. Because I know someone will yell at me for that. Also, I don't want to have a convo about whether to use C#, C++, Java, etc. That just complicates the issue - and I need to get this done.
Edit: I have learned a little more since this question and now realize that I left out a key part of the question. The CVODE library and Linux cluster at school use MPI - parallel programming - which is not available on your average, run-of-the-mill PC. So all development must be done directly on the cluster.
Linux: Simple way is to install gcc or g++.
You can write your code in your plain text editor (nano, vim, gedit, kwrite, etc)
Save your file in .c or .cpp extention and type in terminal
gcc filename.c
or
g++ filename.cpp
You said that you want to write c code on Gentoo Linux, as i understand you're not familiar with Linux? The best choice in this case is to:
Install virtualbox in your windows machine (https://www.virtualbox.org/), it's a free software that let you emulate in your desktop another systems like Linux...
Install Gentoo linux on virtualbox, there are a lot of tutorials on the net, for example this video: http://www.youtube.com/watch?v=DUf_1wAPeyA
When you install Gentoo Linux on virtualbox you have all you need to develop C (gcc compiler, gdb debugger...)
Now you can download your library, and decompress it
In general all (Good) Linux libraries come with a 'README' file that contain all instructions for installing the library.
I think you need to do this:
./configure --prefix=/DIRECTORY_YOU_WANT_TO_INSTALL_THE_LIBRARY
make
make install
You can now play with C and you new library, like this:
suppose you create a new file test_lib_ CVODE.c you can compile it like this:
gcc -Wall test_lib_ CVODE.c -o test_lib_ CVODE -lcvode
I assume that the installed library is named libcvode.so
If you have any questions, you can always get help here.
Regards.
I think you should use Code::Block in Linux, it is very similar to Window's Code::Block and it is very easy to debug and other things.
These were all useful answers. I tried pursuing each of them at least a little bit. However, the only reasonable solution seems to be to use emacs on a terminal window. This is because I'm using MPI - yes, I know I didn't mention that in the OP - which can only be done on a cluster.
I am new to this environment and was not aware of the MPI or the affect it would have on my attempt to develop.
I believe I can do better than this if I can figure out X/Windows using Cygwin. But I am a long way from that.
Thanks all for your effort and sorry I can't really award a best answer (I guess).

what software should i use when i want to study c

i want to write code use notepad and compile the c code use cmd on windows (like java),
what software should i use ,
gcc for windows
cygwin
Turbo C
gvim
thanks
An excellent option for Windows is the Visual C++ Express Edition (which is free). This is a full IDE, compiler, and debugger and is a great way to get started.
GCC is also a good option if for some reason you wish to avoid an IDE. I wouldn't recommend this for a beginner, though.
Please don't use Turbo C, it's very old and you will have problems with it.
Cygwin is a popular linux environment for Windows.
Gvim is an editor and (in my opinion) has a very high learning curve. You could use Notepad++ or Code::Blocks to develop your code in.
mingw is also a good option to start with.
IMO you should avoid IDEs: a beginner needs to actually see and feel the whole process of compiling by invoking the compiler from a terminal, not by simply pressing some keys and bang! Also most of the IDEs have code completion facilities which is not good for beginners (especially for C). So I would go with vim (or gvim) and cygwin.
PS: Give Linux a try, you might actually like it; then vim and gcc.
Use cygwin. It's just about the only way to get a C environment on Windows that's both modern (i.e. not 16-bit, DOS-based) and conformant to the standards, and with it you get a fairly-complete POSIX environment, which allows you to actually do interesting things with C.
The classics would be Cygwin and MSYS/MinGW.
Cygwin provides a Linux API compatibility layer and includes a package manager which provides access to many well-known Linux applications (ie batteries included).
MSYS/MinGW only provides a minimal userland to get the GNU build system working.
Both come with their own shell environment, but can be used from Windows' command line as well.
Binaries created with Cygwin-gcc won't work without cygwin1.dll, but you get all the benefits of a mostly-POSIX environment. You can also use Cygwin's MinGW package for cross-compilation and thus create stand-alone binaries.
Other solutions are possible: Currently, I'm using the MinGW compiler together with the Cygwin userland from the Windows command line...
I think cygwin itself named as gcc for windows.
also gvim is not a compiler , It is a very nice and powerful tool for code editing.Also you can try emacs and kwrite
Some suggestions from my side:
In windows, you need to install a third party software to try all programming stuffs(c,c++ etc) ,Most of the times it is having GUI implementations and you didn't get any exact idea behind the code execution flow.also the command prompt of windows not at all efficient as linux shell.
But if you install a good linux distro (ubuntu/fedora/cent os/debian/pardus/suse/arch linux),all default compilers,debuggers and editors are installed within the OS and you can try the basic and all advanced programs by just opening the terminal(shell).Also the shell is a more powerful tool and you can enjoy all kind of automation comamnds(I mean scripting).
Also: for your added information, most of the embedded programmers in the industry prefer command line execution unlike GUI implementation because it is too fast than GUI.

How can I improve my C coding process on OS X?

I'm currently learning C from some Harvard screencasts. They're great. I'm currently, because I'm used to it, using this process for running my code:
Type it it TextMate
Save it
gcc filname
run "./a.out"
goto 1
Is there a major process, program, or best practice I'm missing here? I've tried apple-R in TextMate, but to compile something bare-bones like...
#include <stdio.h>
int
main(int argc, char * argv[])
{
int x = 342;
int y = 92;
int z = x + y;
printf("these values added are: %d\n", z);
return 0;
}
...takes 4 seconds which is clearly no good. Is there a great environment/compiler shortcut, or anything else I could be doing to make the actual testing more streamlined?
Thanks!
I use text mate and gcc in the terminal. I find Xcode too bulky for small commandline programs in C. It's great if you want to do cocoa GUI applications, need a quick Apple API reference or want a graphic frontend to gdb etc. 4 seconds sounds excessive for the program you have above, but it's probably due to Textmate cmd-R shortcut. I have never used that for anything else than scripts. But I agree that you should install Xcode, it's on your Mac OS install disk.
I disagree if you want to code ANSI C then this is what I'd suggest. Get familiar with a more powerful text editor, be it vim, emacs or whatever. I prefer vim.
See here:
http://macvim.org/OSX/index.php
Now, you should try to setup GNU Make for Mac OS X.
Doing this you will learn the methods most C programmers have used for ages. I think you'll get a lot more out of this than using Xcode straight out. With Xcode you want to make sure you are doing C and not Objective-C.
Now, with an editor like vim, you can do everything from a single terminal and waste fewer cycles switching around in your workflow.
So your workflow becomes:
$vim foo.c
(save)
$make
$./a.out
If you want to do any serious C programming that has any relevance to the UNIX envirnoment (and I suspect that Harvard course does), you will need to learn make at some point.
Install Apple's development tools, and use the XCode IDE. It may or may not increase your compile time, but it will greatly decrease much of the pain of hand coding and compiling on the command line.
To create a simple command-line C program (like the one you're describing above) do this:
File->New Project
Choose Command Line Tool as the project type.
The template includes a "hello world" program with a main.c file.
Edit the main.c file to include the code you're working on.
Run the project by choosing the Build and Run button.
**be sure to open the Console to view the output of your program. (Run->Console)
Good luck!
As mentioned before, XCode is the Mac way of doing it.
Just to give you an alternative solution, you can also use vi/vim from within a terminal. You open the file for the first time by typing vi filename.c. Then you type :w to save the file, :!gcc filename.c to compile it and :!./a.out to run it. The exclamation point does the trick of calling a shell command, so the last two commands can be replaced by one :!gcc filename.c; ./a.out . You also have a command history ("arrow up", "arrow down") from within vi, after pressing the colon.
Getting used to vi is a different story...
Try to build with gcc -Wall -Werror and try to understand and fix your warnings. That is always good practice.
I wouldn't recommended switching to XCode unless you need to use the debugger. GDB integration with XCode is pretty nice, and non-existent with Textmate.
To improve your Textmate workflow for smallish projects, learn to use make and install the Makefile bundle for Textmate. You can find it on GitHub.
This will give you a new Build ("Command-B") option in Textmate that will run make, parse the output, and let you easily correct compile time errors.
I think you should continue what you're doing. 4 seconds isn't that much time to wait, especially once your programs grow more extensive. Just using a basic text editor and gcc will let you master the basics of C. Once you're ready to start making GUI apps, then I would suggest to start using XCode to take advantage of the tools to make those kind of applications. I think an IDE is overkill at this point.
There should use Emacs or Vim with a Makefile. They can do anything.
For example, in Emacs, compiling is as simple as M-x Compile (But of course you could bind any key to it). Emacs is also cross-platform (Unlike XCode and TextMate).
Emacs also provides various other features useful for debugging (Built-in gdb support, ability to parse compile output, etc)
Using an IDE-Specific build tool is the exact wrong way to do things (Especially if that IDE happens to be platform specific, like XCode and TextMate)

Resources