1)
#include <stdio.h>
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 ‘\0’. So the value of ‘str’ is less than that of ‘least’. So the value of ‘least’ finally is 0.
2) Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Answer:
(char*(*)( )) (*ptr[N])(...
Tuesday
Monday
Program of Simple Arithmetic Operation in C
Here's a program that performs basic arithmetic operations like addition, subtraction, multiplication and division using C programming languag...
C Aptitude Questions with Answers Part 32
1)
void main() {
char ch;
for(ch=0;ch<=127;ch++)
printf(“%c %d \n“, ch, ch);
}
Answer:
Implementaion dependent
Explanation:
The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127.
2) Is this code legal?
int *ptr;
ptr = (int *) 0x400;
Answer:
Yes
Explanation:
The pointer ptr will point at the integer in the memory location 0x400.
3)
main() {
char a[4]="HELLO";
printf("%s",a);
}
Answer:...
C Aptitude Questions with Answers Part 31
1)
#define assert(cond) if(!(cond)) \
(fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\
__FILE__,__LINE__), abort())
void main()
{
int i = 10;
if(i==0)
assert(i < 100);
else
printf("This statement becomes else for if in assert macro");
}
Answer:
No output
Explanation:
The else part in which the printf is there becomes the else for if in the assert macro.
Hence nothing is printed.
The solution is to use conditional operator instead of if statement,
#define assert(cond) ((cond)?(0):...
Thursday
C Aptitude Questions with Answers Part 29
1)
main() {
static int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
static *p[]={a,a+1,a+2};
for(i=0;i<3;i++) {
for(j=0;j<3;j++)
printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j),
*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
}
}
Answer:
1 1 1 1 2 4 2 4 3 7 3 7 4 2 4 2 5 5 5 5 6 8 6 8 7 3 7 3 8 6 8 6 9 9 9 9
Explanation:...
C Aptitude Questions with Answers Part 28
1)
main() {
extern i;
printf("%d\n",i);
{
int i=20;
printf("%d\n",i);
}
}
Answer:
Linker Error : Unresolved external symbol i
Explanation:
The identifier i is available in the inner block and so using extern has no use in resolving it.
2)
main() {
int a=2,*f1,*f2;
f1=f2=&a;
*f2+=*f2+=a+=2.5;
printf("\n%d %d %d",a,*f1,*f2);
}
Answer:
16 16 16
Explanation:
f1 and f2 both refer to the same memory location a. So changes through f1and f2 ultimately affects only the value of a.
3)...
C Aptitude Questions with Answers Part 27
1)
1. const char *a;
2. char* const a;
3. char const *a;
-Differentiate the above declarations.
Answer:
1. 'const' applies to char * rather than 'a' ( pointer to a constant char )
*a='F' : illegal
a="Hi" : legal
2. 'const' applies to 'a' rather than to the value of a (constant pointer to char )
*a='F' : legal
a="Hi" : illegal
3. Same as 1.
2)
main(){
int i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
Answer:
1 10
Explanation:
The expression can be written as i=(i&=(j&&10));...
C Aptitude Questions with Answers Part 26
1)
main() {
char str1[] = {‘s’,’o’,’m’,’e’};
char str2[] = {‘s’,’o’,’m’,’e’,’\0’};
while (strcmp(str1,str2))
printf(“Strings are not equal\n”);
}
Answer:
“Strings are not equal” “Strings are not equal” ….
Explanation:
If a string constant is initialized explicitly with characters, ‘\0’ is not appended automatically to the string. Since str1 doesn’t have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a ‘\0’. So str1...
Tuesday
C Program to Generate Magic Square
Here's a C program to generate magic square with output. This program uses C concepts like GOTO statement, Modulus in C, Multidimensional Arrays, IF-Else Condition, For loop and Nested Loops...
Program to Generate Permutation of String in C | C Program
Here's a C program to generate permutations of a string with output. This program makes use of C concepts like Array, For loop, IF-Else Condition, Recursion and String Functions like string copy (strcpy()) and string length (strlen()) functions et...
Friday
C Aptitude Questions with Answers Part 21
1)
main() {
int a[10];
printf("%d",*a+1-*a+3);
}
Answer:
4
Explanation:
*a and -*a cancels out. The result is as simple as 1 + 3 = 4 !
2)
#define prod(a,b) a*b
main() {
int x=3,y=4;
printf("%d",prod(x+2,y-1));
}
Answer:
10
Explanation:
The macro expands and evaluates to as:
x+2*y-1 => x+(2*y)-1 => 10
3)
main() {
unsigned int i=65000;
while(i++!=0);
printf("%d",i);
}
Answer:
1
Explanation:
Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while...
C Aptitude Questions with Answers Part 25
1)
int aaa() {printf(“Hi”);}
int bbb(){printf(“hello”);}
iny ccc(){printf(“bye”);}
main() {
int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}
Answer:
bye
Explanation:
int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized with the address of the function aaa. Similarly, the other two array elements also get initialized...
C Aptitude Questions with Answers Part 24
1)
void main() {
if(~0 == (unsigned int)-1)
printf(“You can answer this if you know how values are represented in
memory”);
}
Answer
You can answer this if you know how values are represented in memory
Explanation
~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. –1 is represented in unsigned value as all 1’s and so both are equal.
2)
int swap(int *a,int *b) {
*a=*a+*b;*b=*a-*b;*a=*a-*b;
}
main() {
int x=10,y=20;
swap(&x,&y);...
C Aptitude Questions with Answers Part 23
116). What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
main(){
error g1;
g1=1;
printf("%d",g1);
}
Answer
Compiler error: Multiple declaration for error
Explanation
The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to ...
C Aptitude Questions with Answers Part 22
1)
void pascal f(int i,int j,int k) {
printf(“%d %d %d”,i, j, k);
}
void cdecl f(int i,int j,int k) {
printf(“%d %d %d”,i, j, k);
}
main() {
int i=10;
f(i++,i++,i++);
printf(" %d\n",i);
i=10;
f(i++,i++,i++);
printf(" %d",i);
}
Answer:
10 11 12 13 12 11 10 13
Explanation:
Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left.
2). What is the output of the...
Tuesday
C Aptitude Questions with Answers Part 20
1)
void main() {
int i=i++,j=j++,k=k++;
printf(“%d%d%d”,i,j,k);
}
Answer:
Garbage values.
Explanation:
An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO).
2)
void main() {
static int i=i++, j=j++, k=k++;
printf(“i = %d j = %d k = %d”, i, j, k);
}
Answer:
i = 1 j = 1 k = 1
Explanation:
Since...
C Aptitude Questions with Answers Part 19
96)
void main() {
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
Answer:
0 0 0 0
Explanation:
The variable "I" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned.
97)
void main() {
int k=ret(sizeof(float));
printf("\n here value is...
C Aptitude Questions with Answers Part 18
90)
main(){
unsigned int i;
for(i=1;i>-2;i--)
printf("c aptitude");
}
Explanation:
i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop.
91) In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'.
main(){
int * j;
void fun(int **);
fun(&j);...
C Aptitude Questions with Answers Part 17
1)
# 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.
2)
#include<stdio.h>
main() {
FILE *ptr;...
C Aptitude Questions with Answers Part 16
1)
struct point {
int x;
int y;
};
struct point origin,*pp;
main() {
pp=&origin;
printf("origin is(%d%d)\n",(*pp).x,(*pp).y);
printf("origin is (%d%d)\n",pp->x,pp->y);
}
Answer:
origin is(0,0) origin is(0,0)
Explanation:
pp is a pointer to structure. we can access the elements of the structure either with arrow mark or with indirection operator.
Since structure point is globally declared x & y are initialized as zeroes
2)
main() {
int i=_l_abc(10);
printf("%d\n",--i);...
Friday
C Aptitude Questions with Answers Part 15
1)
#include<stdio.h>
main() {
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}
Answer:
Compiler error
Explanation:
i is a constant. you cannot change the value of constant
2)
#include<stdio.h>
main() {
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d..%d",*p,*q);
}
Answer:
garbagevalue..1
Explanation:
p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third 2D(which you are not...
C Aptitude Questions with Answers Part 14
1)
main() {
int y;
scanf("%d",&y); // input given is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
Answer:
2000 is a leap year
Explanation:
An ordinary program to check if leap year or not.
2)
#define max 5
#define int arr1[max]
main() {
typedef char arr2[max];
arr1 list={0,1,2,3,4};
arr2 name="name";
printf("%d %s",list[0],name);
}
Answer:
Compiler error (in the line arr1 list = {0,1,2,3,4})
Explanation:...
C Aptitude Questions with Answers Part 13
1)
main() {
char *cptr,c;
void *vptr,v;
c=10; v=0;
cptr=&c; vptr=&v;
printf("%c%v",c,v);
}
Answer:
Compiler error (at line number 4): size of v is Unknown.
Explanation:
You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error.
2)
main() {
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}
Answer:
2...
C Aptitude Questions with Answers Part 12
1) What are the files which are automatically opened when a C file is executed?
Answer:
stdin, stdout, stderr (standard input,standard output,standard error).
2) what will be the position of the file marker?
a: fseek(ptr,0,SEEK_SET);
b: fseek(ptr,0,SEEK_CUR);
Answer :
a: The SEEK_SET sets the file position marker to the starting of the file. b: The SEEK_CUR sets the file position marker to the current position of the file.
3)
main() {
char name[10],s[12];
scanf(" \"%[^\"]\"",s);
}
How scanf will execute?
Answer:...
C Aptitude Questions with Answers Part 11
1)
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...
Thursday
C Aptitude Questions with Answers Part 10

