Python programming intro, Schemes and Mind Maps of Computer Programming

It is very use fulbsbsnnssnnsnsnsnsnsnebejsjamannzbsbehjwhnsbnanzbsnsb

Typology: Schemes and Mind Maps

2021/2022

Available from 08/02/2023

manosakthi-thiyagarajan
manosakthi-thiyagarajan 🇮🇳

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Variables
In today's lesson, we will discuss variables in C programming.
Imagine a glass of water. When you want to drink water, you pour
it into the glass and then drink from it. You can think of a variable
as a glass of water. Just like a glass can hold water, variables can
hold values. Once you store a value in a variable, you can use
that value in your program.
In reality, variables are simply names that point to specific
memory locations. Thanks to the inventor of the C language, it is
easy for us to store values in memory by just using a name of our
choice. You don't need to worry about the exact memory location
where your value is stored. Your task is to provide the name, and
internally it will be associated with a memory location.
However, there is one important thing to note. You must declare
variables before using them in your program. Declaration means
announcing the properties of the variable to the compiler.
Properties of a variable refer to its size and name. Declaring a
variable is a simple task. It involves specifying the variable's
name and data type, which determines the amount of memory it
will occupy. In most cases, declaration and definition are done
simultaneously, but it can vary depending on the modifiers used
with the variables. Let's look at an example of declaring a
variable:
int var;
In this example, the variable name is "var" and its data type is
integer. By writing "int var", you are declaring the variable and
requesting the compiler to allocate memory for it. It is important
to remember to end the declaration statement with a semicolon,
as it separates one statement from another.
The amount of memory allocated for a variable depends on its
data type. In the case of an integer data type, it could occupy
either two or four bytes of memory, depending on the system you
are working on.
pf3

Partial preview of the text

Download Python programming intro and more Schemes and Mind Maps Computer Programming in PDF only on Docsity!

Introduction to Variables In today's lesson, we will discuss variables in C programming. Imagine a glass of water. When you want to drink water, you pour it into the glass and then drink from it. You can think of a variable as a glass of water. Just like a glass can hold water, variables can hold values. Once you store a value in a variable, you can use that value in your program. In reality, variables are simply names that point to specific memory locations. Thanks to the inventor of the C language, it is easy for us to store values in memory by just using a name of our choice. You don't need to worry about the exact memory location where your value is stored. Your task is to provide the name, and internally it will be associated with a memory location. However, there is one important thing to note. You must declare variables before using them in your program. Declaration means announcing the properties of the variable to the compiler. Properties of a variable refer to its size and name. Declaring a variable is a simple task. It involves specifying the variable's name and data type, which determines the amount of memory it will occupy. In most cases, declaration and definition are done simultaneously, but it can vary depending on the modifiers used with the variables. Let's look at an example of declaring a variable: int var; In this example, the variable name is "var" and its data type is integer. By writing "int var", you are declaring the variable and requesting the compiler to allocate memory for it. It is important to remember to end the declaration statement with a semicolon, as it separates one statement from another. The amount of memory allocated for a variable depends on its data type. In the case of an integer data type, it could occupy either two or four bytes of memory, depending on the system you are working on.

When it comes to variables in programming, you don't need to worry about how much space is allocated to them. You only need to focus on declaring and defining the variable. If you assign a value to a variable at the time of declaration, it is called initialization. However, initializing a variable does not mean that you cannot change its value later in your code. You can still change the value if needed. A variable is something that can vary over time. On the other hand, a constant is just the opposite - once defined, it will never change. Let's see how we can change the value of a variable after initialization: #include <stdio.h>int main() { int var = 3; // Initialization var = 4; // Changing the value printf("%d", var); // Printing the value return 0;} In the above code, we first declare a variable named "var" and initialize it to 3. Then, we change its value to 4. Notice that we do not need to write "int var" again when assigning a new value. This is because memory is allocated to the variable only once and redefining it would be illegal. It is important to note that each variable should be defined only once in your program, but it can be used multiple times with different assignments. There is an exception to this rule when we study scope rules, but for now, know that you cannot define multiple variables with the same name inside the same function. The printf function is used to print the value of the variable. The "%d" inside the printf statement is a format specifier that tells the function to expect an integer value. I can only tell you that this will print the contents of whatever is there inside this variable var. You have to put return 0 at the end.