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
Kelish Rai
Expert
last year
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 year
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.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Sanskriti,

Good observation — they’re both used in printf, but for different types of data.

  • %s is used to print a string (a sequence of characters ending with \0)

  • %c is used to print a single character

Here’s an example to make it clear:

char movie[] = "Snowpiercer";

// Prints the full string
printf("The full movie title is: %s\n", movie);

// Prints the 4th character (index 3)
printf("The fourth character is: %c\n", movie[3]);

So if you're printing the entire string, use %s.
If you're just printing one character from that string — like movie[3] — use %c.

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

C
This question was asked as part of the Learn C Programming course.
Udayan Shakya
Expert
last year
Udayan Shakya answered

Hi Harkirat,

Great question — it’s something many learners wonder about.

When you write:

int intValue = (int) doubleValue;

the part (int) is called explicit type casting. You're telling the compiler clearly: "Yes, I know doubleValue is a double, and I want to convert it to an integer."

Even though you're storing the result in an int, if you skip the cast:

int intValue = doubleValue;

C will do an implicit conversion — it still works, but the compiler may give you a warning, especially if there’s a chance of losing data (like dropping decimal points).

Using (int) makes your intention clear and avoids confusion. It’s also a good habit when converting between types, especially when precision matters.

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 year
Abhay Jajodia answered

Hi Suteemon,

Great question — if you want to loop through each digit of an integer (not each number in a range), you can do it using division and modulus.

Here’s how you can do it in C:

int num = 1234;

while (num > 0) {
    int digit = num % 10;  // gets the last digit
    printf("%d\n", digit); // print or process the digit
    num = num / 10;        // remove the last digit
}

This will print:

4  
3  
2  
1

If you want the digits in the original order (1, 2, 3, 4), you can store them in an array or reverse them after collecting.

Let me know if you were asking about looping over a range of numbers instead — happy to help with that too.

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

C
This question was asked as part of the Practice: C Programming course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Adithya,

Good question.
The line #include tells the compiler to include the Standard Input Output library before compiling your program.

This library contains functions like printf() and scanf(). So if you're using printf to display something on the screen, you need to include stdio.h. Without it, the compiler won’t recognize those functions.

It’s like bringing in the tools your program needs to do input and output.

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 year
Abhay Jajodia answered

Hi Jayadithya,

Great question — the difference goes beyond just syntax.

  • Implicit type conversion (also called type promotion) happens automatically when needed. For example:

    int a = 5;
    double b = 3.2;
    double result = a + b;  // 'a' is automatically promoted to double
    

    Here, the compiler handles the conversion safely, and you keep the full floating-point accuracy.

  • Explicit type conversion (casting) is something you do manually. Like:

    double c = 5.7;
    int d = (int)c;  // d becomes 5, decimal part is lost
    

    You’re forcing the type change, and that can lead to data loss — especially when converting from double or float to int.

As for accuracy:

  • If you're converting to float or double, both implicit and explicit methods usually give the same result.

  • If you're converting from a float or double to something else (like int), explicit casting can lose precision, while implicit conversion often avoids that by promoting to a wider type instead.

So the key difference isn’t just the syntax — it’s about control vs. safety. Implicit is automatic and safer, explicit gives you control but comes with more responsibility.

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

C
This question was asked as part of the Learn C Programming course.
Udayan Shakya
Expert
last year
Udayan Shakya answered

Hi Reyann,

Good question. If you’re seeing both double i; and int i in the same code, here’s what’s happening:

The double i is declared outside the for loop, and the int i is declared inside the loop. In C, variables declared inside a block (like a loop) are separate from those outside — even if they have the same name.

So when you write:

double i = 3.5;

for (int i = 0; i < 5; i++) {
    // this 'i' is a different variable
}

The int i inside the loop is its own separate variable, and it temporarily hides the double i. That’s why the compiler doesn’t complain — it’s allowed, but it can be confusing.

Best practice: avoid using the same variable name in different scopes unless there’s a good reason.

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 year
Abhay Jajodia answered

Hi Yeah, great question.

In Java, input.nextLine() is used to read an entire line of text entered by the user — including spaces — up until they press ENTER. This makes it ideal when you're asking for inputs like full names, addresses, or any sentence.

Here's how it works:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your full name:");
        String fullName = input.nextLine();

        System.out.println("Hello, " + fullName);
    }
}

If the user types John Doe, the output will be:

Hello, John Doe

Now, how is it different from nextInt() or next()?

  • nextInt() only reads the integer.

  • next() reads a single word (stops at a space).

  • nextLine() reads the entire line, including any spaces, until ENTER is pressed.

This also means that if you use nextInt() followed by nextLine(), you may get unexpected behavior because nextInt() doesn't consume the newline character. That’s something to watch out for.

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 year
Abhay Jajodia answered

Great question — and yes, in C, the order definitely matters when you're working with pointers.

Let’s break it down:

  • &number means “the address of number

  • pt is a pointer that’s supposed to store that address

So when you write:

pt = &number;  // ✅ Correct

You’re saying: “Assign the address of number to the pointer pt.”

But writing:

&number = pt;  // ❌ Invalid

doesn’t make sense to the compiler. You’re trying to assign a value to an address, which isn’t allowed. In C, you can store an address in a pointer, but you can’t overwrite a memory address like that.

So always think of it this way:

A pointer holds the address — it doesn’t assign to it.

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

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