Cracking C Interview Question : What is ‘x’ ?

Cryptify
3 min readSep 19, 2023

--

hello.c
//hello.c

#include<stdio.h>

int main()
{
int x = printf(“Hello”);
printf(“%d”,x);
return 0;
}

One line Answer :

In the above code the value of 'x' is 5. This gives the output based on which you can store in the 'x' variable in 'printf’.

Note : This C programming interview question is taken from Apna College YouTube channel and this article only explains the code but full credit goes to Apna College.

Video Link : https://youtube.com/shorts/W_pc29_PJYw?si=Nm5q7p6tGmCbl_oW

Explanation of all lines from the code :

This piece of C code does two things. First, it uses the `printf` function to print the word “Hello” on the screen. Second, it prints a number, which is the count of characters that were just printed when “Hello” was displayed. So, it displays “Hello” and then tells you how many characters were used to display it, which is usually 5.

1. #include <stdio.h> : That line of code brings in a library called “standard input/output”. This library gives you access to useful functions such as `printf` which allow you to display information on the screen.

2. int main() : This is the main part of the program where everything starts to move.

3. int x = printf(“Hello”); : This line does two things: it prints the word “Hello” on the screen using `printf`, as well as finds out how many characters were used to write “Hello” and stores what ‘x’ Names are listed in the box. `

4. printf(“%d”, x); : This line displays the number stored in the box named `x` on the screen using ‘printf’. It uses `%d` to indicate that it is working with a numeric value. So, it displays the value of `x` as an integer.

5. return 0; : This line marks the completion of the ``main’’ portion of the program, and also indicates that the program has run successfully by returning the number 0.

Now, let’s talk about the output :

When you run this code it will output the following :

//OutPut

Hello5

Here’s why :

1. The first `printf` statement displays “Hello” on the screen, and it consists of 5 characters.

2. Then, the number 5 is stored in the variable `x`, which means it remembers how many characters were used to print “Hello”.

3. The second `printf` statement displays the number stored in `x` on the screen, which is 5. It uses `%d` to indicate that it is working with a number. So, it shows the value 5.

So, you see “Hello” followed by “5” as output.

Extra Practice :

//salam.c

#include<stdio.h>

int main()
{
int x = printf("Assalamualaikum");
printf(“%d”,x);
return 0;
}
//OutPut

Assalamualaikum15

Thank you so much for reding.🎉If you like this content of mine then quickly clap👏 or press the green button 💚 to follow me 🟩 and don’t forget to share 📩 with your friends if you have any question let me know in the comment section 📩.

--

--

Cryptify

Cyber Security & Web Development. Sharing tips, tutorials, and insights to keep you secure and ahead in the digital world ...!