[C Language] data type and computing, C language data type Calculation

Source: Internet
Author: User

[C Language] data type and computing, C language data type Calculation

Bytes -------------------------------------------------------------------------------------------------

In actual computing, the data we are dealing with is not just an integer, so using only int in C language programs will also bring some problems.

Start with an example:

# Include <stdio. h> int main () {int a; printf ("enter a number and divide it by 3:"); scanf ("% d", & ); printf ("% d", a/3); // 3. return 0 is automatically removed from the fractional part ;}

 

Solution:

1. Use floating-point numbers for Division operations (integer and floating-point numbers for calculation, C will convert integers into floating-point numbers, and then perform floating-point calculation)

# Include <stdio. h> int main () {double a; double B; printf ("Please input two numbers in sequence, for example, 1 2, and calculate the result of division: \ n "); scanf ("% lf", & a, & B); // enter % lf printf ("the result of division is % f \ n", a/B ); // output % f return 0 ;}

 

2. Change the integer to a floating point value for calculation.

#include <stdio.h>int main(){        printf("%f", 10.3/3);  //3.333333    return 0;}

 

Data Type:

Integer

Int

Scanf ("% d ");

Printf ("% d", 5 );

 

Floating Point Number

Double

Scanf ("% lf", & );

Printf ("% f", 10.0 );

 

 

Computing:

# Include <stdio. h> int main () {// calculate the time difference between 1 hour 30 minutes and 3 hours 20 minutes/* Step: 1. use variables to store values 2. set the conversion method (formula): divide the hour by the minute difference by 60 to take the integer part, and divide the minute by the minute difference by 60 to take the remainder 3. expected result is */int hour1, minute1; int hour2, minute2; scanf ("Enter the hour and minute of time 1: % d", & hour1, & minute1 ); // read the user's input value scanf ("Enter the hour and minute of time 2: % d", & hour2, & minute2 );
Int t1 = hour1 * 60 + minute1;
Int t2 = hour2 * 60 + minute2;
Int t = t2-t1;

Printf ("Time Difference: % d hours % d minutes", t/60, t % 60); // This method is more efficient than the following method.
/*
Printf ("Time Difference: % d hour % d minute", (hour2*60 + minute2)-(hour1 * 60 + minute1)/60, (hour2*60 + minute2)-(hour1 * 60 + minute1) % 60 );
*/
Return 0 ;}

 

Average Value

# Include <stdio. h> int main (int argc, const char * argv []) {// calculate the average value of int a, B; printf ("enter two numbers :"); scanf ("% d", & a, & B); printf ("average value: % f", (a + B)/2.0 );}

Operator priority: + (positive),-(negative), * (multiplication),/(except), % (remainder), + (plus),-(minus ), = (assign value)

 

Value of the SWAp variable:

Because the program is executed in steps, if a simple value is assigned, only two identical values are obtained. In this case, the third variable is required.

#include <stdio.h>int main(){    int a = 5;    int b = 8;    int c;        c = a;    a = b;    b = c;        printf("a=%d b=%d", a, b);}

 

@ Blackeye poet <www. chenwei. ws>

 

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.