C language of Cocos2d-x Series 2

Source: Internet
Author: User
Tags constant definition

1. macro definition
<1>. Constant Definition

# Define MATH_PI 3.14 void defineH () {// The macro replaces the constant corresponding to the macro in the compilation phase, so it is very fast; printf ("% f", MATH_PI );}

<2>. method definition

// Define the macro method. The macro method does not have A specific return type. When multiple rows exist, add A backslash; # define MAX (A, B) {\ A> B? A: B \}

Call: MAX (12, 30 );

2. struct
<1>. struct Definition

struct People {    int age;    char *name;};

Usage:

    struct People p;    p.age = 12;    printf("%d", p.age);

<2>. struct pointer

/*** Stdlib needs to be introduced. h */void structpointer () {// declare a People pointer (requires initialization) and allocate a memory address, the size of the memory is the size of the struct People * p = malloc (sizeof (struct People); p-> age = 20; p-> name = "ls "; struct People * p1 = p; p1-> name = "ls1"; printf ("hello struct People % s", p-> name ); // The output result is ls1 // the pointer allocated with malloc needs to be released free (p) after it is not needed );}

3. function pointer

/*** Function pointer */void functionpointer (int I) {printf ("hello functionpointer % d \ n", I );}
Int functionpointer1 () {printf ("hello functionpointer1 \ n"); return 0 ;}
Void functionpointerexe () {void (* p) (int); p = functionpointer; p (34); int (* a) (); a = functionpointer1; ();}

4. typedef keyword
<1>. Define struct

typedef struct {    int age;} P1;

After being defined by typedef, you no longer need to add the struct keyword before declaring P1 struct;

    P1 p;    p.age = 12;    printf("%d", p.age);

<2>. Define the function pointer

typedef int (*Void)();
Void v = functionpointer1; v (); // output functionpointer1

5. Introduce custom header files

Hello. h

// If HELLO_H _ is not defined, HELLO_H _ is defined to prevent repeated header files; # ifndef HELLO_H _ # define HELLO_H_void sayHello (); # endif/* HELLO_H _*/

Hello. c

# Include <stdio. h> // the specific implementation of sayHello in hello. h void sayHello () {printf ("hello c ");}

Main. c

#include "hello.h"int main(int argc, const char * argv[]) {    sayHello();}

6. File Operations

Read:

Void fileRead1 () {FILE * f = fopen ("xx.txt", "r"); fseek (f, 0, SEEK_END); // seek to the end of the FILE: obtain the file size long size = ftell (f); // returns the file size char buf [size + 1]; fseek (f, 0, SEEK_SET ); // return to the file and start fread (buf, sizeof (unsigned char), size, f); buf [size] = '\ 0'; fclose (f ); printf ("% s", buf );}

Write:

Void fileWrite1 () {// f may be null and must be determined! FILE * f = fopen ("xx.txt", "w"); fprintf (f, "hello c"); fclose (f );}

7. Others

<1>. Generate a random number:

    srand((int) time(NULL ));    int randNum = rand();    printf("%d,", randNum);    return 0;

<2>. String operation:

    char buf[100];    memset(buf, 0, 100); //     sprintf(buf, "hello c %d s%f,", 12, 23.2);    printf("%s", buf);

<3>. Input and Output:

Void getput () {char buf [100]; gets (buf); // wait for the user to enter puts (buf);} int scanprint () {int; scanf ("% d", & a); printf ("% d", a); return 0 ;}

 

Related Article

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.