Home > Backend Development > C++ > In C language, access uninitialized integer and floating-point variables

In C language, access uninitialized integer and floating-point variables

王林
Release: 2023-09-08 11:25:02
forward
1252 people have browsed it

In C language, access uninitialized integer and floating-point variables

Question

Declaring uninitialized int and float variables in C and trying to print their values. Explain what will happen.

Solution

  • If a variable is declared but not initialized, or not initialized, and if these variables try to print, then it will return 0 or some garbage value.

  • Whenever we declare a variable, a location is assigned to the variable. The only problem is that, by initializing, we are trying to occupy a memory location that was already allocated at declaration time.

  • But in the following program, we have not initialized the value in the reserved memory location. However, by default these locations are occupied by 0 or garbage values. When we try to print it shows 0 or garbage value as output.

Example

The following is a C program to access int and float variables-

Live Demo

#include<stdio.h>
int main(){
   float a,b,c;
   int x,y,z;
   printf("value of a:%f</p><p>",a);
   printf("value of b:%f</p><p>",b);
   printf("value of c:%f</p><p>",c);
   printf("value of x:%d</p><p>",x);
   printf("value of y:%d</p><p>",y);
   printf("value of z:%d",z);
   return 0;
}
Copy after login

Output

When the above program is executed, the following results will be produced-

value of a:0.000000
value of b:0.000000
value of c:0.000000
value of x:1512368
value of y:0
value of z:27
Copy after login

The above is the detailed content of In C language, access uninitialized integer and floating-point variables. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template