1)
main(){
extern out;
printf("%d", out);
}
int out=100;
Answer:
100
Explanation:
This is the correct way of writing the previous program.
2)
main(){
show();
}
void show(){
printf("I'm the greatest");
}
Answer:
Complier error: Type mismatch in redeclaration of show.
Explanation:
When the compiler sees the function show it doesn't know anything about it.So the...
C Aptitude Questions with Answers Part 9
1)
#include<stdio.h>
main() {
struct xx {
int x=3;
char name[]="hello";
};
struct xx *s=malloc(sizeof(struct xx));
printf("%d",s->x);
printf("%s",s->name);
}
Answer:
Compiler Error
Explanation:
Initialization should not be done for structure members inside the structure declaration
2)
#include<stdio.h>
main() {
struct xx {
int x;
struct yy {
char s;
struct xx *p;
};
struct yy *q;
};
}
Answer:
Compiler Error
Explanation:
in the...
C Aptitude Questions with Answers Part 8
36)
#include<stdio.h>
main() {
int i=1,j=2;
switch(i) {
case 1: printf("GOOD");
break;
case j: printf("BAD");
break;
}
}
Answer:
Compiler Error: Constant expression required in function main.
Explanation:
The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).
Enumerated types can be used in case statements.
37)
main() {
int i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
Answer:
1...
C Aptitude Questions with Answers Part 7
1)
main(){
char *p;
p="Hello";
printf("%c\n",*&*p);
}
Answer:
H
Explanation:
* is a dereference operator & is a reference operator. They can be applied
any number of times provided it is meaningful. Here p points to the first
character in the string "Hello". *p dereferences it and so its value is H. Again
& references it to an address and * dereferences it to the value H.
2)
main() {
int i=1;
while (i<=5) {
printf("%d",i);
if (i>2)
goto here;
...
C Aptitude Questions with Answers Part 6
1)
main() {
clrscr();
}
clrscr();
Answer:
No output/error
Explanation:
The first clrscr() occurs inside a function. So it becomes a function call. In the
second clrscr(); is a function declaration (because it is not inside any
function).
2)
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.
3)
void main() {
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));...
Subscribe to:
Posts (Atom)
Labels
C Program Examples
For Loops
C Aptitude Questions
C Interview Questions with Answers
If Else in C
Nested Loops
Arrays in C
Modulus in C
While Loop
C Programming Tips
If in C
Arithmetic Operations
Auto Incrementing Operator ++
Break
C Programming Facts
Strings in C
C Concepts
Matrix in C
Float Data Type
Power Function pow()
Switch Case
gets Function in C
Entering Value in a Matrix
String Functions
C Operators
C++ Interview Questions with Answers
Continue in C
String Copy strcpy()
Trigonometry Examples
Auto Decrementing Operator --
CTYPE.H Header File in C
Division in C
Fibonacci Series
For Loop with No Body
GOTO Statement
Pointer in C
Sorting
String Length strlen()
Ternary Operator
Two Dimensional Arrays in C
Type Casting in C
Using Arrays as Pointers in C
Using Pointers as Arrays in C
C Header Files
Constructor
Destructor
Do While Loop
Finding Day of Given Date
Finding Leap Year
User Defined Functions in C