C Language: Create INT Variables
In the C Programming Language, you can create variables that store integer (int) values.
Syntax
The syntax for declaring an int variable is:
int name1 [= value1];
Or the syntax for declaring multiple int variables is:
int name1 [= value1], [name2 [= value2], ... name_n [= value_n]];
name1 is the name of the first variable to declare.
value1 is optional. It is the value to assign to name1. You do not need to initialize the variable in the declaration statement.
name2, ... name_n are optional. These are additional variable names with the same data type that you wish to declare.
value2, ... value_n are optional. These are the values that you wish to assign to name2 through name_n.
Note
- Each declaration statement must end with a semicolon.
Example #1 - Declaring a variable
You can define a variable as an integer. For example:
int age;
In this example, the variable named age would be defined as an integer.
Below is an example C program where we declare this variable:
#include <stdio.h>
int main()
{
int age;
age = 10;
printf("TechOnTheNet.com is over %d years old.\n", age);
return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."
Example #2 - Declaring a variable and assigning a value
You can define a variable as an integer and assign a value to it in a single declaration. For example:
int age = 10;
In this example, the variable named age would be defined as an integer and assigned the value of 10.
Below is an example C program where we declare this variable and assign the value:
#include <stdio.h>
int main()
{
int age = 10;
printf("TechOnTheNet.com is over %d years old.\n", age);
return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."
Example #3 - Declaring multiple variables in a statement
If your variables are the same type, you can define multiple variables in one declaration statement. For example:
int age, reach;
In this example, two variables called age and reach would be defined as integers.
Below is an example C program where we declare these two variables:
#include <stdio.h>
int main()
{
int age, reach;
age = 10;
reach = 100;
printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach);
return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."
Example #4 - Declaring multiple variables in a statement and assigning values
If your variables are the same type, you can define multiple
variables in one declaration statement. You can also assign the
variables a value in the declaration statement. For example:
int age = 10, reach = 100;
In this example, two variables called age and reach would be defined as integers and be assigned the values 10 and 100, respectively.
Below is an example C program where we declare these two variables and assign their values:
#include <stdio.h>
int main()
{
int age = 10, reach = 100;
printf("TechOnTheNet.com is over %d years old and reaches over %d countries.\n", age, reach);
return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old and reaches over 100 countries."
C Language: Create FLOAT Variables
In the C Programming Language, you can create variables that store floating-point (float) values.Syntax
The syntax for declaring a float variable is:float name1 [= value1];Or the syntax for declaring multiple int variables is:
float name1 [= value1], [name2 [= value2], ... name_n [= value_n]];name1 is the name of the first variable to declare.
value1 is optional. It is the value to assign to name1. You do not need to initialize the variable in the declaration statement.
name2, ... name_n are optional. These are additional variable names with the same data type that you wish to declare.
value2, ... value_n are optional. These are the values that you wish to assign to name2 through name_n.
Note
- Each declaration statement must end with a semicolon.
Example #1 - Declaring a variable
You can define a variable as a float. For example:float age;In this example, the variable named age would be defined as a float.
Below is an example C program where we declare this variable:
#include <stdio.h>
int main()
{
float age;
age = 10.5;
printf("TechOnTheNet.com is over %f years old.\n", age);
return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old."Example #2 - Declaring a variable and assigning a value
You can define a variable as a float and assign a value to it in a single declaration. For example:float age = 10.5;In this example, the variable named age would be defined as a float and assigned the value of 10.5.
Below is an example C program where we declare this variable and assign the value:
#include <stdio.h>
int main()
{
float age = 10.5;
printf("TechOnTheNet.com is over %f years old.\n", age);
return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old."Example #3 - Declaring multiple variables in a statement
If your variables are the same type, you can define multiple variables in one declaration statement. For example:float age, load;In this example, two variables called age and load would be defined as float.
Below is an example C program where we declare these two variables:
#include <stdio.h>
int main()
{
float age, load;
age = 10.5;
load = 1.4;
printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);
return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."Example #4 - Declaring multiple variables in a statement and assigning values
If your variables are the same type, you can define multiple variables in one declaration statement. You can also assign the variables a value in the declaration statement. For example:int age = 10.5, load = 1.4;In this example, two variables called age and load would be defined as float and be assigned the values 10.5 and 1.4, respectively.
Below is an example C program where we declare these two variables and assign their values:
#include <stdio.h>
int main()
{
float age = 10.5, load = 1.4;
printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);
return 0;
}
This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."
No comments:
Post a Comment