Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What's the difference between const unsigned int & char or unsigned int & char?

Status
Not open for further replies.

Help

Advanced Member level 2
Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
What's the difference between const unsigned int & char or unsigned int & char?

hi...

Anyone help me????
What the difference between them....

1a) const unsigned int
1b) const unsigned char

2a) unsigned int
2b) unsigned char

3a)
----------------------------------------------------------
unsigned int x, y;

for(x=0; x<=3; x++)
{
for(y=0; y<=120; y++);
}
----------------------------------------------------------
3b)
----------------------------------------------------------
unsigned int x, y;

for(x=0; x<=3; x++)
{
}
----------------------------------------------------------



Thanks.....
 

C code

const types are data types which are not changing during the operetion of the program.They must be initialized when they declared.They thought as global variables.

without "const" declaration, variables may be global or local depending on the type of the declaration.If they are global, exists during the operation of the program.these types of variables doen't need to be initialized at first...

for more information see K&R C programming book
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code

Hi!

You should be able to find an answer in any C book for beginner.
If a book doesn't answer these simple questions, well, maybe you'll want
to try a different hobby.

Luciano
 

Re: C code

if you define a constant you must define it's value
Code:
 const unsigned char a= 0x0c
or it will be not useful
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code

mucko said:
const types are data types which are not changing during the operetion of the program.They must be initialized when they declared.They thought as global variables.

without "const" declaration, variables may be global or local depending on the type of the declaration.If they are global, exists during the operation of the program.these types of variables doen't need to be initialized at first...

for more information see K&R C programming book

Hi........

Do you have the K&R C programming book?? Can you send it to my mail (tangbc05@yahoo.com)....

Thank You.....
 

C code

Here is "The C Programming Language", second edition, by Kernighan and Ritchie:


Your question smells like homework.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code

peace upon you
------------------
1a,b)was answered well in the previous massages

2) unsigned mean the variable takes values from zero to the max of tis location in memory, say u define as char u mean that u allocate only one byte(8 bit) in memory for this variable so it 'll take values from 0`255,and int it take two byte (16 bits) so it takes 0`(2 to the power 16(65536))


3) this usigned int x mean allocate place in memory from 2 byte to variable name x,and the same for y
then they make loop for x=0 count y from 0~120 then increment x then count again y from 0~120 and so on until x greater than 3 this routen used to make delay

4)the same as point 3 but no count here for y it just count x from 0~3

pleaze take me\y advise and begin to read in any book for c then try to look again for ur question....................
 

Re: C code

dear friend there is another way to make a constant a variable !!
if you define the constant as a pointer you will fix the location not the data
Code:
const unsigned char *a;
scanf("%c", a);
so you can change it's value at in moment in the program also by direct assignment
Code:
*a = 0x0c
this means assign 0x0c as the data of the memory location fixed by a
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C code

ahm_hassaan said:
peace upon you
------------------
1a,b)was answered well in the previous massages

2) unsigned mean the variable takes values from zero to the max of tis location in memory, say u define as char u mean that u allocate only one byte(8 bit) in memory for this variable so it 'll take values from 0`255,and int it take two byte (16 bits) so it takes 0`(2 to the power 16(65536))


3) this usigned int x mean allocate place in memory from 2 byte to variable name x,and the same for y
then they make loop for x=0 count y from 0~120 then increment x then count again y from 0~120 and so on until x greater than 3 this routen used to make delay

4)the same as point 3 but no count here for y it just count x from 0~3

pleaze take me\y advise and begin to read in any book for c then try to look again for ur question....................


Hi....

Anyone can help me in question 3a) and 3b), i know the idea how it work but i not understand how it work in 8051 Embedded C because i was using the uVision2 software to debug (Watch and Call Stack Window), it just looping 3 time for x but the y just the Hex number 0x0079.....

Anyone can help me .............. THANKS....

Thank You.
 

Re: C code

peace upon you

i just tried it now and it work as i told you before

for x=0;
it count for y from 0==> 120
then icrement x to be 1
then count y from 0==> 120 and so on,

i use Uvision2 and it work well.

.......
i guess you make this error when you debug using "STEP OVER" not "STEP INTO"
s
 

C code

the variable const you must initialise them
like
const unsigned char x ='a';
this can not been change in the program
 

Re: C code

ahm_hassaan said:
peace upon you

i just tried it now and it work as i told you before

for x=0;
it count for y from 0==> 120
then icrement x to be 1
then count y from 0==> 120 and so on,

i use Uvision2 and it work well.

.......
i guess you make this error when you debug using "STEP OVER" not "STEP INTO"
s

Hai...

I use STEP OVER also, the result is like this:

