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
Udayan Shakya
Expert
last year
Udayan Shakya answered

Hi there!

The problem statement is instructing you to select those rows from the Products table whose price is between 145 and 220, BUT the query should not select those rows whose price is either exactly 145 or 220.

This is basically a trick question because if you use the following query, then your solution will be wrong:

SELECT *
FROM Products
WHERE price BETWEEN 145 AND 220;

It's because BETWEEN will also select those rows whose price is 145 or 220.

So if you want to solve this problem using the BETWEEN operator, you need to use BETWEEN 146 AND 219.

This way, you've successfully excluded 145 and 220.

Correct Solution

SELECT *
FROM Products
WHERE price BETWEEN 146 AND 219;

Hope this helps! Let me know if you have more questions.

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

Hello, Kartik! You take user input by creating an object of the Scanner class, and then using the object to call the appropriate method, such as nextInt() for integers, nextDouble() for floating-point numbers, and nextLine() for strings.

For example,

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        
        System.out.println("Enter your age: ");
        
        // Create a Scanner object
        Scanner input = new Scanner(System.in);
        
        // Take user input 
        int age = input.nextInt();
        
        System.out.println("Age is " + age);

    }
}

The basics of user input has already been explained in the Taking Input lesson of Chapter 1: Introduction.

Please review that lesson again before proceeding further.

Hope that helps! Contact us again if you have further questions.

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

Hi there!

Your confusion is completely natural because there are a lot of advanced and deeper aspects of C programming that we haven't taught yet (because they're beyond the scope of our course).

Basically, you're asking why we don't need to print \n right after printing employee1.name, right? That's because of how fgets works.

Basically, fgets not only stores the name that you entered inside employee1.name, but it also stores the newline character \n.

How does this happen?

To give the name input, we type the name and then press the Enter key. Hitting the enter key inputs the newline character \n into the input stream, which fgets ends up storing inside employee1.name.

So if you've entered Brad Pitt as input, fgets will store it like this:

employee1.name = "Brad Pitt\n\0"

Note: Please remember that \0 is the null terminator that's present at the end of all strings in C.

So when you print employee1.name, the \n is already included in that variable. That's why you use

printf("%s %d", employee1.name, employee1.age);

instead of

printf("%s\n%d", employee1.name, employee1.age);

But scanf("%s", string_var) behaves differently

On the other hand, if you use something like this:

scanf("%s", employee1.name) 

then the newline character does not get stored inside employee1.name because scanf throws out everything that comes after a whitespace (including the whitespace itself).

The drawback here is that you can only store a single word.

What's the solution?

The solution I gave you before is good enough, but it's still not perfect because employee1.name stores \n at the end.

This can cause a lot of problems in more complex programs because a name string (or any other string) should only contain the required data (i.e., the name) without unnecessary whitespaces.

So the solution is to trim the string after using fgets.

You can remove/trim the string by following the steps below:

  1. Import the string.h header file by using #include .

  2. Use strcspn() to get the index of the \n character in the string.

  3. Then, replace the character at that index with the null terminating character \0 so that \n gets completely removed.

Here's how to do this:

Using strcspn() to get index of \n

// Get the index of \n
int newline_index = strcspn(employee1.name, "\n");

Here, strcspn(employee1.name, "\n") compares two strings: employee1.name and "\n", and gives the index of the first occurrence of the second string i.e., "\n".

Since employee1.name has only one instance of \n in it, strcspn() effectively gives us the position of the extra line at the end of this string.

Then, we remove this newline character by replacing it with \0:

// Replace the \n with \0
employee1.name[newline_index] = '\0';

You can also simply combine the two steps like this:

employee1.name[strcspn(employee1.name, "\n")] = '\0';

Here's the FULL SOLUTION:

#include 
#include 

// create Person struct
struct Employee {
  int age;
  char name[50];
};

int main() {

  // create struct variable
  struct Employee employee1;

  // get age input for employee1's age
  scanf("%d", &employee1.age);
  getchar();

  // get name input for employee1's name
  fgets(employee1.name, sizeof(employee1.name), stdin);

  // Get the index of \n
  int newline_index = strcspn(employee1.name, "\n");

  // Replace the \n with \0
  employee1.name[newline_index] = '\0';
  
  // print name and age
  printf("%s\n", employee1.name);
  printf("%d", employee1.age);

  return 0;
}

Note: It's better to use employee1.name[strcspn(employee1.name, "\n")] = '\0'; instead of storing the \n index in a separate variable. The whole point of C programming is to be memory-efficient.

Why is this not included in the course (or in my previous answers)?

I wanted to inform you about this issue yesterday but thought the answer might get too long and confusing.

So I'm thankful that you asked this followup question.

Currently, our course cannot go into such deep detail because it's aimed at absolute beginners.

For now, I can only tell learners how to properly deal with it when they ask questions, as you've done here.

I hope you found this helpful! Please don't hesitate to ask further questions if you're still confused.

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

Hi again! You don't need null terminators for an array of numbers. You only need to add null terminators at the end of strings.

This is because strings in C are just character arrays. But how do you know if a char array is a string or just an ordinary group of characters?

For example, suppose you have the following arrays:

// not a C string, just a simple char array
char vowels[5] = { 'a', 'e', 'i', 'o', 'u' };

// is a C string because of \0
char greeting1[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };

// also a C string (compiler adds '\0')
char greeting2[] = "Hello";

