Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:56:26+00:00 2026-06-02T15:56:26+00:00

struct A { int a:2; int b:3; int c:3; }; int main() { struct

  • 0
struct A
{
 int a:2;
 int b:3;
 int c:3;
};

int main()
{
 struct A p = {2,6,1};
 printf("\n%d\n%d\n%d\n",p.a,p.b,p.c);
 return 0;
}    

Output is:
-2,-2,1

What would be output of above code in C complier and in C++ complier?
And Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:56:29+00:00Added an answer on June 2, 2026 at 3:56 pm

    Now Lets see what exactly is happening. Lets start with the given code:

    struct A
    {
     int a:3;
    };
    int main()
    {
     struct A p = {5};
     printf("%d",p.a);
    }
    

    within 3 bits the values would be 101(5) since sign bit of this 3 bit set is 1 thus negative value. Thus we need to find 2’s compliment of 101 which would be 011(3).
    Thus by applying above logic we would output as -3. Similarly others could be proved.

    e.g. for 1001(9) we shall take 3 bit values because of a:3. thus it would be 001(1). Since here sign bit is not set i.e. 1 so no need to use 2’s complement. Straight forward answer would be 1.
    Similarly others could be done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my test code is below: main1.c: #include <stdio.h> extern struct tt ; int main()
Short story: struct A{}; struct B:private A{}; void f(void *){} void f(A*){} int main(){
I am curious why this code works: typedef struct test_struct { int id; }
see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned
#include <stdio.h> typedef struct { short x,y; char type; } Tile; int main(int argc,
int main() { struct HXmap *limitMap; limitMap = HXmap_init(HXMAPT_DEFAULT,HXMAP_CKEY); int a[]={1,2,3}; HXmap_add(limitMap,a,limitMap); HXmap_add(limitMap,(a+1),BuyTree); HXmap_add(limitMap,&a[2],main);
The given C code #include <stdio.h> int x = 14; size_t check() { struct
I have the following code: struct B { //B() {} int x; int y;
I have the following code: #include <iostream> using namespace std; struct S { S(int
I tried the following program struct temp{ int ab; int cd; }; int main(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.