----DELAY_MS 0x0003
|
----x 0x0003
|
----y 0x0079

after the x count from 0 to 3 then the last x=4 then end of the function. It should be, count x=1 time then y count from 0-120 time, then x=2nd time then y again count from 0-120 time, then x=3th time and so on..... Is it like that?? So the total loop is 360 cycle is it??

Thanks
 

Re: C code

Hi
--
if you press "STEP OVER" the command
for(y=0; y<=120; y++);
it'll be done in single click but it actualy take say 121 cycle
so if you press "STEP OVER" ,flow 'll be
1st click x=0 and y=120 (but it takes 121 cycle)
2nd click x=1 and y also =120 (but it take another 121 cycle ).......

if u use "STEP INTO" it will increment y each click so you need 121 click to finish this command
for(y=0; y<=120; y++);
but it also take 121 cycle for each x incrment
 

Re: C code

ahm_hassaan said:
Hi
--
if you press "STEP OVER" the command
for(y=0; y<=120; y++);
it'll be done in single click but it actualy take say 121 cycle
so if you press "STEP OVER" ,flow 'll be
1st click x=0 and y=120 (but it takes 121 cycle)
2nd click x=1 and y also =120 (but it take another 121 cycle ).......

if u use "STEP INTO" it will increment y each click so you need 121 click to finish this command
for(y=0; y<=120; y++);
but it also take 121 cycle for each x incrment

Hi..

The result still the same even i use "STEP INTO", just count the x loop only... :? !! Why...?? After creak 3 time then jump out the loop already..................

Thanks...
 

Re: C code

Help said:
Anyone can help me in question 3a) and 3b), i know the idea how it work but i not understand how it work in 8051 Embedded C because i was using the uVision2 software to debug (Watch and Call Stack Window), it just looping 3 time for x but the y just the Hex number 0x0079.....

for(y=0; y<=120; y++);

it last increment y = 121(dec) or 0x0079(hex)
 

Re: C code

suromenggolo said:
Help said:
Anyone can help me in question 3a) and 3b), i know the idea how it work but i not understand how it work in 8051 Embedded C because i was using the uVision2 software to debug (Watch and Call Stack Window), it just looping 3 time for x but the y just the Hex number 0x0079.....

for(y=0; y<=120; y++);

it last increment y = 121(dec) or 0x0079(hex)

Hi,

Can i know how the For-Loop for y work because i not understand why the y alway at 121 or 0x0079, it should be slowly increment from 0 to 121 as ahm_hassaan say like "use "STEP INTO" it will increment y each click so you need 121 click to finish this command" but why the simulation just display 0x0079, actually the simulation must count from 0x0000->0x0001->0x0002->0x0003->..........0x0079 is it....!!

Thanks...
 

Re: C code

Code:
unsigned int x, y;

for(x=0; x<=3; x++)
{
  for(y=0; y<=120; y++);
}
A good optimizing compiler will detect the unnecessary loops and simplify the code to this:

Code:
unsigned x=4, y=121;
Furthermore, if you don't use x and y anywhere else in your program, then a good compiler will optimize it even further:

Code:
Yes, nothing at all!

I'm guessing that your compiler optimized away some of the code, but not all of it.
 

Re: C code

echo47 said:
Code:
unsigned int x, y;

for(x=0; x<=3; x++)
{
  for(y=0; y<=120; y++);
}
A good optimizing compiler will detect the unnecessary loops and simplify the code to this:

Code:
unsigned x=4, y=121;
Furthermore, if you don't use x and y anywhere else in your program, then a good compiler will optimize it even further:

Code:
Yes, nothing at all!

Hi..

So, you mean that is the compiler is not show us the for loop for y because of compiler already simplify the code then skip the process izit but why ahm_hassaan say can.....??

I am not understand "if you don't use x and y anywhere else in your program, then a good compiler will optimize it even further:" and why you remove the int can you explain more detail abit??

Thank You..
 

C code

Yes, a smart optimizing compiler won't bother generating any machine code that increments the variables, because it already knows their final values.

If you don't use x and y later in your program, then a smart compiler will remove all the machine code and memory storage for those variables, because they do no useful work.

I don't know how smart your 8051 compiler is. Maybe it optimizes some things but not others. That would explain the unexpected behavior you see in the debugger.

Try doing something useful with x and y, so the compiler can't just throw them away.

unsigned int and unsigned mean the same thing. I changed it by habit, without realizing that I had done it!

ahm_hassaan is describing how a debugger usually works. However, you should prepare yourself for some surprises when running a debugger. That's the life of a C programmer!

Maybe you can temporarily disable your compiler's optimizer. That will probably cause your program to run slower, but it should make more sense in the debugger.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top