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 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 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 Program to change the text colors.

Write a C Program to change the text colors.
In this program, we give the example of changing the text colors, background colors using conio.h library.
Syntax: void textcolor(int_color);
Here color is the integer variable, you can specify a color name also, but it should be a proper color name in capital letters.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
textcolor(BLUE); // Change font colour to blue
textbackground(WHITE); //change the background colour to white
cprintf("Color is Blue with white backgroundnn");
clrscr();
textcolor(RED+BLINK);//this one is blinking the text
cprintf("Color is RED with blinkingnn");
return 0;
}
Read more Similar C Programs
 
Graphics in C

C Strings 
 
C Aptitude 
 

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 18
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 18.1

  main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}



Answer: 2 5 5

Explanation: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the ‘’ termination character). The third sizeof is similar to the second one.

C aptitude 18.2

  main()
{
char not;
not=!2;
printf("%d",not);
}


Answer: 0
Explanation:! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

C aptitude 18.3

    #define FALSE -1
#define TRUE 1
#define NULL 0
main() {
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}

Answer: TRUE

Explanation: The input program to the compiler after processing by the preprocessor is,
main()
{
  if(0)
    puts(“NULL”);
  else if(-1)
    puts(“TRUE”);
  else
    puts(“FALSE”);
 }
Preprocessor doesn’t replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence “TRUE” is 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 15
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 15.1

  main()
{
extern out;
printf("%d", out);
}
int out=100;


Answer: 100

Explanation: This is the correct way of writing the previous program 14.3

C aptitude 15.2

 main()
{
show();
}
void show()
{
printf("I'm the greatest");
}

Answer: Compier error: Type mismatch in redeclaration of show.

Explanation:When the compiler sees the function show it doesn’t know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.
The solutions are as follows:
1. declare void show() in main() .
2. define show() before main().
3. declare extern void show() before the use of show().

C aptitude 15.3

    main( )
{
int a[ ] = {10,20,30,40,50},j,*p;
for(j=0; j<5; j++)
{
printf(“%d” ,*a);
a++;
}
p = a;
for(j=0; j<5; j++)
{
printf(“%d ” ,*p);
p++;
}
}

Answer:Compiler error: lvalue required..

Explanation: Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue.

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 14
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 14.1

  main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}


Answer: Compiler Error

Explanation: in the end of nested structure yy a member have to be declared

C aptitude 14.2

  main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}

Answer: Linker error: undefined symbol ‘i’.

Explanation:extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn’t find an error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.

C aptitude 14.3

     main()
{
printf("%d", out);
}

int out=100;

Answer: Compiler error: undefined symbol out in function main.

Explanation: The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main.

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

Aptitude Questions and answers with explanation

C Aptitude 11
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 11.1

   main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2)
goto here;
i++;
}
}
fun()
{
here:
printf("PP");
}

Answer: Compiler error: Undefined label ‘here’ in function main

Explanation: Labels have functions scope, in other words The scope of the labels is limited to functions . The label ‘here’ is available in function fun() Hence it is not visible in function main.

C aptitude 11.2

   main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}

Answer: Compiler error: Lvalue required in function main

Explanation:Array names are pointer constants. So it cannot be modified.

C aptitude 11.3

    void main()
{
int i=5;
printf("%d",i++ + ++i);
}

Answer: Output Cannot be predicted exactly.

Explanation: Side effects are involved in the evaluation of i

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 5
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 5.1

    #define int char
main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}

Answer: sizeof(i)=1

Explanation: Since the #define replaces the string int by the macro char

C aptitude 5.2

main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}


Answer:i=0

Explanation:In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

C aptitude 5.3

     #include
main()
{
char s[]={'a','b','c','n','c',''};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}

Answer:77

Explanation: p is pointing to character ‘n’. str1 is pointing to character ‘a’ ++*p. “p is pointing to ‘n’ and that is incremented by one.” the ASCII value of ‘n’ is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to ‘a’ that is incremented by 1 and it becomes ‘b’. ASCII value of ‘b’ is 98. Now performing (11 + 98 – 32), we get 77(“M”); So we get the output 77 :: “M” (Ascii is 77).

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 4
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 4.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 4.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 4.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