C data types and variables finishing

type of data

The method of obtaining the size in bytes of the int

printf("int bytes:%d",sizeof(int));

Finishing list

Types of

Byte count

Ranges

char

1

[-128,127]=[-2^7,2^7-1]

unsigned char

1

[0,255]=[0,2^8-1]

short int(short)

2

[-2^15, 2^15-1]

unsigned short int

2

[0,2^16-1]

int

4

[-2^31,2^31-1]

unsigned int

4

[0,2^32-1]

long int

4

[-2^31,2^31-1]

unsigned long int

4

[0,2^32-1]

long long int

8

[-2^63, 2^63-1]

unsigned long long int

8

[0, 2^64-1]

float

4

- / + 3.4e38 (accurate to 6 decimal places)

double

8

- / + 1.7e308 (accurate to 15 decimal places)

long double

12

- / + 1.19e4932 (accurate to 18 decimal places)

 The difference between Int and long int

Early C system platform 16 int, int represented by two bytes, in the range of -32768 ~ + 32767; long long int is shorthand, represented by 4 bytes, the range -2147483648 2147483647 +. Obviously integer range in early platform long int can represent much more than. And nowadays popular 32-bit platform int system, i.e., 4-byte system, has been long int and no difference in this system, and they are 4-byte signed integer, the range of the number of tables are -2147483648 + 2147483647.

Data Overflow

When the variable is greater than the maximum or smaller than the minimum value of the type type, the value of the variable is to jump, such as a maximum value plus a minimum value.

#include <stdio.h>
 void main () {
        char min = - 128 - . 1 ;
        char max = 127 + . 1 ; 
       the printf ( " min =%. 1-D \ R & lt \ n- " , min);    // Output: 127 
       the printf ( " max =% +. 1 D \ R & lt \ n- " , max);   // output: -128 
}

Static variables and functions

Variable or function is only to be used in this document all use the static keyword to declare

Static variables

0, static global variable is visible only to the current file, other files inaccessible, other files can define variables with the same name, both independently of each other, can not be achieved with extern boast file access

1, static variables are automatically assigned the initial value of 0, while the average variable compiler will not automatically given initial

2, local static variables are placed in the data area, so it will not disappear because of the function of death, but on top of parasitic function. Global static variable is similar to global variables.

3, when the static variables defined repeatedly, static local variable high priority, but only a partial effect on

#include <stdio.h>
 static  int I; // global static variable 
void fun1 () {
     static  int I; // local static variables 
    the printf ( " % D \ R & lt \ n- " , I); // 0-9 ( partial) 
    I ++ ; 
} 
void main () {
     int X;
     for (X = 0 ; X < 10 ; X ++ ) fun1 (); 
    the printf ( " ---% D \ R & lt \ n- " , I); // 0 ( global) 
}

Static function

0, before the return type of the function plus static, static function is

1, static function can only be seen, other documents can not reference the function in which it is declared file

2, different files can use the static function of the same name, independently of each other

Structure

statement

method 1:

struct structure name 
{ 
   member list 
}; 
struct   structure variable name 1, variable 2;

Method 2

struct structure name 
{ 
   member list 

} variable 1, variable 2;

Method 3

struct 
{ 
   member list 
   
} variable 1, variable 2;

Method 4

typedef struct 
{ 
  member list; 

} Synonyms structure; 
structure alias variable 1, variable 2;

initialization

method 1

struct 
{ 
   member list 
   
} variable 1 = {value 1 members, two members of the value, the value of 3 members ......};

Method 2

memset (& structure variable name, 0 , sizeof (structure variable name)); // all variables clear all

Reference (use) Members:

Structure variable

struct   structure variable name 1, variable 2; 
Variable 1. Member Name

Structure pointer variable

struct   structure name * Variable 1; 
variable 1 -> Member Name

File Sharing variables between C

external

extern keyword is a computer language, can be placed before the variable or function to indicate the definition of a variable or function in another file. When prompted compiler encounters this variable or function, find its definition in other modules, in addition, extern also be used to specify the link.

Extern can be written by the header file, write in any definition of a variable point C a file (e.g. main.c), after which the shared variable can be achieved as long as the folder that contains the header

For chestnut:

C1.h, declare the function c1fun ()

#ifndef _C1_H_
#define _C1_H_
void c1fun(char *strx);
#endif

C1.c, achieve function c1fun ()

#include<stdio.h>
#include"c1.h"
char *wu;
void c1fun(char *strx){
   wu=strx;
}

Main.c, call the function c1fun (), and reads the value of the external variable wu

#include <stdio.h>
#include "c1.h"
extern char *wu;
void main(){
   c1fun("123456");
   printf("===:%s",wu);//输出:123456
}

#define

For the amount does not change, can be declared in the header file, you can use the included in other c file

As c1.h in:

#define DONG "dongxiaodong"

Mian.c in:

#include"c1.h"
……
printf("===:%s",DONG);
……

Use macro definition

#define use

Macros define constants

#define DONG '2'          // character 
#define DONG "IS STR"     // string 
#define DONG 12 is           // digital 
#define DONG 13.36        // float

Macro definition

#define P 123 
……
#undef P

Use a typedef

Rename the macro definition of the type

typedef unsigned char u8;

Rename type of macro definition Comparative

#include<stdio.h>

#define ch1 char*
typedef char* ch2;

void main(){
 ch1 a,b;//char *a,b;
 ch2 c,d;//char *c,*d;
 //输出: --8,1,8,8--
 printf("--%d,%d,%d,%d--",sizeof(a),sizeof(b),sizeof(c),sizeof(d));
}

Macro definition with parameters

#include<stdio.h>

#define Max(x,y) ((x>y)?x:y) 
#define Add(x,y) (x+y);

void main(){
  int i=Max(10,2);
  int ii=Add(2,1) //此处无分号 
  printf("max:%d,add:%d",i,ii);//max:10,add:3
}

reference:

https://blog.csdn.net/xlh006/article/details/81540703

https://blog.csdn.net/kuniqiw/article/details/84495732

Guess you like

Origin www.cnblogs.com/dongxiaodong/p/11256237.html