C Language Basics

Source: Internet
Author: User
Tags bitwise string format

C compilers and linker



(1), the first experience

# include <stdio.h>
int main (int argc, char* argv[])
{
printf ("Hello world!\n");
return 0;
}

(2), do-it-yourself, compile, connect C program
Install vc++6.0
The CL hello.c/c compiles the build. obj file,. obj file is a common COFF format and is a target file that can interact with other platforms.
CL hello.c/c/P, there will be 1 more. i files. Then open it, there are more than 400 lines, which is the contents of the Stdio.h file. Of course, the Stdio.h file also contains other files, so the full expansion will be more than 400 lines.
Link Hello.obj link on the win platform to create a PE format hello.exe
If CL hello.c directly in the CMD named line, the compiler gives the automatic link, and the. obj and. exe files are generated.

(3), bitwise operation: bit operator There are 6 kinds, respectively, by Bit and (&), bitwise OR (|), bitwise OR (^), reverse (~), move left (<<), right (>>). (4), array characteristics

#include <stdio.h>
void change (int x[])
{
    int temp = x[0];
    X[0] = x[1];
    X[1] = temp;
    printf ("The size of the array name is:%d\n", sizeof (x));
}

int main (void)
{
    int a[2] = {one,};
    printf ("Array elements before swapping are:%d\t%d\n", a[0],a[1]);
    Change (a);
    printf ("The Commutative array element is:%d\t%d\n", a[0],a[1]);
    printf ("The size of the array name is:%d\n", sizeof (a));
    return (0);
}
/**********************************************
The array elements before swapping are: 11 22
The size of the array name is: 4
The exchanged array elements are: 22 11
The size of the array name is: 8
***********************************************/
When the array name is used as a function argument, passing is the 4-byte first address of the array,
So the copy of the array name parameter only records 4 byte first address. Because the address is passed, the value of the original array can be modified.

(5), the array of addressing formula:
A[n] = a + sizeof (type) * n
A[n]: The array element you want to address
A: Array first address
sizeof (type): the size of an element

N: Array subscript


