[C language] data types and calculations

Source: Internet
Author: User

-------------------------------------------------------------------------------------------------

The actual calculation of the data we contact is not only the integer type, then the C language program using only int will also bring some problems.

Start with an example:

#include <stdio.h>int  main () {    int  A;    printf (" Enter a number, get the result divided by 3:");    scanf ("%d", &a);    printf ("%d", A/3); // 3, the fractional part is automatically abandoned.    return 0 ;}

Workaround:

1. Use floating-point numbers for division operations (integers and floating-point numbers to do operations, C will convert integers to floating-point numbers, then floating-point arithmetic)

#include <stdio.h>intMain () {DoubleA; Doubleb; printf ("Please enter two numbers, such as 1 2, to calculate the result of dividing: \ n"); scanf ("%LF%LF", &a, &b); Input with%LF printf ("The result of the division is:%f \ n", A/b); %f for Outputreturn 0;}

2. Change the integer value directly to a floating-point value participation operation

#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", &a);

printf ("%f", 10.0);

Calculation:

#include <stdio.h>intMain () {//calculates the time difference between 1 hours and 30 minutes and 3 hours 20 minute    /*steps: 1. Use a variable to store the value 2. Set the conversion method (Formula): The hour takes the minute difference to divide by 60 to take the integer part, the minute is the minute difference divides 60 takes the remainder 3. Get Results*/        intHour1, minute1; intHour2, Minute2; scanf ("Please enter the hour and minute of time 1:%d%d", &hour1, &minute1);//read-In user input valuesscanf"Please enter the hour and minute of time 2:%d%d", &AMP;HOUR2, &minute2);
int T1 =HOUR1 * + minute1;
int t2 = HOUR2 * + minute2;
int t = T2-T1;

printf ("Time difference is%d hours%d minutes", T/60, t%60); More efficient than the following way
    /*
printf (" time difference is%d hours%d minutes ", (hour2*+minute2)-(hour1* +minute1) )/, ((hour2* +minute2)-(hour1* +minute1));
*/
return 0 ;}

Averaging

#include <stdio.h>intMainintargcConst Char*argv[]) {    //averaging    intb; printf ("Please enter two number:"); scanf ("%d%d", &a, &b); printf ("average value is:%f", (A + B)/2.0);}

Operator Precedence: + (positive),-(negative), * (multiply),/(except),% (remainder), + (plus),-(minus), = (Assignment)

The value of the interchange variable:

Since the program is step-up, if you simply assign a value, and then only get two identical values, you need a third variable.

#include <stdio.h>int  main () {    int5;     int 8 ;     int C;         = A;     = B;     = C;        printf ("a=%d b=%d", A, b);}

@ Black eyed poet <www.chenwei.ws>

[C language] data types and calculations

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.