844 views
5 5 votes

What is the output of the following program: (Assume that the appropriate preprocessor directives are included and there is not syntax error)
 

main( )
    { char S[]="ABCDEFGH";
        printf("%C", *(&S[3]));
        printf("%s", S+4);
        printf("%u", S);
    /*Base address of S is 1000 */
    }

  1. $ABCDEFGH1000$ 
     
  2. $CDEFGH1000$ 
     
  3. $DDEFGHH1000$ 
     
  4. $DEFGH1000$

6 Answers

2 2 votes

Ans : D maybe😁


First Printf statement is Give Value at S[3] mean D

Second Give base address +4*(value of each string like for int ,float = 4 ) Give E to whole string EFGH

Third and last is base address of s mean 1000 and if you not solve 3rd statement also okay because in all option same value

 

and correct me if i am wrong

2 2 votes
We can imagine it as given below, since it will be stored as a String only; S will be constant pointer to first element of the String;

A         B        C        D       E        F        G        H        \0

1000   1001   1002  1003  1004  1005   1006  1007   1008

(S+0)  (S+1)  (S+2) (S+3) (S+4) (S+5)   (S+6) (S+7)  (S+8)

Now we can say S[3] = *(S + 3) so we will evaluate it as       *(&*(1000 + 3))

                                                                                                *(&*(1003))

                                                                                                *(& D)  because *(1003) will be D

                                                                                                *(1003) which will be D

You can see the above string, S+4 is a pointer to E and since format specifier is %s the entire string EFGH will be printed so output till now is DEFGH

S is a constant pointer to first element of the string so it is 1000 only and we have to treat it as unsigned int so no problem at all
0 0 votes
printf ("\%C", *(\&S[3])); will print character at *(\&S[3]) i.e. D.
printf ("\%s", S + 4); will print string starting from S + 4 i.e. EFGH.
printf ("\%u", S); will print address of S i.e. 1000.
Since there is no new line instruction, So DEFGH1000 will be the output.
So, option (D) is correct.
• reshown by
0 0 votes

S is a character array and since it is initialised using double quotes the final character is the null character \0. Character array S holds 9 characters in total.

  • S is a constant pointer to the base address i.e 1000 (given)
  • S+4 points to the address of the 5th element in character array S. Since we used %s as format specifier in printf , printf would print all the characters from this address until it encounters null character.
  • *(&S[3]) dereferences the value at address of S[3] i.e 4th element of S. 
Using above information, answer should be option D.

 

0 0 votes

 


 Given Code:

main( )
{
    char S[] = "ABCDEFGH";
    printf("%C", *(&S[3]));
    printf("%s", S+4);
    printf("%u", S);  // Assume S base address is 1000
}

 STEP 1: Understand the array

char S[] = "ABCDEFGH";

Means:

Index:   0 1 2 3 4 5 6 7 8
Value:   A B C D E F G H \0   ← '\0' is automatically added
Address:1000 1001 1002 ... (assumed)

So:

  • S[0] = 'A'

  • S[3] = 'D'

  • S[4] = 'E'

 STEP 2: First printf → printf("%C", *(&S[3]));

  • S[3] = 'D'

  • &S[3] = address of 'D'

  • *(&S[3]) = value at address of 'D' = 'D'

 Output: D

 STEP 3: Second printf → printf("%s", S+4);

  • S+4 = address pointing to S[4] = 'E'

  • "%s" prints characters from this address until '\0'

So:

S+4 points to "EFGH"

Output: EFGH

 STEP 4: Third printf → printf("%u", S);

  • S is base address = 1000 (given)

  • %u prints the unsigned integer (address)

 Output: 1000


 FINAL OUTPUT

DEFGH1000
  1. Know how pointers & arrays work. S+4 means skip first 4 characters.

  2. Know *(&S[3]) just gives value of S[3].

  3. %s prints till null. %u prints address

 Summary Output:

D E F G H 1 0 0 0

 

Answer:
Position:
Show:

Related questions

6 6 votes
4 4 answers
750
750 views
GO Classes asked Jul 19, 2025
750 views
int arr[ ]={1, 2, 3, 4} int count; incr( ) {return ++count;} main( ) { arr[count++]=incr( ); printf("arr[count]=%d\n", arr[count]); }The value printed by the above progra...
2 2 votes
2 2 answers
505
505 views
GO Classes asked Jul 19, 2025
505 views
What is the value returned by the function $f$ given below when $n=100$ ?let f(int n) { if (n==0) then return n; else return n+f(n-2); }$2550$ $2556$ $5220$ $5520$
3 3 votes
2 2 answers
574
574 views
GO Classes asked Jul 19, 2025
574 views
Consider the following structure on a $32$ -bit machine ( $4$ -byte alignment for int, char alignment is $1$ byte):struct test { char a; int b; char c; };What is the size...
5 5 votes
1 1 answer
449
449 views
GO Classes asked Jul 19, 2025
449 views
Consider the following C code:int arr[] = {10, 20, 30, 40, 50}; int *p = arr + 2; printf("%d", *(p - 1));What is the output of this code? $10$  $20$  $30$  $40$