2. Basic C language Learning 02

Source: Internet
Author: User
Tags function definition posix custom name

=============================================================================
Upload the Windows executable file to Linux and see if it can be executed?
Sftp> put A.exe

The following error occurred: No permissions, Access denied
-bash:./a.exe:permission denied
Let's try to elevate the A.exe's authority.
chmod u+x A.exe
The following error occurred: cannot execute binary: execution format error
-bash:./a.exe:cannot Execute binary file:exec format error
Description of Windows executables can no longer be performed under Linux systems

Fatal error fatal errors

=============================================================================
#include的意思是头文件包含, use the C library function to include the header file corresponding to the library function used earlier
C language If you want to use the printf function, before use must #include <stdio.h>
-----------------------------------------------------------------------------
There are two ways to use include:
1. #include < file name > description of files to be included in the system directory
2, #include "file name" description needs to include the file in the current directory
-----------------------------------------------------------------------------
Executable code must be placed inside {} curly Braces
; In the C language, it is possible to write a single semicolon in a line, which means an empty statement
A group of curly braces in the C language can also have more than one set of curly braces
-----------------------------------------------------------------------------
int A; Define a variable
extern int B;//Declare a variable
-----------------------------------------------------------------------------
C-Language custom name requirements:
You can use uppercase and lowercase letters, underscores, numbers, but the first letter must be a letter or an underscore
And the letters are case sensitive. (Note: The basic language is case insensitive)
-----------------------------------------------------------------------------
printf is a C library function that outputs a string to a standard output device
(Note: Standard output devices don't just refer to screens, printers, etc.)
can be viewed by command Man 3 printf
-----------------------------------------------------------------------------
There are two types of compilation errors
Warning does not affect compilation, just warning
The error compiler is completely out of work.
-----------------------------------------------------------------------------
int main ()
{
return 0;
}

void Main ()
{
return;//or you can use this sentence without writing
}

For C, both are written, but for C + +, only the first one is supported.
-----------------------------------------------------------------------------
In the main function
return 0; Represents the successful execution of the program,
return-1; Failed to represent program execution.
-----------------------------------------------------------------------------
The extension of the C language file is a. c,c++ file extension of CPP

Linux systems do not use suffix names to differentiate files;
Linux does not differentiate file suffixes like windows, so it can be changed without affecting the usage.
1,MV can be used to move files or to change file names.
2, modify the name: MV Filename.type filename.newtype You can change the file suffix.
3, but some specific files should not be changed arbitrarily, such as configuration files.

=============================================================================
You need to include the Stdlib.h header file before using the system.
Call the system function to execute another program in the code of the C language
-----------------------------------------------------------------------------
If a program is executed at the command line, the caller of the program is the operating system, and if it is passed through the system in code
Executes a program, then the caller of the program is the code itself written by itself.
All C language library function calls can only guarantee that the syntax is consistent, but there is no guarantee that the execution result is consistent.
The same library functions may or may not be the same if they are executed under different operating systems.

The library function of C language under different platforms can only guarantee the same syntax, but it cannot guarantee the same result.
(Because of the hardware requirements of the C language, too many requirements for the operating system)
So in C language on different platforms when writing code, there may be some porting workload.
-----------------------------------------------------------------------------
POSIX is a standard, and as long as it conforms to this standard function, the results can be consistent across different systems.
If you have a POSIX-compliant function, use it as much as possible.

UNIX and Linux Many library functions are POSIX-enabled, but Windows supports poor comparisons. (once Microsoft said to show strong support, but said much less!) )
So, if porting UNIX code to Linux is generally a small cost, it would be cumbersome to port the Windows Code to UNIX or Linux.
-----------------------------------------------------------------------------
The system returns an integer
The return value of system is the value of return of the main function in the called program.

=============================================================================
C language Compilation process
1, precompiled--2, compiling -3, Link
-----------------------------------------------------------------------------
Precompiled command:
Gcc-o cc1.c c1.c-e or Gcc-o cc1.c-e c1.c
(Note:-O function is to specify the name of the output file, if not add-O, the resulting file name is always called a.out)
Precompiled c1.c, precompiled after the file name is cc1.c
(Note: The compiled name can be arbitrary, but we know that the precompiled file is still the text of the. c file, so in order to distinguish between the name of XXX.C)
in the C language in the beginning of the statement is also called the precompiled Directive
#include <stdio.h>
One of the pre-compiled features: a simple replacement for the contents of the header file included in the include, which is replaced with the. c file.
...
//Omit 10,000 lines here
...
Pre-compiled feature two: the comments in the code are removed.
-----------------------------------------------------------------------------
Compiled commands:
Gcc-o cc1.o cc1.c -C or Gcc-o cc1.o-c cc1.c
compiled cc1.c, compiled after the file name CC1.O
Compiled function is: The text of the C language compiled into binary instructions.
-----------------------------------------------------------------------------
-linked command:
Gcc-o C1 CC1.O (Note: GCC does not have a separate link parameter)
Links system library functions to CC1.O (in short) to an executable program named C1

