Found 33676 Articles for Programming

Nested functions in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

5K+ Views

In some applications, we have seen that some functions are declared inside another function. This is sometimes known as nested function, but actually this is not the nested function. This is called the lexical scoping. Lexical scoping is not valid in C because the compiler is unable to reach correct memory location of inner function.Nested function definitions cannot access local variables of surrounding blocks. They can access only global variables. In C there are two nested scopes the local and the global. So nested function has some limited use. If we want to create nested function like below, it will ... Read More

Implicit return type int in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

476 Views

If some function has no return type, then the return type will be int implicitly. If return type is not present, then it will not generate any error. However, C99 version does not allow return type to be omitted even if it is int.Example#include my_function(int x) {    return x * 2; } main(void) {    printf("Value is: %d", my_function(10)); }OutputValue is: 20

Java Program to create Arrow Button positioning North

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

259 Views

To create Arrow Button at position north, use BasicArrowButton:BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);Above, we have set the arrow to NORTH. Now add it to Panel:panel.add(arrow, BorderLayout.NORTH);The following is an example to create Arrow Button positioning North:Exampleimport java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo extends JPanel {    public SwingDemo() {       setLayout(new BorderLayout());       JPanel panel = new JPanel(new BorderLayout());       add(panel, BorderLayout.EAST);       BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);       panel.add(arrow, BorderLayout.NORTH);    }    public static void main(String[] args) {       JFrame frame = ... Read More

Using range in switch case in C/C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

8K+ Views

In C or C++, we have used the switch-case statement. In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement.The syntax of using range in Case is like below −case low … highAfter writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.In the following program, we will see what will be the output for the range based case statement.Example#include main() {    int data[10] = { 5, ... Read More

Difference between char s[] and char *s in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

We have seen sometimes the strings are made using char s[], or sometimes char *s. So here we will see is there any difference or they are same?There are some differences. The s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data. But second one is showing only 4 as this is the size of one pointer variable. For the ... Read More

How to change display mode with Java Swings

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

1K+ Views

To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as:new DisplayMode(800, 600, 32, 60));Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.The following is an example to change display mode with Java Swings:Exampleimport java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(800, 600);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       GraphicsDevice graphics = GraphicsEnvironment.getLocalGraphicsEnvironment()   ... Read More

Java program to sort string stream with reversed comparator

Krantik Chavan
Updated on 23-Nov-2024 03:58:06

627 Views

In this article, we will learn how to sort a stream of strings using a reversed comparator in Java. Java 8 introduced the Stream API, which allows powerful operations like sorting using custom comparators. Java Comparator A Comparator is a functional interface in Java that defines custom sorting logic. It compares two objects and returns a result based on the comparison. Java Stream A Stream is a sequence of elements that can be processed in parallel or sequentially, supporting methods like sorting, filtering, and mapping. Sorting string stream with a reversed comparator The following are the steps for sorting a ... Read More

Difference between #define and const in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

880 Views

The #define is preprocessor directives. So when we define some macro using #define, it replaces into the code with its value before compilation. So when the compiler does not know anything about the code, in that time also the macro values are replaced.The constant is actually a variable. By declaring this variable, it occupies memory unit. But we cannot update the value of constant type variable directly. We can change it using some pointer values.Sometimes programmer may think that using macro is better than const, as this is not taking any additional space into the memory, but for some good ... Read More

Sort String Array alphabetically by the initial character only in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here, we are sorting string array alphabetically by the initial character i.e. ‘J’ for ‘John’ will come after ‘Chris’ since the first character of ‘Chris’ is ‘C’.Let us first create a String array:String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };Now, sort the string array based on the first character:Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));The following is an example to sort String Array alphabetically by the initial character only:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" }; ... Read More

Variable length arguments for Macros in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

995 Views

We know that we can use variable length arguments for functions in C. For that we have to use ellipsis (…). Similarly for macros, we can use variable length arguments. Here also we have to include ellipsis, The ‘__VA_ARGS__’ is used to handle variable length arguments. Concatenation operator ‘##’ is used to concatenate the variable arguments.In this example, the Macro will take variable length argument like the printf() or scanf() function. In this macro, we will print the filename, line number, and error messages. The first argument is pr. This is used to determine the priority i.e. whether it is ... Read More

Advertisements