Ticker

6/recent/ticker-posts

Data Types and Variables

 Chapter 4: Data Types and Variables



 




4.1
Data Types:



In C, data types specify the kind of values that
variables can hold. The following are the basic data types in C:



 



·        
`int`: Used for representing whole
numbers (e.g., -10, 0, 42).



 



·        
`float`: Used for representing
single-precision floating-point numbers (e.g., 3.14, -0.5).



 



·        
`double`: Used for representing
double-precision floating-point numbers with higher precision than `float`
(e.g., 3.14159, -0.123).



 



·        
`char`: Used for representing individual
characters (e.g., 'a', 'B', '#').



 



·        
`void`: Represents the absence of a
value. It is commonly used as a return type or for indicating no arguments in
functions.



 



·        
Additional data types, such as `short`,
`long`, `unsigned`, and `signed`, can modify the range or behavior of the basic
data types.



 



4.2
Variables:



 



A variable is a named storage location that holds a
value of a specific data type. Before using a variable, it needs to be declared
with its data type. The general syntax for declaring a variable is:



 



 





data_type
variable_name;





 



Here's an example of variable declaration and
initialization:



 





#include
<stdio.h>



int
main() {



    int age;              // Declaration



    age = 25;             // Initialization



    float weight = 68.5;  // Declaration and initialization



    printf("Age: %d\n", age);



    printf("Weight: %.1f\n", weight);



   



    return 0;



}





 



 



        
·           
`int age;`: Declares an integer variable
named `age`.



 



        
·           
`age = 25;`: Assigns the value 25 to the
`age` variable.



 



        
·           
`float weight = 68.5;`: Declares and
initializes a float variable named `weight` with the value 68.5.



 



        
·           
`printf()`: The `printf()` function is
used to print the values of variables. `%d` and `%.1f` are format specifiers
used to indicate the types of variables to be printed.



 



        
·           
Note: The format specifier `%d` is used
for integers, `%f` for floats, and `%c` for characters.



 



Compile and run the program using the instructions
provided in the previous chapter. The output should display the values of the
variables `age` and `weight`.



 



4.3
Constants:



Constants are fixed values that cannot be changed
during program execution. They can be of any data type and are typically used
to represent values that remain constant throughout the program. Constants can
be declared using the `const` keyword:



 





const
data_type constant_name = value;





 



Here's
an example:



 





#include
<stdio.h>



int
main() {



    const int MAX_SCORE = 100;



    const float PI = 3.14159;



    const char NEW_LINE = '\n';



    printf("Max Score: %d\n",
MAX_SCORE);



    printf("PI: %.2f\n", PI);



    printf("New Line: %c", NEW_LINE);



    return 0;



}





 



 



·        
`const int MAX_SCORE = 100;`: Declares a
constant integer `MAX_SCORE` with the value 100.



 



·        
`const float PI = 3.14159;`: Declares a
constant float `PI` with the value 3.14159.



 



·        
`const char NEW_LINE = '\n';`: Declares
a constant character `NEW_LINE` with the newline character (`\n`).



 



·        
The program uses `printf()` to display
the values of the constants.



 



Compile and run the program to see the output, which
should show the values of the constants.



 







 

Post a Comment

0 Comments