Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Varun, really nice question.

A lot of beginners run into this when they start working with functions in C, so you’re definitely on the right track noticing it.

Here’s the simple idea:
If you write a function like this:

int getProduct(float a, float b)

C expects that function to return an int, no matter what you actually calculate inside it. So even if a * b produces a floating-point value, C will convert it to an integer when returning it. That’s why the decimal part disappears.

If you want the full, precise result, you need the function to return a floating-point type:

double getProduct(double a, double b) {
    return a * b;
}

This keeps the decimal values and avoids losing accuracy.

And one more thing students often miss:
You have to actually use the returned value, otherwise it's wasted:

double result = getProduct(3.5, 4.6);
printf("Product: %.2f\n", result);

If anything about return types still feels unclear, feel free to ask — I’m happy to help you understand it fully.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Ivan, really nice question.

It’s easy to mix up the idea of a function “returning” with what \n does in a print statement. They sound similar, but they’re completely different things.

When you write:

printf("Hey\nHow are you?");

the \n does not return from the function. It doesn’t stop anything, and it doesn’t exit the print. All it does is tell the output: start a new line here. So the text comes out like this:

Hey
How are you?

The printing continues normally after the newline.
A real return is something you do with a return statement inside a function, not with \n.

If you want more examples or want to see how multiple \n behave, I’m happy to show you.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Zade, really nice question.

The reason the function is defined as int instead of void is that it actually produces a value — the sum. Whenever a function calculates something and you want to use that result later in the program, the function needs a return type that matches the kind of value it gives back. In this case, the sum of natural numbers is an integer, so the function returns an int.

A void function is different. It performs an action but doesn’t hand anything back. For example, a function that only prints something to the screen doesn’t need to return a value, so void makes sense there.

So the rule is simple:
If the function gives back a value, choose a matching return type.
If it doesn’t return anything, use void.

If you have further questions, I'm here to help.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

It’s not a bug. Your count never changes, so count <= 3 stays true forever.

Buggy code:

#include 

int main() {
    int count = 1;

    while (count <= 3) {
        printf("I am inside a loop.\n");
        printf("Looping is interesting.\n");
    }

    return 0;
}

Output (repeats forever):

I am inside a loop.
Looping is interesting.
...

Fix:

#include 

int main() {
    int count = 1;

    while (count <= 3) {
        printf("I am inside a loop.\n");
        printf("Looping is interesting.\n");
        count = count + 1;
    }

    return 0;
}

Output:

I am inside a loop.
Looping is interesting.
I am inside a loop.
Looping is interesting.
I am inside a loop.
Looping is interesting.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Harkirat, in C, isdigit() returns 0 for false and any non-zero number for true, and the exact “true” number can be different depending on the system. So 2048 still just means true.

printf("%d\n", isdigit('8')); // non-zero means true (could be 2048)

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Sanskriti,

Great question. A function doesn’t need a return statement to run, but whether or not you use it depends on what the function is supposed to do.

If a function is only doing a task (like printing something or updating a value) and isn’t meant to give anything back, you don’t need a return value — that's when you define it with void.

But if you want the function to send a result back to the place it was called from, you need a return statement.

Here’s an example:

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 5);
    printf("%d", result);  // Output: 8
    return 0;
}

The add function returns the sum of two numbers. That return value is stored in result and printed. Without return, the function wouldn’t send anything back, and you'd have nothing to work with.

Also, if you declare a function to return a value (like int, float, etc.) but forget to use return, you might get incorrect results or even undefined behavior.

So in short:

  • Use return when you need a result from the function.

  • Skip it if the function is just doing something and doesn’t need to report back.

If you have more questions, I’m here to help.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi MārtiƆơ,

Variables are called temporary because they only exist while the program is running. They're used to store values in memory that your program needs at that moment — like numbers, strings, or results from calculations.

Here’s a simple example:

int main() {
    int score = 0;
    score += 10;
    printf("Current Score: %d\n", score);
    return 0;
}

In this code, the variable score holds a value temporarily. Once the program finishes running, the data stored in score is gone — unless you explicitly save it somewhere, like a file or database.

So to sum up: variables help your program work with data during execution, but they don't keep that data permanently.

If you have more questions, I’m here to help.

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Soumyadeep,

Yes, it’s easier than it seems! Let’s take a simple example using the OR operator (||), which is one kind of logical gate.

The OR operator checks two conditions and gives true if at least one of them is true.

Here’s how it works in code:

if ((age >= 18) || (gpa > 3.5)) {
    printf("You meet the criteria.\n");
}

This line means:
“If the person is 18 or older, or their GPA is above 3.5, then print the message.”

Examples:

  • If age is 20 and GPA is 3.0 → it prints (because age ≄ 18)

  • If age is 16 and GPA is 3.8 → it prints (because GPA > 3.5)

  • If age is 16 and GPA is 3.0 → it doesn’t print (because both are false)

So in simple terms:
OR means only one condition needs to be true for the code to run.

Let me know if you want help understanding other gates like AND or NOT — I’m here to help.

C
This question was asked as part of the Learn C Programming course.
Kelish Rai
Expert
last month
Kelish Rai answered

Hi Jayadithya,

Great question — this trips up a lot of learners.

The sizeof() operator does return the correct value — it gives you the size of a type or variable in bytes. But here’s the key part:
printf() doesn’t know what type you're printing unless you tell it using a format specifier.

So even if sizeof(int) returns a valid number like 4, you still need to use %d or %lu (depending on your system and compiler) to tell printf how to interpret and display that value.

Now, about using different format specifiers — yes, that’s sometimes intentional. For example:

char ch = 'A';
printf("%d", ch);  // Prints 65 instead of 'A'

Here, %d is used to show the numeric (ASCII) value of the character, not the character itself.

So to sum up:

  • sizeof() works fine — the issue is with how you choose to print the result.

  • Sometimes, using a "wrong" format specifier is done on purpose to view data differently.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.
Kelish Rai
Expert
last month
Kelish Rai answered

In C, when you use a comparison like total > 100, it doesn’t return true or false as words — it returns a number:

  • 1 if the condition is true

  • 0 if it’s false

So if you write:

result = total > 100;

C checks if total is greater than 100. If it is, result becomes 1. If not, result becomes 0.

That’s why the output is either 0 or 1 — it's just how C handles boolean logic under the hood.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.