C Aptitude: Endianness, Pointer Arithmetic

C Aptitude 31
In this episode we’ll learn learn more about Endianness, Pointer Arithmetic.

C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.


Predict the output or error(s) for the following:

C aptitude 31.1

  
main() {
int i = 258;
int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}

Answer: 2 1

Explanation: The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are ‘small-endian’ machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010.

C aptitude 31.2

main() {
int i=300;
char *ptr = &i;
*++ptr=2;
printf("%d",i);
}

Answer:556

Explanation:The integer value 300  in binary notation is: 00000001 00101100. It is  stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it  is  00000010 00101100 => 556.

C aptitude 31.3

main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
least = ((*ptr)<(least))?(*ptr):(least);
printf("%d", least);
}

Answer: 0
Explanation: After ‘ptr’ reaches the end of the string the value pointed by ‘str’ is ‘’. So the value of ‘str’ is less than that of ‘least’. So the value of ‘least’ finally is 0.

Read more Similar C Programs 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can Follow @c_program You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link. Or if you can’t find the program you are looking for, you can contact [email protected]. Thank you for visiting.

(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 30
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

–> Predict the output or error(s) for the following:

C aptitude 30.1

  main()
{
int i;
i = abc();
printf("%d",i);
}
abc()
{
_AX = 1000;
}

Answer:1000

Explanation: Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.

C aptitude 30.2

   main( )
{
void *vp;
char ch = ‘g’, *cp = “goofy”;
int j = 20;
vp = &ch;
printf(“%c”, *(char *)vp);
vp = &j;
printf(“%d”,*(int *)vp);
vp = cp;
printf(“%s”,(char *)vp + 3);
}

Answer: g20fy

Explanation: Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is ‘g’. Similarly the output from second printf is ‘20’. The third printf statement type casts it to print the string from the 4th value hence the output is ‘fy’.

C aptitude 30.3

     # include<stdio.h>
aaa() {
printf("hi");
}
bbb(){
printf("hello");
}
ccc(){
printf("bye");
}
main()
{
int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}

Answer: bye

Explanation: ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc.

Read more Similar C Programs 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–> To browse more C Programs visit this link. Or if you can’t find the program you are looking for, you can contact [email protected]. Thank you for visiting.
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 29
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

Predict the output or error(s) for the following:

C aptitude 29.1

  enum colors {BLACK,BLUE,GREEN}
main()
{

printf("%d..%d..%d",BLACK,BLUE,GREEN);

return(1);
}

Answer: 0..1..2

Explanation: enum assigns numbers starting from 0, if not explicitly defined.

C aptitude 29.2

   void main()
{
char far *farther,*farthest;

printf("%d..%d",sizeof(farther),sizeof(farthest));

}


Answer: 4..2

Explanation: The second pointer is of char type and not a far pointer

C aptitude 29.3

     main()
{
int i=400,j=300;
printf("%d..%d");
}

Answer: 400..300

Explanation: printf takes the values of the first two assignments of the program. Any number of printf’s may be given. All of them take only the first two.

Read more Similar C Programs 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 28
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.

Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

–>
Predict the output or error(s) for the following:

C aptitude 28.1

 main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='') ++*p++;
printf("%s %s",p,p1);
}

Answer: ibj!gsjfoet

Explanation: ++*p++ will be parse in the given order
 *p that is value at the location currently pointed by p will be taken
  ++*p the retrieved value will be incremented
 when ; is encountered the location will be incremented that is p++ will be executed

Hence, in the while loop initial value pointed by p is ‘h’, which is changed to ‘i’ by executing ++*p and pointer moves to point, ‘a’ which is similarly changed to ‘b’ and so on. Similarly blank space is converted to ‘!’. Thus, we obtain value in p becomes “ibj!gsjfoet” and since p reaches ‘’ and p1 points to p thus p1doesnot print anything.

C aptitude 28.2

   #include <stdio.h>
#define a 10
main()
{
#define a 50
printf("%d",a);
}

Answer: 50

Explanation:The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.

C aptitude 28.3

     
main()
{
printf("%p",main);
}


Answer:Some address will be printed.
Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers.

Read more Similar C Programs 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can Follow @c_program You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!

–>
To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 27
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.

Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

–>
Predict the output or error(s) for the following:

C aptitude 27.1

 main()
{
int i;
i = abc();
printf("%d",i);
}
abc()
{
_AX = 1000;
}

Answer: 1000

Explanation: Normally the return value from the function is through the information from the accumulator. Here _AX is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.

C aptitude 27.2
// If the inputs are 0,1,2,3 find the o/p

  int i;
main()
{
int t;
for ( t=4;scanf("%d",&i)-t;printf("%dn",i))
printf("%d--",t--);
}

Answer: 4–0 3–1 2–2

