Question

PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

PLease explain output of these two programs:

1.

#include <stdio.h>

typedef struct {
char *name;
int x, y;
int h, w;
} box;

typedef struct {
unsigned int baud : 5;
unsigned int div2 : 1;
unsigned int use_external_clock : 1;
} flags;

int main(int argc, char** argv){

printf("The size of box is %d bytes\n", sizeof(box));
printf("The size of flags is %d bytes\n", sizeof(flags));
return 0;
}

2.

#include <stdio.h>
#include <string.h>

/* define simple structure */
struct {
unsigned int widthValidated;
unsigned int heightValidated;
} status1;

/* define a structure with bit fields */
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;

int main( ) {

printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Note: in both the C programms while printing the size in main function you wrote data specified as integer but the size of structure will return the unsigned long so we need to change it %lu to get the output or else it will through output.

Here is the Modification of codes with explanation as comments and output

1.

//including header for standard input and output

#include <stdio.h>

typedef struct {

//data members in structre

char *name;

int x, y;

int h, w;

} box;

typedef struct {

//another structure with bit field assigned values

unsigned int baud : 5;

unsigned int div2 : 1;

unsigned int use_external_clock : 1;

} flags;

//main

int main(int argc, char** argv){

//printing the size of structure called box

printf("The size of box is %lu bytes\n", sizeof(box));

//printing the size of structure called flags

printf("The size of flags is %lu bytes\n", sizeof(flags));

return 0;

}

Editor:

1 2 3 4 5 6 8 10 main.c //including header for standard input and output #include <stdio.h> typedef struct { //data members i

output:

> ./main The size of box is 24 bytes The size of flags is 4 bytes

2.

//including necessary headers

#include <stdio.h>

#include <string.h>

/* define simple structure */

struct {

//data inside the structure

unsigned int widthValidated;

unsigned int heightValidated;

} status1;

/* define a structure with bit fields */

struct {

unsigned int widthValidated : 1;

unsigned int heightValidated : 1;

} status2;

//main

int main( ) {

//print the size of structure without bit fields

printf( "Memory size occupied by status1 : %lu\n", sizeof(status1));

//print the size of struct with bit fields

printf( "Memory size occupied by status2 : %lu\n", sizeof(status2));

return 0;

}

Editor:

1 2 3 4 5 9 10 main.c //including necessary headers #include <stdio.h> #include <string.h> /* define simple structure */ stru

output:

./main Memory size occupied by statusi : 8 Memory size occupied by status2 : 4 >

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.

Add a comment
Know the answer?
Add Answer to:
PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; };...

    Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....

  • Question 1 Given the declarations: #include <stdio.h> struct STRUCTURE { int aa; char bb[20]; } enum...

    Question 1 Given the declarations: #include <stdio.h> struct STRUCTURE { int aa; char bb[20]; } enum Numbers { zero, one, two, three } typedef struct STRUCTURE STR; typedef enum Numbers NB; STR st1, st2; NB value1, value2; check the validity of the following assignments: A) Valid B) Not valid value1 = four; value2 = three; printf (“st1= %d”, st1); st2.aa = two; strcpy(st2.bb,"Quiz4");

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • main.c #include <stdio.h> int main() { Tong Tong int x = 1234567890000; char y = (char)...

    main.c #include <stdio.h> int main() { Tong Tong int x = 1234567890000; char y = (char) x; printf("%d\n", y); Compile and run the above main.c file. a) Indicate the compiler command you would use to compile this program. Do any of the following flags affect the compilation? -Wall -Werror -pedantic -Woverflow b) Indicate any discrepancy between expected and actual behavior, and as a programmer, how to construct best practices to avoid this deviation. c) What happens if we do not...

  • Given the following source programs, write down the letter that correspond to the right answer #include...

    Given the following source programs, write down the letter that correspond to the right answer #include <stdio.h> #ifndef MEMORY #include "myhead.h" #include <stdlib.h> myhead.h #include <string.h> int main() { #endif char *text= MEM(SIZE, char); #define SIZE 10 COPY_TEXT (text, MSG); #define MSG "CS262" newtext (text); #define MEM(size, type) SHOW (text); (type *) malloc(size * sizeof(type)) free (text); #define COPY_TEXT (dest, source) return 0; strcpy (dest, source) } text.c #define SHOW (x) printf("Output: %s\n", x) void newtext (char *text) { int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT