answersLogoWhite

0


Best Answer

Yes, an integer can be assigned as a float value.
But it get stored as a float value, that is an implicit type conversion occurs during compilation.Smaller data types are convertible to larger data types.

eg:

float b=12;// an integer constant is assigned to a float variable
printf("%f",b);// when printing b it will print as 12.000000

User Avatar

Wiki User

βˆ™ 15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 15y ago

In C a float value can be assigned to an integer variable but an implicit casting occurs when compiler forces a float value to be assigned as an integer. So that the digits after the decimal notation in the float value get loss after assigning a float to an integer.

eg:

int a=12.356;// Implicit casting occurs
printf("%d",a);// when printing a it shows as 12 as an integer

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can an float value be assigned to int variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you invoke function result () takes two float argument and returns an integer in c plus plus program?

You can do this by creating a forwarddeclaration of the function. You can call the forward drclared function inside the main to use it.int result(float num1, float num2);intmain(void){int value = result(3.14, 2.74);return (0);}intresult(float num1, float num2){int value = 0;// function codes goes here// you can alter the value of variable 'value'return (value);}The returning value of the 'result()' function is assigned to variable 'value' in 'main()'.


Can an short value be assigned to int variable?

Yes, but you may cause truncation error because the short variable does not necessarily have the same range as an int variable. Most modern compilers will flag this as a warning. If you know that the value of the int variable will not exceed the range of a short variable, you can explicitly prevent the warning with a typecast, i.e. someShort = (short) someInt; and the compiler will assume that you know what you are doing.


What is the initial value of a numeric variable?

When we talk about instance variables, the default initial value for a numeric variable is always '0'. Any other variable in your code must be initialized before you can use it. public class MyClass{ public int x; // 0 by default public float y: // 0 by default public MyClass{ int z; z++; // Error 'z' don't have a default value } }


What is a primitive type variable in c plus plus?

same the types used in C. that is int...char...float...


What is variable in c plus plus programming?

You declare a variable by first defining its data type and then its name followed by a semi-colon. Here is an example: int variable; The example above declares an uninitialized integer variable. You can initialize the variable by giving it a value such as "int variable = 1;". It is important to initialize your variables, because you can get errors when executing your program that the variable does not have a value or is uninitialized. Variables that are uninitialized have whatever garbage value happens to be when the program is executed. Here are all of the data types that a variable can be: *int - integer value *char - character value *bool - boolean value

Related questions

How do you invoke function result () takes two float argument and returns an integer in c plus plus program?

You can do this by creating a forwarddeclaration of the function. You can call the forward drclared function inside the main to use it.int result(float num1, float num2);intmain(void){int value = result(3.14, 2.74);return (0);}intresult(float num1, float num2){int value = 0;// function codes goes here// you can alter the value of variable 'value'return (value);}The returning value of the 'result()' function is assigned to variable 'value' in 'main()'.


What is the by default value of globally declaration variable?

int, float: 0 pointer: NULL


How do you declare a pointer variable in c?

int* pint; // instantiate a pointer to an int. float* pflt; // instantiate a pointer to a float.


Can an short value be assigned to int variable?

Yes, but you may cause truncation error because the short variable does not necessarily have the same range as an int variable. Most modern compilers will flag this as a warning. If you know that the value of the int variable will not exceed the range of a short variable, you can explicitly prevent the warning with a typecast, i.e. someShort = (short) someInt; and the compiler will assume that you know what you are doing.


What is the difference between data and variable?

data type refers to the kind of value that is held by a particular variable. For ex: an int variable contains integer value, a string holds a alpha numeric value etc. variable refers to the name of a value using which we can refer to this value. Ex: public int age = 28; here int is the data type and age is the variable.


What is the initial value of a numeric variable?

When we talk about instance variables, the default initial value for a numeric variable is always '0'. Any other variable in your code must be initialized before you can use it. public class MyClass{ public int x; // 0 by default public float y: // 0 by default public MyClass{ int z; z++; // Error 'z' don't have a default value } }


What is difference between equals and equals equals operators?

The equals operator is used for assigning a value to a variable. ex String name = "efu"; the equals equals operator is used for comparaing if the given value is equal to another value which is assigned for a variable. ex int i = 0; int j = 5; if( i == 0){ System.out.println(j+"can't be divided by"+i); }else{ System.out.println("answer is " +j/i); }


Store value in variable c plus plus?

#include<iostream> int main() { int var=42; // store the value 42 in a variable named var. return(0); }


What is the main variable?

Variables are names for which the value may be altered. That is, the value of a name is variable. For example: int x; // Declares a variable named x. int y = 0; // Declares and initialises a variable named y. x = 5; // Changes the value of the variable named x. y = x; // Changes the value of the variable named y. By contrast, constants are names for which the value does not change: const int z = 10; // Declares a constant named z, which will always have the value 10.


What defines a data type?

the type of data which we store in a variable.. example: int a=10; /*here a is variable (data) which is of type int and stores a value 10.*/


What is a primitive type variable in c plus plus?

same the types used in C. that is int...char...float...


What is variable in c plus plus programming?

You declare a variable by first defining its data type and then its name followed by a semi-colon. Here is an example: int variable; The example above declares an uninitialized integer variable. You can initialize the variable by giving it a value such as "int variable = 1;". It is important to initialize your variables, because you can get errors when executing your program that the variable does not have a value or is uninitialized. Variables that are uninitialized have whatever garbage value happens to be when the program is executed. Here are all of the data types that a variable can be: *int - integer value *char - character value *bool - boolean value