Set sail, C language basic content finishing

Source: Internet
Author: User
Tags arithmetic operators ticket

I. Concept and basic format

Program: A collection of ordered commands written to solve a problem.

Algorithm: The specific methods and steps to solve the problem.

Flowchart is a graphical representation of the algorithm. Flowcharts can be intuitive, clear, and more conducive to people to design and understand the algorithm.

Basic structure of C language

#include <stdio.h>

void Main ()

{

printf ("Hello world\n");

}

C comments in the language:

// single-line comment

/*

Multi-line comments

*/

#include <stdio.h>

void Main ()

{

printf ("Hello World");

}

Example:

#include <stdio.h>
void Main ()
{
/*
printf ("**************************************\n");
printf ("Result sheet \ n");
printf ("**************************************\n");
printf ("Name language math English sports \ n");
printf ("Zhang 110 \ n");
printf ("Li 211 one-by-one \ n");
printf ("King 311");
printf ("Zhao 411 one-by-one \ n");
*/
printf ("**************************************\n");
printf ("\ t \ t performance table \ n");
printf ("**************************************\n");
printf ("name \ t language \ t math \ t english \ t sports \ n");
printf ("Zhang \t10\t10\t10\t10\n");
printf ("Lee \t11\t11\t11\t11\n");
printf ("Wang San \t11\t11\t11\t11\n");
printf ("Zhao four \t11\t11\t11\t11\n");
}

Ii. variables and data types

Declaring variables:

Data type variable name = value;

Example: int num = 5

You can also declare multiple variables at once (in the middle, separated):

int num = 5, i = 3;

The use of variables needs to be satisfied: First declare, then assign, then use.

int A;

a=10;

int a=10;

Declare first, then assign value

Naming conventions for identifiers: see Names and meanings

1, there are letters, numbers and underscores, cannot have other special characters, can not start with a number.

2. Reserved keywords cannot be used.

3, strictly case-sensitive. Boy and boy are two variables.

4, it is suggested to use the Hump name method: The first word lowercase, the other words of the first letter capital (EmployeeName).

Data type: Numeric, non-numeric (char);

Value: integer, floating point type;

Integral type: short, integer (int), long integer type (long);

FLOAT: single-precision floating-point (float), double-precision floating-point type (double).

printf (), scanf (): Outputs and inputs, gets and outputs information that interacts with the user.

Common placeholders:%d (int),%f (float),%LF (double),%c (char),%s (string)

Accessor use method: scanf ("%d", &a);

Example:

Calculate Cylinder Volume

Float raduis,height,vol,pi=3.14;//declares two variables for saving radii and high

printf ("Please enter radius: \ n");

scanf ("%f", &raduis);

printf ("Please enter High: \ n");

scanf ("%f", &height);

vol = pi*raduis*raduis*height;

printf ("Volume vol=%0.2f of Cylinders", vol);

Example 2:

Print a small ticket for shopping

Float price=20,totalprice,realprice,discount;//toothpaste Price

Float total,change;//Payment Amount and change

int num;//The quantity of toothpaste to buy

printf ("The price of toothpaste is $20 per \ n");

printf ("Please enter the quantity of toothpaste purchased: \ n");

scanf ("%d", &num);//Gets the number of inputs

printf ("Please enter a discount: \ n");

scanf ("%f", &discount);//Get the discount you entered

printf ("Please enter payment amount: \ n");

scanf ("%f", &total);//Get Payment amount

Totalprice = price*num;//Calculated Total Price (original price)

Realprice = (1-discount) *totalprice;//calculates the amount due

Change = total-realprice;//Calculate change

printf ("*************** Shopping small ticket **************\n");

printf ("Unit price \ t number \ n");

printf ("%0.2f\t%d\t%0.2f\n", Price,num,totalprice);

printf ("Discount:%0.2f, discounted price:%0.2f\n", Discount,discount*totalprice);

printf ("Payable:%0.2f, paid:%0.2f, change:%0.2f\n", Realprice,total,change);

printf ("************************************");

Arithmetic operators: Divided into unary operators and two-tuple operators

Binary operator (operand two): +,-, *,/,% (for redundancy or modulo)

Unary operator (only 1 operands): + + (from + 1),--(auto minus 1)

Example:

int num=5;

printf ("%d\n", num++);//5

printf ("%d\n", num);//6

printf ("%d\n", ++num);//7

printf ("%d\n", num);//7

printf ("*************************\n");

printf ("%d\n", num--);//7

printf ("%d\n", num);//6

printf ("%d\n",--num);//5

printf ("%d\n", num);//5

Type conversions: auto-conversions and casts

Automatic conversion:

1. Two types of data are compatible with each other

2. The target type is larger than the source type---> range of small can be a large range of automatic conversion

Short--->int--->long--->float--->double

Cast: A loss of progress may occur during the casting process

Syntax: (data type) variable or numeric value;

Example:

int a=3,b=2;

float c,d,e,f;

c=a/b;//1.000000

D= (float) (A/b);//1.000000

E= (float) a/b;//1.500000

f= (int) c%a;//1.000000

Example: Swapping two numeric values for an input

1. Get the two values entered by the user and complete the interchange

int num1,num2,temp;

printf ("Please enter the value of NUM1: \ n");

scanf ("%d", &num1);

printf ("Please enter the value of num2: \ n");

scanf ("%d", &num2);

printf ("Before Exchange: num1=%d,num2=%d\n", num1,num2);

Exchanging values for NUM1 and num2

TEMP=NUM1;

num1=num2;

Num2=temp;

printf ("After Exchange: num1=%d,num2=%d\n", num1,num2);

Example: Calculates the area of the trapezoid based on the value entered by the user

float top,bottom,height,s;

printf ("Please enter the bottom of the ladder: \ n");

scanf ("%f", &top);

printf ("Please enter the bottom of the ladder: \ n");

scanf ("%f", &bottom);

printf ("Please enter the trapezoid high: \ n");

scanf ("%f", &height);

S= (top+bottom) *height/2;//calculated area

printf ("trapezoidal area:%0.2f", s);

Set sail, C language basic content finishing

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.