We want to know what kind of library is being linked to under the Linux system. (What kind of chur is needed to execute the program?) )
Use the command LDD C1 to view
(Note: The code written in C is the least dependent on the library, if you use a different language depends on the library more Oh!!) Need to pack a lot of bags)
So what library does the executable program need to use under Windows system?
Using a small software Depends.exe can be viewed to

=============================================================================
Operating system structure
1. User mode
The programs we are writing now are running in user mode.
2. Kernel mode
These software, such as the operating system itself, device drivers, and so on, are running in kernel mode.
-----------------------------------------------------------------------------
A register of one CPU can put 8 bits
For example:
0000 0000
1111 1111
Then this CPU is 8-bit CPU.
-----------------------------------------------------------------------------
A register of one CPU can put 16 bits
0000 0000 0000 0000
1111 1111 1111 1111
Then this CPU is 16-bit CPU
-----------------------------------------------------------------------------
There are three types of bus: Data bus, address bus, control bus
The bus is the same as the number of registers in the CPU most of the time

8-bit CPU bus is 8-bit
1111 1111
F F
256BYTE (255 bytes)

16-bit CPU bus is 16-bit
1111 1111 1111 1111
F F F F
65536BYTE (64k bytes)

32-bit CPU bus is 32-bit
1111 1111 1111 1111 1111 1111 1111 1111
F F F F f f f f
32-bit CPU management memory has a maximum value of 4G bytes = 4 x 1024M = 4 x x 1024x768 x 1024Byte (bytes)

64-bit CPU bus is 64-bit
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
F F F F F F F F F f f F F. F F f f
In theory 64-bit CPU management memory is: 2^64 bytes, but also depends on the comprehensive indicators: such as motherboard, operating system support and so on.
But at least it's far more than 4G.

If the register is 64 bits, but the bus is 32 bits, this CPU is called Quasi 64-bit CPU.
(That is, the inside can be very wide, but the outside process is not up to)

If the 64-bit software operating system is running on a 64-bit CPU architecture, the system is 64-bit;
If you run a 32-bit software operating system on a 64-bit CPU architecture, the system is 32-bit. The CPU architecture in the
-----------------------------------------------------------------------------
Instruction Set
Simplified instruction Set RISC (server-side) For example: SPARC, arm belongs to the thin instruction set

Complex instruction set CISC (such as Inter CPU) For example: X86 is a typical complex instruction set
------------------------ -----------------------------------------------------
All calculations inside the CPU must be done inside the register.

Registers inside the CPU also have the name:
8-bit CPU (x86 architecture) 16-bit CPU 32-bit CPU 64-bit CPU
a ax,al,ah   eax    rax
B BX,BL,BH     EBX               RBX
C CX   &NBSP , ecx               RCX
D dx edx RDX
--------------------------- --------------------------------------------------
When using QT, a console window appears when you use the system function. What is the
?
A: Because the system function is a console-based function (a function of the character interface) under the Windows System, a console window appears even when the system is lowered in the graphical interface.
So how do I downgrade the function in the graphical interface and not display the console?
Answer: You can change another function! The function is WinExec ("notepad", sw_normal); However, the function needs to contain a header file that is #include <windows.h>

QT Common shortcut keys
Ctrl + I Auto-format code
Ctrl +/Comment Code/Uncomment code
Ctrl + R does not debug run code
Ctrl + B compiles code but does not run code
ALT + Enter auto-complete class function definition
F9 setting breakpoints
F5 Debug Run
F10 Next Commissioning
F11 Step Commissioning
-----------------------------------------------------------------------------
vs Common Shortcut keys
Ctrl + F5 do not debug run code
Ctrl + K,ctrl + F Automatic Formatting code
Ctrl + K,ctrl + C Comment code
Ctrl + K,ctrl + u Uncomment code
Ctrl + Shift + b compile, do not run code
F5 Debug Run
F9 setting breakpoints

Note: The C language ends with a semicolon, not the end of a line.
=============================================================================

2. Basic C language Learning 02

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.