Basically, normal char arrays don't have \0 at the end, but strings do. The presence of \0 is how C knows whether something is a string or not.

So, you don't need to put \0 in arrays except for strings.

Hope that helps!

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

When you use fptr == NULL, you're actually checking if fptr is equal to the special value NULL, which indicates that no file was opened successfully at that pointer.

Basically, fopen() is designed to either:

  • return a pointer to a FILE object representing the file you want to work with,

  • or, if there's an error (like if the file isn't found), it returns NULL.

  • The value NULL in this context signals that the pointer fptr doesn't point to any valid file, allowing you to safely check if the file operation succeeded.

In other words, NULL isn't just an ordinary value; it's actually a special marker that's treated differently from ordinary literals such as 1, 0, "yes", "no", etc.

That's why you can indeed check fptr == NULL but can't do checks such as fptr == "yes" since that's not allowed in pointer operations.

Hope this helps you understand the behavior of NULL and pointers better! Let me know if you have any more questions.

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

Hi again! A short answer to this particular question is included in my answer to your previous question.

If you're still confused, you can try the following program and see what happens:

#include 

int main() {

  int age;

  // get age input
  printf("Enter age: ");
  scanf("%d\n", &age);
  
  // print age
  printf("Age: %d", age);

  return 0;
}

You'll see that this program "hangs" after you've entered the age and pressed enter, i.e., it doesn't print the age after you've given the input.

The only way to make it print the age is to give another input and press enter. Here's my full output:

Enter age: 25
a
Age: 25

Notice that I had to enter a (or any other non-whitespace input) to print the age.

Obviously, this is bad because the program appears to be hanging/freezing in the middle of the execution.

And as I've explained in my previous answer, this happens because you're telling scanf() to consume all whitespace characters and wait for non-whitespace character input.

That's what scanf("%d\n", &variable); does, and it's not something you want in your code.

Hope this helps! Let me know if you have any more questions.

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

Hi there! At first glance, your code appears to make intuitive sense. But thanks to the way C programming functions internally, you've ended up with several problems in your code.

The two major issues are:

1. Using \n with scanf

Here's how you've taken input for age:

scanf("%d\n", &enployee1.age);

Please don't do this. In scanf format strings, whitespace characters (including \n) mean: consume any amount of whitespace, then keep waiting until the next non-whitespace character appears.

So scanf("%d\n", ...) will:

  • read the integer,

  • then consume the newline you typed,

  • and then block until you type the first non-whitespace character of the next input (for example, the first letter of the name).

To fix this, please remove \n from this code like this:

scanf("%d", &enployee1.age);

Then consume the leftover newline before reading a line of text:

getchar(); 

Here, getchar() will consume the newline character \n entered after the age input so it doesn't interfere with the next input statement.

2. Getting individual character input for "name" and printing the individual characters instead of printing the name as a single string.

On the surface, it makes sense to use a loop to get individual character inputs for a string (since scanf() can only take input for a single word).

But there are many problems with this:

  • Using a loop from 0 to 49 means the code will always take input for 50 characters, even if the actual name contains far less characters (say, 20 characters). This is wasteful.

  • This loop also doesn't add a null terminating character \0 at the end of the string.

  • Printing each character of the string is not ideal. It's better to print the entire name as a string rather than individual characters.

To fix this, please use fgets() to get string input like this:

fgets(employee1.name, sizeof(employee1.name), stdin);

A Minor Spelling Mistake

You've misspelled Employee by writing it as Enployee, and your object names also have this mistake.

This doesn't matter within the code but it's still better to use proper spellings.

Full Solution

#include 

// create Person struct
struct Employee {
  int age;
  char name[50];
};

int main() {

  // create struct variable
  struct Employee employee1;

  // get age input for employee1's age
  scanf("%d", &employee1.age);

  // use getchar() to consume the newline
  getchar();

  // get name input for employee1's name
  fgets(employee1.name, sizeof(employee1.name), stdin);

  // print name and age
  printf("%s", employee1.name);
  printf("%d", employee1.age);

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

Hello there! It's natural to be confused about the sizeof operator in C, and whether it includes the null terminator as well.

To answer your question: no, we don't need to write sizeof(name) + 1 because sizeof measures the full size of the variable (in bytes), which includes the null terminator as well.

Hope that helps! Contact us again if you have further questions.

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

Great question — let’s break it down clearly.

Operator precedence decides which operation is performed first in an expression. For example, multiplication and division have higher priority than addition and subtraction. So in this expression:

9 / 3 + 8 * 4 - 2

C++ first evaluates 9 / 3 and 8 * 4, then performs the addition and subtraction.

Associativity decides the direction in which operators are evaluated when they have the same priority. In C++, most arithmetic operators are evaluated from left to right. So in:

3 + 32 - 2

the addition is done before the subtraction.

To make expressions easier to read and control, you can use parentheses, like this:

(9 / 3) + (8 * 4) - 2

This clearly shows which parts are calculated first.

C++
This question was asked as part of the Learn C++ Basics course.
睿宏 陳
PRO
last year
睿宏country asked
Palistha Singh
Expert
last year

Hi! You usually should not add \n after %d in scanf.

%d already skips leading whitespace (spaces, tabs, newlines). If you write scanf("%d\n", &x);, that \n tells scanf to keep waiting for more whitespace after the number, and it can look like the program is stuck until you press Enter again or type something else.

So use this:

scanf("%d", &ages[i]);

If you want each input on a new line, that’s handled by how the user types, not by adding \n to scanf.

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