Learn notes 1-c language basic Grammar __c language

Source: Internet
Author: User
Tags float double

1 C Language Program composition Way

1 C Language Program is composed of functions, each function can implement one or more functions.

2 A regular program can have multiple functions, but there is only one main function.

3 The function is executed only when it is called, and the main function is executed by the system call.

4 The format of the function must be written in accordance with the specification.

5 C language program file suffix is. c

2 a simple C program.

1 in Xcdoe to create a C language program, open xcode--Click Create a new Xcode project--click the OS x--right select Commad Line tool--next--product name to fill in the title, Organi Zation name, organization identifier fill in the reverse of the company URL. Language Select C.


Simply print a sentence.

///MAIN.C//
a simple C program////
Created by Ywxkdz on
15/11/14.
Copyright (c) 2015 Itcast. All rights reserved.

#include <stdio.h>

int main (int argc, const char * argv[]) {

printf ("Hello, world!\n");
return 0;

}


3 C language Execution process


SOURCE program---Compilation--link--Execution


Compiling: Compiling the source program to build. O Target file (shortcut command +b)

Link: A link library file that generates an executable file for the. Out of the target file. (Shortcut command +r)


4 identifier of the C language

Identifiers are divided into keywords, predefined identifiers, and user identifiers.


1) Identifier naming principle

A, can only have letters, numbers, underscores and dollars ($) composition.

b, you can't start with a number.

c, you cannot duplicate the keyword.

D, strict case sensitivity.

2) identifier naming specification

A, the Hump naming act. (First letter lowercase, other words first letter in uppercase). GetName.

b, the name is concise, the hope text knows the meaning

C, avoid digital numbering. Name1,name2.

D, a global variable or function name that is used by multiple files plus a range qualifier. Ui_name

3 keyword is the use of C itself, can not be used for his words. A total of 32.


Data type keywords: void int char float double (5)

Type modifier keywords: short long signed unsigned (4)

Complex Type keywords: struct enum Union (3)

Process Control Keywords: for-break continue do-if else goto Suitch case default return (12)

Storage type keywords: auto extern static regist (4)

Other keywords: const sizeof typedel volatile (4)

5 Notes

Explanatory text for the code.

Role: facilitate communication between programmers.

Feature: Do not participate in compiling.

Annotation categories

1) single-line category (//comment content). Comment Single line only

2 Multi-Line Classification (* * Comment content * *). Comment Multiple lines, and note text can be wrapped between lines. Shortcut keys (Command +.) )

6 Data types



Common basic data types occupy space (64-bit machine for example)

Char:1 bytes

Int:4 bytes

Float:4 bytes

Double:8 bytes

Basic type Writing

Integer

A, the default is 10, 10, 20.

b, starting with 0 as 8, 045,021.

C., starting with 0b 2, 0b11101101.

D, starting with 0x 16, 0X21458ADF.


Decimal

Single-precision constants: 2.3f.

Double-precision constants: 2.3. The default is double precision.


Character-type constants

Enclosed in English single quotes, save only one character ' a ', ' B ', ' * ' and an escape character ' \ n '. ' t '


String constants

You can save multiple characters by using double quotes in English. "ABC"


The concept of 7 variables

A variable represents a memory space, and the value can be changed. is the basic unit of data, consisting of categories and variable names.

1 variable definition Format: Variable type variable name, variable name ...; You can define multiple variables at once, separated by commas.

int A, b,c;

Note: Variable name naming conventions follow identifiers naming conventions.


2 initialization of the variable

A, the definition variable is initialized at the same time

Fully initialized. Variable type variable name = value, int a=10,b=20;

• partially initialized. int a =10,b; A initialization, b not initialized.

B, first define the variable and initialize it.

int a,b,c;

a=10;

b=20;

c=30;

c, use variables to initialize variables.

int a=10;

int b=a;

d, variable continuous assignment

int a=b=10;

Note: The variables must be initialized when they are defined, and the uninitialized variables may produce random numbers when they are used.


3 Scope of the variable

Variables are divided into global variables and local variables.

Local variables: variables defined within a function or within a code block, where the scope is from the variable definition position to the end of the code block. When the code block ends, the variable is destroyed

Global variables: Variables defined outside the function, scoped from the defined position to the end of the program, and the variable is destroyed only when the entire program is run.

Note: The variables defined inside the code block mask the variables outside the code block.

8 printf functions are used.

Need to introduce standard input output header file in use # includ <stdio.h> no semicolon

1 format printf ("Format placeholder", variable list); The number of format placeholders should be the same as the number of variable lists.

2) Common placeholder

%d output 10 binary integers

%MD output M bit, not enough left to fill space, other normal output.

%0MD output m bit, less than 0, other normal output.

%c Output string

%f Output Real number

The default is 6 decimal digits, the number of valid digits is 7, and the end is less than 0.

%.2f output Two decimal numbers, rounded.

%M.NF output m bit where decimal n bit. Rounded, decimal less than 0, integer insufficiency (m-n) bit left complement space.

%M.NF output m bit where decimal n bit. Rounded, decimal less than 0, integer insufficiency (m-n) bit right complement space.

%s output string.

%o Output 8 in Number

%x Output 16 Number

%p Output Variable memory address

Escape character

\ t Output tab

\ n Output Line wrap

\ output \

%% OUTPUT%


9 Why does a variable distinguish between types

1) Reasonable use of memory

2 data storage format is different

3 The operation of the data is different


scanf function (blocking function)

Blocking functions are executed and wait for user input before they can proceed.

Function: Receive input from keyboard

Format: scanf ("Format control character", variable address list); & Take the address of the variable.

Usage considerations

1 when a single variable is received, the space entered, the carriage return, and the tab characters are ignored before the value is entered.

2 The default multiple values are separated by a space. The Middle Space Carriage return tab will be ignored.

3 if the format control character is set to, separate, it needs to be used when entering.

4%*c skips a character%d skips an integer.

5 cannot use \ n for scanf use.


Operation Principle of one scanf


1 when the user enters the content, the content is stored in the scanf input buffer, and then the value is taken from the buffer according to the format control character requirements

If the value of the buffer is the same as the required format, the value is assigned to the variable and cannot be assigned if the format is inconsistent.


Example: scanf ("%d%c%d", &a,&b,&c);

Console input 20 a back car

In this case, a 20 contains spaces stored in the buffer.

10 assigned to variable A in%d format when value is taken

The buffer then assigns the space to the variable B when the value is taken down in the%c format.

Then the value in%d format, at which time the buffer will take a character is not in the format, not assigned

A=10,b= ' A ' C is not assigned after assigning a value.

It is recommended that you enter mixed values with commas to separate


2 If there is content in the buffer, we will not be prompted to enter a value. Carriage returns are also received when you enter.

scanf ("%c", ch); You can receive a carriage return.


















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.