Pages

Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Thursday, August 15, 2024

What is anti- Debugging

 

Anti- debugging is how we can stop somebody from debugging our system and getting all the information out of your subsystem.

Friday, July 26, 2024

Find the Output of the C program

  


Output

20
40

Explanation
the  printf will print the local declaration to that block 

Wednesday, July 24, 2024

Find the output of the C program

 


Output
20

Explanation:
the local declaration will be printed as it lies within the block that is {}

 

Monday, July 22, 2024

Write a macro to swap adjacent nibbles (word size is 32 bits)

 #define SWAP_NIBBLE(num)   (((num & 0xF0F0F0F0) >>4) | ((num & 0x0F0F0F0F) << 4)))


Explanation

if num = 0x12345678 the output should be 0x21436587


Extract the alternate nibbles and shit by 4

            ((num & 0xF0F0F0F0) >>4) = 0x01030507

           ((num & 0x0F0F0F0F) << 4) = 0x20406080

ANDing both we get 

                                                0x21436587



Thursday, July 18, 2024

What is the use of the functions atoi(), itoa() and gcvt()

 atoi() - converts a string to an internet

 itoa() - converts an integer to string 

 gcvt() - converts a floating point number to a string 

Wednesday, July 17, 2024

What do you mean by scope of a variable and what are the different types of scopes that a variable can have?

 Scope of a variable means the area of code in which the variable has effect.


The four different kinds of scopes are

  • File
  • Function
  • Block 
  • Prototype

Tuesday, July 16, 2024

What are the different types of linkages

 There are three different types of linkages

  1. External linkage - global, non static variables and functions
  2. Internal linkage - static variables and functions with file scope
  3. No linkage - local variables 

Monday, July 15, 2024

Find the output - size of pointers

 

#include <stdio.h>
#include <string.h>

int main()
{
    void *str = "Hello";
    printf("%1d %1d\n ",strlen(str),sizeof(str));
    return 0;
}


Output

5,8


Explanation

strlen returns the size of the string i.e. "Hello" takes 5bytes

sizeof gives the size allocated for the datatype, In our case a pointer is allocated 8bytes

Sunday, July 14, 2024

Where are static variables stored in memory

 Static variables are stored in the data segment.

The data segment also contains function call frames and dynamically allocated memory.

Saturday, July 13, 2024

Friday, July 12, 2024

Interview Question :Find the output of the following C program



#include <stdio.h>

int main()
{
    int k = 1;
    printf("%d == 1 is" "%s\n ",k,k == 1? "TRUE": "FALSE");
    return 0;
}



Solution : 
                 1 == 1 is TRUE

Explanation : 

                         printf("%d == 1 is" "%s\n",k,k == 1 ? "TRUE" : "FALSE");

The underlined statement can be expanded as:

                        if(k == 1)
                        {
                                "TRUE"
                        }
                        else 
                        {
                                "FALSE"
                        }
                        
 Since k is initialised to 1,Hence
                            k = TRUE
The datatype assigned for k is integer and its value is 1

So %d in the printf statement will print 1 
While the %s will print the string "TRUE"

Wednesday, July 10, 2024

Multiply a given number by 3.5 without using the multiplication operator (*)

#include<stdio.h>
int main()
{
    int num;
    int result;
    printf("Enter the number: ");
    scanf("%d",&num);

    result = ((num << 2) - (num >> 1));
    printf("\nResult = %d",result);
    return 0;
}


Explanation

Lets take the input as 10

10 = 0x1010 in hex

So,

                            0x1010 << 2 = 0x101000 = 40

                            0x1010 >>1 = 0x0101  = 5

hence 

                            result = 40 - 5 =35 = 10*3.5

You can try with other numbers as well.

Sunday, June 16, 2024

What is Static reallocation in C

 During the linker stage, the system has multiple object files to combine and it may be a possibility that some functions might have the same addresses so relocation or changing these addressing during combining the object file is called Static reallocation.

Wednesday, June 5, 2024

MultiLine Macros in C

Macros can be single line as well as multiline

for example :

#define AddMacro(x,y) \
{\
    int total = x + y; \
    printf("%d",total); \
}

In the above example \ is used to tell the compiler to consider the below lines too
if \ is missing then only the first line will be considered as Macro.

Tuesday, June 4, 2024

Toggle a bit in C using XOR

  


int number= 0b11001100;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
number ^= 1 << 2;
delay(100);
Serial.println(number,BIN);

}



since we count from zero the 3rd last bit will toggle









Sunday, June 2, 2024

What is Stack overflow

  If a program accesses a memory beyond the available limit then a stack overflow occurs and the program gets terminated

Saturday, June 1, 2024

C Language Interview Question:What is the use of "##" operator in C

  #include <stdio.h>

#define concat(a, b) a##b

int main(void)

{

    int yx =7;

    printf("%d\n", concat(y, x));

    printf("%d\n", concat(3, 2));

    return 0;

}

OUTPUT

7

32


EXPLAINATION

the ## operator will concatinate the parameters passed to the macro

In the first case

                            concat(y, x) - the macro will output yx and pass its value to printf as yx is treated as a variable by printf

In the second case

                            concat(3, 2) - the macro will output 32 and printf will print 32 as its treated as an integer


What is anti- Debugging

  Anti- debugging is how we can stop somebody from debugging our system and getting all the information out of your subsystem.