Black Horse Programmer _ C Language Foundation (ii)

Source: Internet
Author: User
Tags modifiers

Overview Today the basics are divided into the following points (note: Loops, conditional statements are not included here): 1.Hello World 2. Operation Process 3. Data type 4. Operators 5. Common FunctionsHello World since is the iOS development series first look at the run of C in Mac OS x open xcode Select command Line program fill in the project name and choose to use C language Select Save directory automatically generate the following code OK, on Xcode we write our own program as follows
  main.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h>void showmessage () {    printf ("hello,world!\n");} int main (int argc, const char * argv[]) {    showmessage ();    return 0;}

In the above procedure we need to explain several points:

The main function is the program entry, a program can have only one main () function, need to have an integer return value (in fact, the return value int can be omitted, but this does not mean that it does not return a value, but the default is int; we can also not provide a return in the main () function, This is because C language grammar requirements are not rigorous);

#include是预处理指令, which is used to include the specified file (note the pre-translation process), it actually does the job of copying the corresponding file to the specified location; the content can be any type of file, not just. h file;

The ShowMessage function above must be written above the main () function, and if written below it must be declared before the main () function;

Note: There are two ways to #include include files: Use <> and. The difference is that <> contains only the compiler library function files, so it is suitable for containing library functions, and "" contains the first to find the program's current directory, if not found, find the library function path, so it applies to the customization file;

Run process

The C language runs in two big strides: compiling and linking

Compile: Compile phase will compile the corresponding xxx.c source file (ASCII format) to the target file xxx.obj, it is binary format (of course, we will have more than one. c file, will also generate multiple corresponding. obj); pre-processing (for example, # include directives) before compiling. A syntax check is also performed while compiling, and the resulting. obj file cannot be executed separately because each. obj is associated, and they each refer to the C language library function;

Link: The process of linking the individual. obj files with the C language library functions to generate an executable file;

Extended

It is unrealistic to write all of the code in a program in a large project development, and we typically divide a sub-operation into two files:. c files and. h files. Implement the corresponding function in the. c file and declare the function in. h so that the sub-operation can be separated without regard to the order problem as long as the corresponding header file is included above the main function. For example, rewrite the example of "Hello World" (note that the. C and. h filenames are completely different, but for canonical purposes we still take the same file name):

Message.h

  message.h//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//void showmessage ();

Message.c

  message.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h>void showmessage () {    printf ("hello,world!\n");}

Main.c

  main.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h> #include "Message.h" int main (int argc, const char * argv[]) {    showmess Age ();    return 0;}

You can see that the program is still working, but we're thinking about a question: if we don't divide into two files, will it be possible to include MESSAGE.C directly in the main function file? The answer is no, because the compiled two files generated by Main.obj and Message.obj in the link will be found in main.obj already have message.obj defined in the ShowMessage function, throw "identifier duplicate" error.

Data type

Type modifier

We can clearly see the data type structure of the C language, and of course we have some type modifiers (or qualifiers) for these types.

short, modifier int, double

Long, modifier int, double

Signed signed, modified int, char

unsigned unsigned, modifier int, char

The following explanation is required for type modifiers

These modifiers are often used to modify the int type, and int can be omitted when the int is modified;

Short and long will change the length of the int type, the length of different compiler items is not the same, but the short length is not much longer than int,int length;

singed, unsigned does not change the type length, only indicates whether the highest level is a sign bit, signed represents a positive number greater than or equal to 0;

Of course, sometimes we have to be aware of the bytes occupied by each type, and the following table lists the storage space used by common data types

Note: The char type is the smallest unit of data type and occupies 1 bytes under any type of compiler, and variable assignments of type char can be assigned directly to a character or to an integer (the corresponding ASCII value).

Operator

There are 34 operators in C, not much different from languages such as C #, Java, and here are some caveats

The relational operator returns 1 for true, 0 for false, and true for non-0 in the conditional language (negative, positive is true), and only 0 is false;

The C language can not save the value of the relational operator;

The final value of the comma expression is the value of the last expression;

For the above points, see the following example

  main.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h>int main (int argc, const char * argv[]) {    int a=2>1,b=2<1,c=99,d= 0;    int f=0,g=0,h=0,e= (f=3,g=4,h=5);        a>0;//did not save the result        of the operation printf ("%d,%d\n", A, b);//Result: 1,0        if (c) {//Can pass        printf ("true.\n");    }    if (d) {//Cannot pass        printf ("false\n");    }        printf ("%d\n", e);/results: 5    return 0;}

Common functions printf () functions

The printf () function is used to output data to a standard output device, with a format character that can perform powerful output functions, which we have already used in the example above.

Usually our output is not fixed content but contains some variables, this time we need to use the format, commonly used format characters are as follows

We can precisely control the output width of the format character and the number of decimal points of floating-point numbers.

  main.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h>int main (int argc, const char * argv[]) {    int a=16;    float b=79.3f;    printf ("[a=%4d]\n", a);    printf ("[a=%-4d]\n", a);    printf ("[b=%10f]\n", b);    printf ("[b=%.2f]\n", b);    printf ("[b=%4.2f]\n", b);    return 0;}

The operation results are as follows

From the running results we are not difficult to find a positive number before the format character% can be set front-end, negative number set back-end alignment, if the total length of the data exceeds the decorated length of the set, then the actual length of the display; the integer after the decimal point is used to control the length of the decimal place after the point.

scanf () function

The scanf () function is used to receive input data from a standard input device

  main.c//  C language Foundation////  Created by Kenshin Cui on 14-7-12.//  Copyright (c) 2014 Cmjstudio. All rights reserved.//#include <stdio.h>int main (int argc, const char * argv[]) {    int a,b,c;    scanf ("%d,%d,%d", &a,&b,&c);//You need to enter at this time:--and then enter    printf ("a=%d,b=%d,c=%d\n", a,b,c);    return 0;}

For the scanf () function We need to emphasize a few points

    1. Parameter received with carriage return for end operation
    2. If you need to receive more than one parameter, the delimiter between multiple arguments is arbitrary, but if the delimiter is a "space" then the delimiter can make spaces, tabs, and carriage returns (the last carriage is considered a terminator

Black Horse Programmer _ C Language Foundation (ii)

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.