An example analysis of automatic implicit conversion and type casting in C language _c language

Source: Internet
Author: User
Tags type casting

In this paper, we analyze the attention points of automatic implicit conversion and type casting in C language through a C program instance, as follows:

First look at a C program:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
double proc (int q) {
int n;
Double sum,t;//The key to this example is sum = 2.0 on the types of these variables
;
while (sum<=q) {
t=sum;
sum = sum+ (n+1)/n;//automatically implicitly converts
sum =sum + double ((n+1)/n);/type cast
n++;
return t;
}
void Main () {
system ("C");
printf ("%f\n", proc)
}

Analysis:

When we see sum = sum+ (n+1)/n; Such a statement always casts an unthinking force on the variable n, but there is another word in C. "When a low precision data type and a high-precision data type operation, the result of the operation is a high-precision type." "Don't be confused at this time, it just emphasizes the result." In order for others to understand what we want to do with the data, it is best to cast it. In this case, the so-called low precision to high-precision automatic implicit conversion is actually a forced type conversion.

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.