C language introductory dry goods: C environment setting and simple code operation of various systems

C environment settings

Local environment settings

If you want to set the C language environment, you need to make sure that the following two softwares are available on your computer, a text editor and a C compiler.

text editor

This will be used to enter your program. Text editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS and vim/vi.

The name and version of the text editor may be different on different operating systems. For example, Notepad is usually used on Windows operating systems, and vim/vi can be used on Windows and Linux/UNIX operating systems.

The file created by the editor is usually called the source file, and the source file contains the program source code. Source files of C programs usually use the extension " .c ".

Before starting programming, please make sure you have a text editor and have enough experience to write a computer program, then save it in a file, compile and execute it.

C compiler

The source code written in the source file is a human-readable source. It needs to be "compiled" and converted into machine language so that the CPU can execute the program according to the given instructions.

The C language compiler is used to compile the source code into the final executable program. It is assumed that you have a basic understanding of programming language compilers.

The most commonly used freely available compiler is GNU's C/C++ compiler. If you are using HP or Solaris, you can use the compiler on the respective operating system.

The following section will guide you how to install GNU's C/C++ compiler on different operating systems. C/C++ is mentioned here, mainly because GNU's gcc compiler is suitable for C and C++ programming languages.

Installation on UNIX/Linux

If you are using  Linux or UNIX , please use the following command on the command line to check if GCC is installed on your system:

$ gcc -v

If the GNU compiler is already installed on your computer, the following message will be displayed:

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr .......
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

If GCC is not installed, please follow the detailed instructions on http://gcc.gnu.org/install to install GCC.

This tutorial is based on Linux and all the examples given have been compiled on Cent OS Linux.

Installation on Mac OS

If you are using Mac OS X, the fastest way to obtain GCC is to download the Xcode development environment from Apple's website and install it according to the installation instructions. Once Xcode is installed, you can use the GNU compiler.

Xcode is currently available for download from developer.apple.com/technologies/tools.

Installation on Windows

In order to install GCC on Windows, you need to install MinGW. In order to install MinGW, please visit the MinGW homepage www.mingw.org, enter the MinGW download page, and download the latest version of the MinGW installation program, named in the format MinGW-<version>.exe. </version>

When installing MinWG, you must at least install gcc-core, gcc-g++, binutils and MinGW runtime, but in general, you will install more other items.

Add the bin subdirectory of the MinGW you installed to your  PATH  environment variable so that you can specify these tools by simple names on the command line.

When the installation is complete, you can run gcc, g++, ar, ranlib, dlltool and some other GNU tools from the Windows command line.

After the installation is complete, before we formally learn the basic building blocks of the C language, let's run a minimal C program structure.

C Hello World example

The C program mainly includes the following parts:

  • Preprocessor directive
  • function
  • variable
  • Statement & expression
  • Comment

Let's look at a simple code that can output the word "Hello World":

#include <stdio.h>
 
int main()
{
   /* 我的第一个 C 程序 */
   printf("Hello, World! \n");
 
   return 0;
}

Next we explain the above program:

  1. The first line of the program  #include <stdio.h>  is the preprocessor directive, telling the C compiler to include the stdio.h file before the actual compilation.
  2. The next line  int main()  is the main function, and the program executes from here.
  3. The next line / ... / will be ignored by the compiler, and the comment content of the program is placed here. They are called program comments.
  4. The next line  printf(...)  is another function available in C that will display the message "Hello, World!" on the screen.
  5. The next line  return 0;  terminates the main() function and returns the value 0.

Compile & execute C program

Next let us see how to save the source code in a file, and how to compile and run it. Here are the simple steps:

  1. Open a text editor and add the above code.
  2. Save the file as  hello.c .
  3. Open a command prompt and go to the directory where the file is saved.
  4. Type gcc hello.c and press  Enter to compile the code.
  5. If there are no errors in the code, the command prompt will jump to the next line and generate  a.out  executable file.
  6. Now, type  a.out  to execute the program.
  7. You can see *"Hello World"* displayed on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!

Please make sure that the gcc compiler is included in your path, and make sure to run it in the directory containing the source file hello.c.

If it is a source file of multiple c code, the compilation method is as follows:

$ gcc test1.c test2.c -o main.out
$ ./main.out

test1.c and test2.c are two source code files.

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/111225363