(6), original code, inverse code, complement
Anti-code: When it is a positive number, it has the same inverse code as the original code. Negative numbers. The sign bit is 1, and the other bits are reversed.
[+100] Anti-= 01100100 [+0] anti-=00000000
[-100] anti = 10011011 [-0] anti-=11111111
Note: In the inverse code, 0 also has two representations.
Complement: Counter code plus 1
[+100] complement = 01100100 [+0] complement =00000000
[-100] complement = 10011100 [-0] complement =00000000
Note: In the complement, 0 has a unique encoding, [+0[complement =[-0] complement = 00000000.
For positive numbers, the original code and the inverse code, the complement is the same
This is because in the computer system, the numerical value is represented by the complement (storage).
Main reasons:
1, unified 0 of the code;
2, the symbol bit and other numerical position unified treatment;
3, the subtraction operation into the addition operation;
4, two the number represented by the complement, if the highest bit (symbol bit) has carry, then the carry is discarded.
Eg: Calculate the result of 9-6.
9-6=9+ (-6) =3
Using the complement to calculate it?
The 9 complement is 0000 1001,-6, and the complement is 11111010.
0000 1001
+1111 1010
1 0000 0011
The top 1 overflow, and the remaining 8-bit binary represents 3 of the complement. The result is 3.

(7), global variables, global variables, static local variables, static global variables
Global variables, static local variables, static global variables are allocated space in the static storage area, and local variables in the stack allocated space
Global variables: Variables defined inside a function are called local variables.  Local variables that are not decorated locally are called automatic variables (which exist in the Dynamic Data area-the stack), and the data in this area is generated and released dynamically as the program runs. function returns, and the function is generated when it is called. Keywords: default is Atuo (generally not write) features: When the program executes to the scope of the automatic variable to allocate


Global variables: Variables defined outside the function are called global variables. Global variables can be used for all source files only if they are defined in one source file, and of course, other source files that do not contain global variable definitions need to declare the global variable again with the extern keyword.


A static local variable has a local scope that is initialized only once, and has been in existence since the first time it was initialized until the end of the program's execution. It differs from global variables in that global variables are visible to all functions, while static local variables are always visible only to the body of the function that defines itself.


Static global variables also have global scope, which differs from global variables in that if a program contains multiple files, it acts on the file in which it is defined. Cannot function in other files, that is, a variable modified by the static keyword has a file scope. This way, even if two different source files have defined static global variables of the same name, they are also different variables.
(8), pointer and pointer variables
The nature of pointers: the essence of pointers is the abstraction of the memory address, which is used in the computer to represent the address plus the way it is interpreted.
If the element of an array is a char, then this is a char array, and if the array element is an int, then this is called an int-type array.
If the array element is a pointer, then this is an array of pointers.
Pointer variable: A variable that holds the address of a value other than a memory object is called a pointer variable.
The difference between a pointer and an address: An address is a numbered message that represents memory; the pointer has 2 information, one is the address information, and the other is the interpretation information for this address. For example: int *p The Memory object that represents the address to which this pointer points is resolved with int. Float *Q The Memory object that represents the address to which this pointer points is parsed with float.
Direct reference to C: We refer to variables directly by variable names. For example a=123. The compiler automatically converts the variable name to the stored address of the variable. Then put 123 in the storage space of variable A.
C-Language indirect reference: first, the variable's address is stored in a variable (pointer variable), and then by holding the variable's address of the pointer variable to refer to the variable.

#include <stdio.h>
void Main ()
{
    int a, b;
    int *p;//define pointer variable p
    p = &b;//Place the address of variable B in the variable p
    a = 3;//Direct reference variable a
    *p = 5;//Indirect reference variable b
}

(9), string and String functions

9.1, string format and input and output

char word [;
] scanf ("%s", word);
printf ("%s", word);

Equivalent to
char word [;
] Fgets (Word,, stdin);

The difference between the string "X" and the character ' X ':
"X" is actually composed of ' X ' and ' two ' characters.

eg
char word [;
 ] scanf ("%s", word);
 printf ("%s\n", word);
sizeof the size of the data in bytes
printf ("%d\n", sizeof (word));
Strlen computes the length of the string in characters
printf ("%d\n", strlen (word));
9.2. String classification
string constant: Refers to any character in a pair of double quotes. The compiler automatically appends the end tag to the ending character, and the string constant belongs to the static store, saving only one copy in the run. You typically use #define to define string constants. String constants are generally static storage classes, which means that if you use string constants in a function, even if you call the function multiple times, the string is only one copy of the entire run.
9.3. Array and initialization of strings
const char m[] = "Hello World"
Equivalent to const char m[]={' H ', ' e ' ... '
Note the null character at the end of the tag. Without it, you get an array of characters instead of a string.

(9), pointer and array
Pointer variable pointing to an array element
int a[5], *p;
p = &a[0];
In the 1.C language, the array name represents the first address of the array, which is the address of the first element. Because the above assignment is equivalent to: P = A;
2. Pointers to array elements can also be assigned when: int *p = &a[0]; or int *p = A; but not a=p; because a is a constant, p is a variable, just like the concept of x=3 oh 3=x.
If P's initial value is &a[0], then: both P+i and a+i can represent the address of the element a[i], * (p+i), and * (a+i) represent the values of the array element p+i that the pointer a+i or a[i points to.

Summary: There are two ways to refer to an array element: Subscript method: A[i]; Pointer method: * (P+i).

Difference: The array name is a constant, the address of the first array is stored, and the pointer is a variable.
	char heart[] = "I love Mill";

	Char *head = "I love Till";
	
	(1) You can use the array symbol
	for (i=0;i<6;i++)
	{
		printf ("%c", Heart[i])
	;
	printf ("\ n");
	for (i=0;i<6;i++)
	{
		printf ("%c", Head[i]);
	printf ("\ n");
	(2) You can use pointer addition
	for (i=0;i<6;i++)
	{
		printf ("%c", * (Heart+i));
	printf ("\ n");
	for (i=0;i<6;i++)
	{
		printf ('%c ', * (Head+i));
	}
	printf ("\ n");
	(3) The pointer can use the increment operator, while the array name cannot be
	(* (head)!= ' I ')
	{
		printf ("%c", * (head++));
	}
	printf ("\ n");

(10), pointer functions and function pointers
function that returns a pointer value

1 A function that returns a pointer-type data, defined in general form:

Type name * Function name (parameter table)

For example: int* func (int x, int y);

Explanation: Indicates that the return value of the Func is a pointer to int-type data.

Pointer to function
Definition: type name (* pointer variable name) ();
Why there is a pointer to a function: because AH. function as a program, in memory also occupy a piece of storage area.
It will have a starting address, the entry address of the function,
What does a pointer to a function do? This way, we can define a pointer variable to point to a function, and then call the function through the pointer. Passes functions as arguments between functions.
Indirect calls vs. Direct calls: calling functions through function pointers called indirect calls, called direct calls through function names.
Example:
Int (*p) ()
"P" is a pointer variable that points to a function, and the return value of this function is int type.

int m1 (int x, int y) {
.....
Return
}

void Main () {
int A;
Int (*p) (int,int) = NULL;
P = M1;
A = P (2,3); Indirect call
}

(11), the definition of the array of pointers: If the elements of an array are pointer types, then we call this an array of pointers.
The pointer array is defined as follows: type name * array name [constant expression]
Char *astringa[]=
{
"I ' m not afraid to take a stand",
"We'll walk this road together, through the Storm",
"Whatever weather, cold or warm"
};

The string is in the constant area, the length is 12 bytes, element exchange is convenient, only need to exchange pointers.


(12), pointing pointer
Direct and indirect references to variables
By the name of the variable called the direct reference, the reference to the variable by the pointer is called Indirect reference
Two types of indirect references
1, if a pointer variable is stored in the address of a target variable is called the first level address
2, if you are holding the address of a pointer variable that points to the address of the target variable, this is called a level two address.
void Main ()
{
int a = 99;
int *PA = &a;
int **ppa = &a;
}
This is absolutely not, because the type does not match ah. There will be an error.
Then look at this program:
void Main ()
{
int a = 99;
int *PA = &a;
int **ppa = &pa;
}
function that returns a pointer value

(13), easy to confuse the const
The keyword const does not change a variable to a constant, and a symbol preceded by a const qualifier only means that the symbol cannot be assigned, meaning that its value is read-only for the symbol. The most useful place for const is to limit the parameters of a function so that the function will not modify the data that the argument pointer refers to, but other functions may modify it.
const int limit = 0;
const int *limit = &limit;
Limit is a pointer to a constant shaping that cannot be used to modify this integer value, but at any time the value of the pointer itself can be changed.
const INT * GRA;
int const * GRA;
int * Const GRA;
The difference between three people:
In the last case, the pointer is read-only, and the first two cases point to an object that is read-only, which is the integer value of the address store

(14), C memory allocation mode:
Memory Partition:
1, stack area (stack) by the compiler automatically allocated release, stored for the run function and the allocation of local variables, function parameters, return data, return address and so on. The operation is similar to the stack in the data structure.
2. The heap area (heap) is typically released by the programmer, who is responsible for releasing memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but if space is allocated on the heap, it is the responsibility to recycle it, otherwise the running program will have a memory leak allocation method similar to the linked list.
3, the Global zone (static region static) holds global variables, static data, constants. After the program is finished, the system releases the initialization of the global variable, which is done by the compiler. is written in an executable file.
4, the literal constant area constant string is here. The system is released after the program is finished.
5, the Code area holds the function body (class member function and global function) binary code. Readable executable.

float x; Char place[] = ' Doub ... '; The system will set aside enough memory to store float or string, and you can also explicitly request the number of memory char place[20] malloc (), allocating memory when the program runs. Double *ptd; PTD = (double*) malloc (30*sizeof (double)); This code requests a space of 30 double values and points the ptd to the location of the space. Free () frees only the blocks of memory that its arguments point to. When compiling a program, the number of static variables is fixed and does not change when the program is running. The amount of memory used by an automatic variable increases or decreases automatically when the program executes, but the amount of memory used by the allocated memory only increases. Unless using free ();

(14), what is the declaration, what definition
In C language objects (functions, variables) must have and have only one definition, but can have multiple declarations. The extern declaration tells the compiler the type and name of the object, defined as allocating memory for the object.

Arrays and pointers:
Array of pointers: if the elements of an array are pointer types, then he is an array of pointers.
Definition: int *arr[100]
This int *x[100] What is the length of the 32-bit system, the pointer is 4 bytes, then there are 100, that is 400 bytes.

Eg:char *carr[]= {"Hello", "World"};
The Carr array contains 2 4-byte pointers. is a pointer to a string, the first address of a string.


(15), stdafx.h   Standard application Framework extensions 
So-called header file precompilation is to put a project ( Project), some of the MFC standard header files (such as Windows.H, Afxwin.H) are precompiled, and later when the project is compiled, this part of the header file is not compiled, only the results of the precompilation are used.   This will speed up compilation and save time.   The precompiled header file is generated by compiling stdafx.cpp, named after the project name, and the compiled result file is projectname.pch because the suffix of the precompiled header file is "PCH". The compiler uses a precompiled header file StdAfx.h through a header file. StdAfx.h this header filename can be specified in project's compilation settings. The compiler believes that all code before the instruction #include "stdafx.h" is precompiled and skips #include "stdafx.   H "instruction, using PROJECTNAME.PCH to compile all the code after this instruction. Therefore, all MFC implementation files The first statement is: #include "stdafx.h".   
Differences from Stdio.h
We generally use TC or VC compile C program when all must first include this stdio.h header file, this header file contains the definition of scanf and printf function, if we do not include this file at the beginning of the program, then you call these two functions will not succeed, it actually and c+ + iostream.h files are similar in function, they are generally included in the StdAfx.h file.


 












[
] (a) [
] [

] [@] () ()

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.