Objective-C prelude-C Language

Source: Internet
Author: User
Tags float double

The premise of this article is to understand Java or other languages. Because C has too much knowledge to complete, it is just a difference between Java and Java.

I am planning to write about iOS development recently. iOS development is inseparable from OC. OC must first understand C. This blog was born for this.

The basic things will not be introduced much, otherwise there will be too much C, which will be compared with Java


I. Basic Data Types


Java data type and length int 4 byte double 8 byte float 4 byte long 8 byte short 2 byte Boolean 1 byte char 2 byte 1 byte

C Data Types: Char 1, int 4, float 4, double 8, signed, unsigned, long 4, short 2 and void

Java

Int 4

Doulbe 8

Float 4

Long 8

Short 2

Boolean 1

Char 2

Byte 1

C

Int 4

Doulbe 8

Float 4

Long 4

Short 2

None (0 false 1 true)

Char 1

None

In C language, the data type is less than that in Java. in C language, there is no boolean data. How does one represent the data type? Int 1 represents true 0 represents false

In C, data of the string type is not used. In Java, a string is represented. In C, a string is represented by an array of the char type.

The C language does not have the byte type, so the char type is used to represent the byte type.

Code:


#include <stdio.h>
#include <stdlib.h>
// sizeof (); a function of C language can get the length of a certain data type int
 main ()
{//% d Similar to sql statement? Placeholder
            printf ("char length is% d \ n", sizeof (char));
            printf ("The length of int is% d \ n", sizeof (int));
            printf ("The length of the float is% d \ n", sizeof (float));
            printf ("The length of double is% d \ n", sizeof (double));
            printf ("The length of long is% d \ n", sizeof (long));
            printf ("The length of short is% d \ n", sizeof (short));
            
            // signed, unsigned, data type modifiers
           // signed int; represents signed int data unsigned int; unsigned int data
              printf ("signed int is% d \ n", sizeof (signed int));
              printf ("The length of the unsigned int is% d \ n", sizeof (unsigned int));
        
           // symbol modifier can only modify integer type data long int
           // cannot modify floating-point data float double
           // printf ("The length of the signed float is% d \ n", sizeof (signed float));
        
              system ("pause"); // Invoke the system command under windows to suspend the execution of the program. It is convenient to observe the execution result of the program.
}

Result:



Ii. Basic Input and Output Functions

% D-int short

% LD-long int

% C-Char

% F-float

% Lf-double

% X-hexadecimal output int, long int, or short int

% O-octal output

% S-string

Code:


#include <stdio.h>
#include <stdlib.h>
  main () // entry point of the program
{int i = 3;
      float f = 3.1415;
      double d = 6.2815001; // accurate to 6 digits after the decimal point, so there is no last digit when typing
      char c = 'A'; // Define characters by single quotes
      short s = 2;
       
      printf ("int i =% d \ n", i);
      printf ("float f =% f \ n", f);
      printf ("char c =% c \ n", c);
      printf ("double d =% lf \ n", d);
      printf ("short s =% d \ n", s);
     
      / * char arr [20]; // Define an array of length 20
      scanf ("% s", arr); // accept a string from the keyboard and place it in the c array
     
      printf ("arr =% s \ n", arr);
        * /
       
      int j;
      scanf ("% d", & j); // & stands for taking the address
      printf ("j =% d \ n", j);
        
      system ("pause"); // Invoke the system command under windows to suspend the execution of the program. It is convenient to observe the execution result of the program.
}


Result:



3. pointer

The address of the memory stick has been allocated at the factory, for example, 0x00002f.

What is a pointer? For example, int I = 3; the alias 0x00002f is I, and the value in the memory 0x00002f is 3.

In this case, int * P; P = & I; then the pointer Variable P is the address of I, and the printed value is 0x00002f. * P is the value in the memory, that is, 3, therefore, changing the value of P does not affect the value of I. Changing the value of * P affects the value of I. That is to say, * P and I actually represent the same variable, representing the same memory space.

The pointer understands. Here is an example to exchange two numbers.


void swap2 (int * p, int * q) {// The parameters passed are the addresses of the i and j variables
     // * p means i * q means j
     int temp;
     temp = * p;
     * p = * q;
     * q = temp;
}


void swap1 (int i, int j) {// The parameters i and j are two different variables from i and j in the main function
           printf ("Subfunction i address% # X \ n", & i);
           printf ("Subfunction j address% # X \ n", & j);
           int temp;
           temp = i;
           i = j;
           j = temp;

}
 main ()
{
           // Using pointers, you can modify the data in the main function in the subfunction
    
           int i = 3;
           int j = 5;
           printf ("i =% d \ n", i);
           printf ("j =% d \ n", j);
           printf ("Main function i address% # X \ n", & i);
           printf ("Main function j address% # X \ n", & j);
            
           
           
           // swap1 (i, j);
           
           swap2 (& i, & j);
           printf ("After swapping \ n");
           printf ("i =% d \ n", i);
           printf ("j =% d \ n", j);
    
    
            system ("pause"); // Invoke the system command under windows to suspend the execution of the program. It is convenient to observe the execution result of the program.
} 

The swap1 function cannot change the values in the main function. Because swap1 parameters I and j are copies of I and J in the main function, they are not the I and J of the main function, but the pointer does.

To be continued...

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.