to get the correct value with unsigned int


 
Thread Tools Search this Thread
Top Forums Programming to get the correct value with unsigned int
# 1  
Old 04-11-2007
to get the correct value with unsigned int

hi,

Please help me with the following code to get the difference in values.
Code:
struct a{
		int b1;
		int c1;
		char d1;
	}

main()
{
	unsigned int b=10;
	unsigned int c;
	c = b - (unsigned int )sizeof(a);
  	printf("%d",c);
}

Here c returns some junk value. How can i get the difference between b and sizeof (a) in a positive value without using abs.

Thanks

Last edited by blowtorch; 04-11-2007 at 07:19 AM.. Reason: add code tags
# 2  
Old 04-11-2007
I modified your program slightly to get it to work:
Code:
#include<stdio.h>

struct a {
        int b1;
        int c1;
        char d1;
};

int main() {
        unsigned int b=10;
        unsigned int c;
        c = b - (unsigned int )sizeof(struct a);
        fprintf(stdout,"size of a: %d, size of int: %d, size of char: %d\n",sizeof(struct a),sizeof(int),sizeof(char));
        fprintf(stdout,"%d\n",c);
        return(0);
}

This is the output:
Code:
# ./a.out
size of a: 12, size of int: 4, size of char: 1
-2

Now I agree that for two ints and one char, the size should be 4+4+1=9, but memory allocation is not done a single byte at a time. This is system dependent.
# 3  
Old 04-11-2007
struct size

Hi ,
the solution given above is right but i want to correct at one point.
c = b - (unsigned int )sizeof(struct a);
printf("%d\n",c); will print -2
because any struct occupy the memory in chunk of words. and for linux word is 4 bytes. so for char it occupies 4 bytes give one byte to char and pad the rest 3 bytes so sizeof(struct a) gives 12 as size
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Unsigned to signed, error?...

Hi guys... Macbook Pro, 13", circa August 2012, OSX 10.7.5, default bash terminal. I require the capability to convert +32767 to -32768 into signed hex words... The example piece code below works perfectly except... #/bin/bash # sign.sh # Unsign to sign... while true do # I have used... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Programming

Unable to assign zero to unsigned character array

Hi, I am unable to assign value zero to my variable which is defined as unsigned char. typedef struct ABCD { unsigned char abc; unsigned char def; unsigned char ghi; } ABCD; typedef ABCD *PABCD; In my Por*C code, i assign the values using memcpy like below ... (3 Replies)
Discussion started by: gthangav
3 Replies

3. Programming

Signed and unsigned intergers

when a date type is considered signed and unsigned is that simple referring to - for signed and positive numbers for unsigned? Further if that is the case would mutiplying and dividing ect where 2 signed numbers, like (-2)*(-2) = 4 result in a unsigned. (3 Replies)
Discussion started by: Fingerz
3 Replies

4. Programming

Comparing unsigned char bits.

/******************************************************************************/ /* Printing an unsigned character in bits */ #include <stdio.h> void display_bits ( unsigned char ); int main() { unsigned char x; /*... (15 Replies)
Discussion started by: robin_simple
15 Replies

5. Programming

Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables?? When I declare a variable "unsigned;" what did I do it??? Why would I delcare an integer "long" or "short" ( unsigned or signed)?? Any examples of when things like "unsigned", "long", "short" etc...... (6 Replies)
Discussion started by: cpp_beginner
6 Replies

6. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

7. Red Hat

cast from const void* to unsigned int loses precision

Hello everey one, here i am attempting to compile a c++ project .it's throughing the following errors. my machine details are as follows: Linux chmclozr0119 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux errors: ===== Generating... (0 Replies)
Discussion started by: mannam srinivas
0 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

10. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies
Login or Register to Ask a Question

Featured Tech Videos