Introduction to Hexadecimal Values in C
- Prefixing a number with
0xdenotes a hexadecimal value. - Format specifier
%xprints hexadecimal in lowercase;%Xprints in uppercase. - Example:
0x3FFprints as3ffwith%x, and3FFwith%X. - Decimal output requires converting hexadecimal to decimal.
Memory Layout in C Programs
C programs are divided into distinct memory segments:
Text Segment (Code Segment)
- Stores the actual compiled machine code.
- Read-only to prevent modification during execution.
Data Segment
- Contains initialized global, static, and constant variables.
- Divided into:
- Read-only: for constant globals.
- Read-write: for initialized variables.
BSS Segment (Uninitialized Data Segment)
- Stores uninitialized global and static variables.
- Variables here are automatically initialized to zero by the system.
Stack and Heap Segments
- Stack: Manages function call frames and local variables.
- Heap: Used for dynamic memory allocation.
Behavior of Static Variables
- Uninitialized static variables are stored in the BSS segment and default to zero.
- Initialized static variables with non-zero values are stored in the data segment.
- Initializing a static variable with zero explicitly still places it in the BSS segment.
- Multiple declarations of the same static variable name without re-initialization are allowed.
- Redefining a static variable with an initializer more than once causes a compiler error.
For a comprehensive understanding, review Understanding Static Modifier in C with Practical Code Example to see practical implications of static variables and their storage.
Compilation and Size Analysis
- Using the
gcccompiler and thesizecommand, you can observe segment sizes. - Adding an uninitialized static variable increases BSS segment size.
- Initializing the static variable with a non-zero value increases the data segment size.
- Initializing with zero keeps the variable in the BSS segment.
Exploring Key Features of C Programming and Basic Code Execution Guide can provide additional context on the compilation process and memory layout.
Scope and Variable Selection
- Local static variables within functions overshadow global variables of the same name.
- The program prints the value of the local static variable when both local and global have the same variable name.
Understanding Essential C Programming Variable Naming Rules and Best Practices can be beneficial when managing variables with overlapping names and scope.
Best Practices and Important Notes
- Avoid multiple initializations of static variables with the same name to prevent redefinition errors.
- Assignments outside functions (i.e., global scope) are not allowed and lead to compiler errors.
- Understanding the storage behavior helps optimize memory usage and debugging.
Summary
This discussion clarifies the often confusing aspects of static variable storage, initialization, and scope in C programming, enhanced by practical compilation examples and memory size inspections. Familiarity with these concepts improves efficient coding and debugging of C applications.
For deeper insights into variable behavior and output formatting, consider reading Understanding Advanced printf Usage and Integer Behaviors in C Programming and Understanding Variables in C Programming: Declaration, Initialization, and Usage.
let's consider some more questions in C programming what is the output of the following C program fragment this is the
C program fragment available over here now you have to produce the output of this particular program here you can see
I assigned a value to this variable now what kind of value is this this is treated as an hexadecimal value when we
place 0 X in front of any value then that value is treated as an hexadecimal value like if you place simply 0 in
front of any value it will be treated as an octal as we had already learned in our previous lesson in the similar way
if we place 0 X in front of any value then that value is treated as an hexadecimal value ok now the format
specifier for hexadecimal is percentile X and if we try to print this out this is going to be the answer for 3f f okay
now one thing that you need to note here is that you can either write 0 small X or 0 capital X both are one and the same
thing you can write small X or capital X over here there is no difference between these two things you can use any of them
ok you can also write small F or capital F also there is no difference in that case but yes one thing that you need to
note here is that if we change this small X to capital X over here as a format specifier then the output printed
would be 4/3 capital F capital F please note down this thing previously the output is 4/3 small F small F in this
case the output is 4/3 capital F capital F so please be careful about such things if you're using percentile capital X
then the output would be capital F capital F and if you're using small X then the output would be small F small F
here in this case now as usual if we change the format specifier to percentile D then we need to convert
hexadecimal to decimal as we know this thing already we learned this thing in our previous lesson already if you want
to convert this hexadecimal number to the decimal number then we have to apply the same procedure that we did for the
octal number if you remember in our previous lecture we already learned this thing that how to convert an octal
number to a decimal number in the similar way you have to convert and hexadecimal number to a decimal
number and here is the output for this particular program one seven four zero seven this is the corresponding decimal
value for this hexadecimal value you can say let's consider the next question what is the output of the following C
program fragment let me tell you one thing this is very important question okay so please concentrate while I am
giving you the answer for this question first of all you should check what is the output of this particular program is
it 27 is it zero is it no output or is it none of the above in order to understand this question you need to
understand the memory layout of C program this is the memory layout of C program you might think of this thing
that might be when the C program is compiled successfully that it will get stored into one memory section but in
reality different different parts of the program will get stored in different different sections of the memory let's
try to understand what these all sections mean basically there are two memory segments you have the one is text
segment or a code segment this is the one it is called text segment or a code segment where the actual machine code of
the compiled program is stored okay and this section is read-only section let me tell you one thing the another segment
of the memory would be data segment there are several parts of the data segment initialized a segment
uninitialized a segment stack segment and the last one is heap segment here you can see this is called initialized
data segment and it would consist of global variables externals static variables both local and global and
constant global variables as well this initialized data segment would consist of all these different kinds of
variables you can say that means when you define a static variable that will get stored into this particular segment
only now one more thing this section would consist of two parts basically read only and read right there is a
reason behind that initialize data segment would not only consist of global variables X term variables and static
variables but also consists of constant global variables let me tell you one thing constant global variables we
already know this thing when we define a constant global variable its value we won't be able to
change in our code at any cost that means it has to be in the read-only section right and the other variables
must be in the readwrite section so there is no problem in understanding this point right therefore we have two
sections for this initialized data segment one is read-only and the other one is readwrite now what is the purpose
of this uninitialized data segment which is also known as VSS segment or block started by symbol segment ok
uninitialized data segment would consist of uninitialized to global static and constant global variables the variable
if it is uninitialized will get stored into this segment otherwise the variable which is initialized will get stored
into this segment variables such as global X turn static and constant global variables get stored inside this
initialized data segment if they are initialized that means if these variables have been provided with some
initial value they are getting stored inside this data segment on the other hand if suppose the variables are not
initialized then they are getting stored inside this uninitialized data segment also called as BSS segment know-down
this full form also block started by symbol important apart from this thing we have stack segment and heap segment
as well but we are going to have a lot of discussion later on in the chapters when we're going to talk about pointers
and recursion right now our concern is these two segments only because we are talking about static in particular and
we know this thing if static variable we declared and it is uninitialized then it will get stored inside this
uninitialized data segment on the other hand if the static variable is initialized with some value then it gets
stored inside this data segment now let's try to consider code of this particular question and please try to
understand this code very carefully here you can see in this program we just have a main function along with that a return
zero statement my purpose of writing this code is to just make you understand what would be the size of the program ok
and we are going to check this out with the help of command prompt we have to shift our attention to command prompt to
understand what would be the size of the let's move to the command prompt here you can see this is the command prompt
window this program kosha answer said 3 dot c is stored inside this directory c programs now what I'm going to do is I'm
going to compile this code over here by writing a command in the command prompt please carefully observe the command if
you are using a GCC compiler then this is the command you need to write GCC then space then quotient answer set 3
dot C which is my file name then space then - oh then space then quotient answer set 3 dot e ex e that is your
executable this is your source code and this is your executable ok you simply hit enter this simply means that your
source code is now compiled successfully if it produce any error it will produce over here but there is no error at all
therefore no problem now if you want to use this command successfully compiler automatically comes with code blocks if
you want to use that compiler which is available from the code blocks itself then what you need to do is you need to
set the environment variable before using this command click on windows C Drive here you can see your Program
Files folder the one is Program Files x86 click on it you find out the file code blocks click on it click on wing W
then simply click on bin copy this path over here and click on this PC again right click click on properties choose
advanced system settings click on environment variables now what you need to do is search over here you will find
out a path variable which is one of your system variables what do you need to do is click on this and click on edit here
you need to add this particular path here you can see here I have already added this that a C Drive Program Files
x86 code blocks Ming W bin and then click on OK everything is applied successfully now you can use this
command over here now you want to know the size of your program then what you need to do is simply type size then
space then question answer set 3 dot XC which is my file name and this is the executable then I simply hit enter this
would be the size in the text segment you will find out the size is four three six zero bytes and in data segment we'll
find out nine four zero which is actually your initialized data segment in the BSS segment you will find out 96
bytes and these three things are visible to you total size would be 5 3 9 6 bytes and this will be your file name which is
quotient answer set 3 dot e ex e now let's change the code a little bit and let's check out the size once again here
I declare a static variable outside in global scope and let's see what would be the size of the file we need to
recompile this code once again by using the same command that I have written over here if you don't want to type this
command again and again then you can click on the up arrow available in your keyboard and you will find out this
command over here you simply click enter this compiled successfully now we need to know the size of this file spawn
answer set 3 dot e x e I don't need to write this name again what we need to do is to use an up arrow in the keyboard or
down arrow click on enter you'll find out the size is quite changed right here in this case the BSS segment size
previously was 96 bytes now it is hundred bytes why there is a 4 bytes increment in the BSS segment because
this static integer I variable is uninitialized and this variable we know this thing is going to get stored into
the BSS segment as we had already discussed right there for this variable is getting stored inside this BSS
segment and that is why the size is increased data segment is as it is text segment is as it is but the size of BSS
is increased as you can see ok let me change the code a little bit over here now I'm going to explain you a very
important point try to understand it very carefully what I'm going to do is I'm going to initialize this variable
with a value is equals to 0 this is the initialization step we already know what is the meaning of initialization when we
provide value at the time of declaration itself then that is called initialization right therefore I
initialize this variable with value is equals to zero as we know this think if we initialize the variable then this
will get stored into the data segment right and that is what we are expecting so data segment size needs to be
increased right let's see what would be the size here again we need to recompile click on down arrow you find out the
size question answer set tree dot exe hit enter now this is something that we need to know data segment sizes as it is
if I initialize this variable then this variable need to be stored into the data segment right and data segment size
needs to be increased and BSS segment size needs to be reduced but as you can see BSS segment size is as it is that
means this variable is stored inside the BSS segment even though we initialize it with value now this is an important
point to note when we initialize this variable we are thinking about this thing that this will get stored into the
data segment right but it is actually getting stored inside the BSS segment why is it getting stored inside the BSS
segment now one thing that I would like to put here is that when we won't initialize any global variable we
already know this thing that it is by default initialized with value is equals to 0 if I simply write static int I and
I won't initialize it with any value then that will get stored inside the BSS segment right apart from that it is by
default initialized with value is equals to 0 now what is the change we are doing here here also we are initializing it
with value is equals to 0 so what is going on internally is that this variable is also getting stored inside
this BSS segment even though we initialize this value here we are not initializing any different value we are
initializing it with value is equals to 0 no difference therefore this variable is getting
stored inside the BSS segment only and that is one of the most important point actually this variable need to be stored
inside the data segment because we initialized this variable with some value but it is getting stored inside
this VSS segment that is the very important point we need to note ok suppose we initialize with some
different value other than 0 and save this code let we compile it now the data segment size
is increased while the BSS segment size is reduced that means this variable now shifted to data segment because now we
initialize it with some different value other than 0 therefore this particular variable get stored inside the data
segment and not inside the BSS segment ok let's move to our question of concern according to our question these are the
first two lines right first line here is a static int I and the next line is static int I and I initialized it with
value is equal to 27 let's try to understand this code here I am compiling this once again
and this would be the size because of this first line static int I first it will get stored into the BSS segment
right then after that when I declare another variable with the same name when I initialize it with some value other
than 0 this variable because of the same name this variable is transferred from BSS segment to data segment and the data
segment size is getting increased and the BSS segment size decreased this would be the third line right okay
let's save the code and see what would be the changes no changes at all if you carefully observe this thing that data
segment will be same and BSS segment would be same this simply means that multiple times you can declare there is
no problem at all you can write this statement multiple number of times even before or afterwards once you initialize
it will get stored into the data segment and you won't be able to initialize it further anymore suppose you write
another value like 24 then this thing is something which is not allowed if you try to execute this code it will produce
a compiler error let's see how it is producing a compiler error here you can see it is treated as a redefinition of I
static int I is equals to 24 previous definition of I was here static int I is equals to 27 right this is called as
redefinition of a variable I because you initialize this variable previously therefore you won't be able to
initialize it further in the global scope with the same name please note down this thing on the other hand simply
writing this thing is allowed totally allowed even though if we write something like this I is equals to 0 and
if we save the code and try to see what would be the output in this case let's recompile the code then this is also
treated as redefinition because this value is initialized by us and not by the BSS therefore it is considered to be
as a redefinition even though this variable is stored inside the BSS segment and we initialize it value is
equals to 0 not the BSS therefore if we try to initialize it once again then that will be treated as
a redefinition of I please don't do such things let's change this once again according to over question inside this
main function there is another variable right according to the question which is static int I right and then after that
you have a printf function now we know this thing that scope rule comes in this case therefore this eye is preferred
over to this eye this eye or this I when we initialize a variable then that variable is the only variable stored
into the data segment and the other variables are simply ignored you can say with the same name right this variable
is stored inside the data segment even though if you write static int I you might think like that way after this
this will get updated to value is equals to 0 but this is not the case therefore this variable is only
available with us which is the initialized one multiple number of times you write this statement there is no
difference this value is not getting updated suppose this is not available with us then you know this thing that
this I would be taken from this variable itself if you try to execute this code then this would be our output right 27
because this is the variable which is inside the data segment and this is the variable which is available only if we
won't have this statement over here like static int I then the output would be 27 only and we can tick mark the a option
if you remember that let me remove this comment now let's see what would be the output output is 0 why this variable is
within this function only right as we know this thing that this variable is getting stored inside the BSS segment if
you want to know this thing let me execute it within the command line let me recompile this code you can see over
here previously it was 96 now it is updated 200 right that means this variable is getting stored inside the
BSS segment and we know this thing that this variable get preference over to this variable which is initialized 1
therefore the value printed over here is equals to 0 and not 27 local variable get preference over to the global
variable we already know the scope rules therefore the value printed is equals to 0 and not 27 please note
down this thing okay let me change the code a little bit for fun sake suppose I won't do any initialization I simply do
the assignment of the I variable let's see if this is allowed or not this is redefinition of I why note down these
warnings over here type defaults to int in Declaration of I because compiler automatically put integer datatype in
front of a variable assignment is not allowed at the global declaration suppose we comment this particular
statement and this statement as well instead of assignment done over here we do assignment over here like I is equals
to 45 let's execute the code it runs perfectly fine assignment is allowed within the function block only and not
outside the function or any other function okay in such global declarations these assignments are not
allowed compiler automatically puts integer in front of this variable therefore this variable is treated as an
integer variable and the whole thing is considered to be as a redefinition you already define this variable over here
and you're trying to define another variable with the same name now this is considered to be as a redefinition now
you understand this thing that why these variables are by default initialized with value is equals to 0 because they
are stored inside the BSS segment and inside the BSS segment by default these variables are initialized with 0 value
not only the static variables all the global variable static variables and constant global variables are
initialized with value is equals to 0 except the external variable right static int I when we simply write it
down it is getting stored inside the BSS segment and by default this variable is initialized with value is equals to 0 if
we won't initialize it ok apart from this such type of assignments are not allowed and this is very very
important when we write static int I is equals to 0 then that variable gets stored inside
the BSS segment only and if we try to initialize it once again then we won't be able to do that because that
initialization of 0 is done by us and not inside the BSS egman by default therefore it is
considered to be as a redefinition if you've changed this code according to our question then let's see what would
be the output output is zero therefore we can say option B is correct right okay friends this is it for now see you
in the next lecture bye bye [Applause]
[Music]
In C, prefixing a number with '0x' indicates that the number is in hexadecimal (base 16) format. For example, '0x3FF' represents a hexadecimal value that can be printed using format specifiers like '%x' or '%X' in printf for lowercase and uppercase letters respectively.
Static variables in C are stored differently based on their initialization: uninitialized static variables are placed in the BSS segment and automatically initialized to zero, while static variables initialized with non-zero values reside in the data segment. If a static variable is explicitly initialized to zero, it still resides in the BSS segment.
The BSS segment holds uninitialized global and static variables which the system initializes to zero at runtime, whereas the data segment contains initialized global, static, and constant variables. The data segment is divided into read-only areas for constants and read-write areas for initialized variables with non-zero values.
Redefining a static variable with an initializer more than once results in a compiler error because it violates the One Definition Rule. Static variables should only be initialized once; multiple initializations can cause conflicts in memory allocation and linkage, leading to compilation failure.
Local static variables within functions have their own scope and overshadow global variables of the same name. When both exist, the program accesses and modifies the local static variable, leaving the global variable unaffected. This encapsulation helps manage variable states specific to functions.
Using the 'gcc' compiler along with the 'size' command allows you to observe the changes in memory segment sizes. For example, adding an uninitialized static variable increases the BSS segment size, whereas initializing it with a non-zero value increases the data segment size. This helps understand memory optimization in your program.
Avoid multiple initializations of the same static variable name to prevent redefinition errors, and never place assignments for static variables at global scope outside functions. Understanding their storage behavior aids in efficient memory usage and debugging, so carefully plan variable scope and initialization.
Heads up!
This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.
Generate a summary for freeRelated Summaries
Understanding Static Modifier in C with Practical Code Example
This detailed guide explains the static modifier in C programming through a step-by-step project example. Learn the difference between automatic, global, and static variables, how static variables retain values within functions, and why static helps control variable scope within files for safer code.
Understanding Variables in C Programming: Declaration, Initialization, and Usage
This lesson introduces the concept of variables in C programming, explaining their declaration, definition, initialization, and how to use them effectively. Learn how variables act as memory storage with simple examples and best practices to manage variable values within your programs.
Understanding Advanced printf Usage and Integer Behaviors in C Programming
This comprehensive summary explores key concepts in C programming, including nested printf functions, string width specifiers, character variable overflow, integer declarations, and nuances of signed versus unsigned integer arithmetic. Learn how printf returns values, how formatting affects output, and how integer operations behave in different contexts.
Understanding Variable Scope: Local vs Global Variables in Programming
Explore the concept of variable scope in programming, focusing on the differences between local and global variables. Learn how variable lifetime and visibility affect program behaviour with clear examples and key principles.
Essential C Programming Variable Naming Rules and Best Practices
This summary highlights the crucial rules and conventions for naming variables in C programming, emphasizing why proper naming is vital for effective coding. It covers valid characters, case sensitivity, forbidden keywords, and practical tips to avoid common mistakes and improve code readability.
Most Viewed Summaries
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakarang kolonyal ng mga Espanyol sa Pilipinas at ang mga epekto nito sa mga Pilipino.