Explanation:Let us assume some x= scanf(“%d”,&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0

C aptitude 27.3

     
int a,b;
func(a,b)

{
return( a= (a==b) );
}
main()
{
int process(),func();
printf("The value of process is %d !n ",process(func,3,6));
}
int val1,val2;
process(pf,val1,val2)
int (*pf) ();
{
return((*pf) (val1,val2));
}

Answer:The value if process is 0 !
Explanation: The function ‘process’ has 3 parameters – 1, a pointer to another function2 and 3, integers. When this function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for val1 and 6 for val2. This function returns the result of the operation performed by the function ‘func’. The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b. since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the function ‘process’.

Read more Similar C Programs 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can Follow @c_program You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!

–>
To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 24
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

Predict the output or error(s) for the following:

C aptitude 24.1

  main()
{
printf("%x",-1<<4);
}


Answer: fff0

Explanation: -1 is internally represented as all 1’s. When left shifted four times the least significant 4 bits are filled with 0’s.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

C aptitude 24.2

   main()
{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}

Answer: Compiler Error : Type mismatch in redeclaration of function display

Explanation:In third line, when the function display is encountered, the compiler doesn’t know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

C aptitude 24.3

      main()
{
int c=- -2;
printf("c=%d",c);
}

Answer:c=2

Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.
Note: However you cannot give like –2. Because — operator can only be applied to variables as a decrement operator (eg., i–). 2 is a constant and not a variable.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 24
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about how to prepare and write the C Aptitude Exams?
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

 

Predict the output or error(s) for the following:

C aptitude 24.1

main()

{

char str[4]="test";

printf("%s",str);


}

Answer: test#$634278621@!

Explanation: The character array has the memory just enough to hold the string “test” and doesn’t have enough space to store the terminating null character. So it prints the test correctly and continues to print garbage values till it accidentally comes across a NULL character.

C aptitude 24.2

 
main()

{

char * str = "hello";

char * ptr = str;

char least = 127;

while (*ptr++)

least = (*ptr <least ) ?*ptr :least;

printf("%d",least);

}

Answer: 0

Explanation: After ‘ptr’ reaches the end of the string the value pointed by ‘str’ is ‘’. So the value of ‘str’ is less than that of ‘least’. So the value of ‘least’ finally is 0.

C aptitude 24.3

     main()

{

int i = 257;

int *iPtr = &i;

printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );

}

Answer: 11

Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 23
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about how to prepare and write the C Aptitude Exams?
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

 

Predict the output or error(s) for the following:

C aptitude 23.1

void main(){
int res;
res= 56>76 ? return 0:return 1;
printf("%d",res);
}

Answer: Compilation error.

Explanation: We cannot use return keyword in conditional operator.

C aptitude 23.2
Note: here input for command line argument is, example 5 4 7 8

 
/* example.c */
#include

int main(int argc, char *argv[])
{
int j;
j = argv[1] + argv[2] + argv[3] + argv[4];
printf("%d", j);
return 0;
}

Answer: Error

Explanation: Command line arguments are string types, we can’t add or do the arithmetic operations on string types in C, we have to convert the argv[] to the integer types using atoi function.

C aptitude 23.3

     int main()
{
int num1 = 123;
float num2 = 123.0;
if(num1 == num2)
printf("num1 and num2 are equal");
else
printf("num1 and num2 are not equal");
return 0;
}

Answer: num1 and num2 are equal

Explanation: This is the tricky question, in if statement we are comparing 123=123.0, condition satisfied, so the result is num1 and num2 are equal.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 22
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about how to prepare and write the C Aptitude Exams?
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

 

Predict the output or error(s) for the following:

C aptitude 22.1

int main()
{
int *ptr;
*ptr=100;
return 0;
}

Answer: No error.

Explanation: At the time of compilation ,their is a no error, but at the run time you may caught up with the error “null pointer assignment”, because variable *ptr is not initialized during the declaration..

C aptitude 22.2

 
int main()
{
int example[]={2, 3, 4, 1, 6};
printf("%u, %u, %un", example, &example[0], &example);
return 0;
}

Answer: 1200 1200 1200

Explanation: In the printf statement example, &example are returning the base address of the array, i.e 1200, and &example[0] pointing to the address of the first element of array example, i.e 1200, so answer is 1200 1200 1200.

C aptitude 22.3

     int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%sn", strcpy(str2, strcat(str1, str2)));
return 0;
}

Answer: Hello World

Explanation: In the printf statement strcat function concatenates or appends the string str2 to string str1, so str1 contains Hello World, then strcpy function copies the value of string str1 to string str2, so output is Hello World.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Aptitude Questions and answers with explanation

C Aptitude 21
C program is one of most popular programming language which is used for core level of coding across the board. C program is used for operating systems, embedded systems, system engineering, word processors,hard ware drivers, etc.

In this site, we have discussed various type of C programs till date and from now on, we will move further by looking at the C aptitude questions.

In the coming days, we will post C aptitude questions, answers and explanation for interview preparations.

The C aptitude questions and answers for those questions along with explanation for the interview related queries.

We hope that this questions and answers series on C program will help students and freshers who are looking for a job, and also programmers who are looking to update their aptitude on C program.
Some of the illustrated examples will be from the various companies, and IT industry experts.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

Predict the output or error(s) for the following:

C aptitude 21.1

 char *Fun1()
{
char temp[ ] = “example";
return temp;
}
char *Fun2()
{
char temp[ ] = {‘e’, ‘x’,’a’,’m’,’p’,’l’,'e'};
return temp;
}
int main()
{
puts(Fun1());
puts(Fun2());
}


Answer: Garbage values.

Explanation: Both the functions suffer from the problem of dangling pointers. In Fun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string “string”. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function Fun2() also suffers from the same problem but the problem can be easily identified in this case.

C aptitude 21.2

   char *strexp()
{
char *temp = "example string";
return temp;
}
int main()
{
puts(strexp);
}

Answer: example string

Explanation: The character constants are stored in code/data area and not allocated in stack, so this doesn’t lead to dangling pointers.

C aptitude 21.3

     main()
{
int i=0;
while(+(+i--)!=0)
i-=i++;
printf("%d",i);
}

Answer: -1

Explanation: Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i–!=0) which is false and so breaks out of while loop. The value –1 is printed due to the post-decrement operator.

Read more Similar C Programs 
 
Learn C Programming
C Aptitude
C Interview questions
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com