C questions and answers

https://www.

C questions and answers with explanation

1. Data types        2. Operators              3. Pointers
4. Arrays            5. Loops                  5. Preprocessor
6. Structures        6. If else                6. Switch case
7. printf            8. Variables              7. File handling
8. Strings           9. Command line arguments

C questions and answers for interview

Online Tests

1. Practice set 1    2. Practice set_2         3. Practice set_3

C tutorials:

1. Memory mapping    2. Variables              3. Data types
4. Storage Class     5. Looping                5. Pointers
6. Functions         6. Arrays                 6. Preprocessor
7. File handling     8. Advance c    
         
C pdf to free download

C interview questions and answers


C interview questions and answers for freshers. It is basic c language technical frequently asked interview questions and answers. It includes data structures, pointers interview questions and answers for experienced
Interview questions and answer of C with explanation for fresher

1
Write a c program to print Hello world without using any semicolon.
Answer
Explanation:
Solution: 1
void main(){
    if(printf("Hello world")){
    }
}

Solution: 2
void main(){
    while(!printf("Hello world")){
    }
}

Solution: 3
void main(){
    switch(printf("Hello world")){
    }
}

2
Swap two variables without using third variable.
Answer
Explanation:
#include<stdio.h>
int main(){
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    return 0;
}

3
What is dangling pointer in c? 
Answer
Explanation:
Dangling pointer:

A dangling pointer arises when it continues to reference a memory address that once held a variable but has since been deallocated. This situation is commonly referred to as the dangling pointer problem.

Initially:

Later:
For example:

What will be output of following c program?

#include<stdio.h>

int *call();
int main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){

int x=25;
++x;

return &x;
}

Output: Garbage value

Note: Certain compilers may issue a warning message when returning the address of a local variable or a temporary.

Explanation: The variable 'x' is a local variable, confined to the scope and lifetime of the function call. Consequently, after returning the address of 'x', the variable 'x' becomes invalid, while the pointer 'ptr' persists in pointing to that location.


Solution of this problem: 
Make the variable x is as static variable. In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

#include<stdio.h>

int *call();
int main(){
int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){

static int x=25;
++x;

return &x;
}

Output: 26

4
What is wild pointer in c?  
Answer
Explanation:
An uninitialized pointer in C is commonly referred to as a "wild pointer."
Example:

What will be output of following c program?

int main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
return 0;
}

Output: Any address
Garbage value

In this context, 'ptr' is deemed a wild pointer due to its lack of initialization. It's crucial to distinguish between a NULL pointer and a wild pointer. A NULL pointer points to the base address of a segment, serving as a deliberate indication of no specific memory location, whereas a wild pointer lacks such a targeted assignment.

5