1.1 Arithmetic expressions

Source: Internet
Author: User

Since the C + +, which was directly learned from C + +, though C + +, is somewhat different, I simply start with the simplest:

Program 1-1 calculates and outputs a value of 1 + 2


1 #include <stdio.h>2int main ()3{4 printf ( " %d \ n " 1 2 ); 5 return 0 ; 6 }

There is no difficulty,%d is a placeholder,

When compiling, the following values are brought in, where D is the decimal.

  Extended experiment: Change 1+2 to 8/5

You can see that the output is 1, not 1.6, for the same reason as C + +: 8/5 The exact meaning is 8 divided by the integer portion of 5.

To get 1.6 is also easy.

Program 1-2 calculates and outputs a value of 8/5 and retains 1 digits after the decimal point

1 #include <stdio.h>int main (){4 printf ("%.1f \8.0/5);  change the original%d to%.1f0;  7}           

  

Note: The percent symbol follows a decimal point followed by the number 1, which represents the exact 1 digits after the decimal, and finally the lowercase f.

8 instead of 8.0, the reason is also simple in the world of computers integer-to-integer operations result in integers, and when a floating-point number is calculated, the result is a floating-point number.

Summarize:

1. Integer output with%d, floating point output with%f.

2. Integer/integer = integer; floating-point/float = floating-point number

Program 1-3 calculation of complex expressions

1#include <stdio.h>2#include <math.h>//******************************3 intMain ()4 {5printf"%.8f \ n",1+2* SQRT (3) / (5-0.1));6     7     return 0;8}

There's nothing to note here, just know that when you encounter a mathematical function in your program, you have to include MATH.H in your header file

1.1 Arithmetic expressions

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.