Found 1401 Articles for C

C Program to validate an IP address

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

7K+ Views

In this program we will see how to validate an IP address using C. The IPv4 addresses are represented in dot-decimal notation. There are four decimal numbers (all are ranging from 0 to 255). These four numbers are separated by three dots.An example of a valid IP is: 192.168.4.1To validate the IP address we should follow these stepsTokenize the string (IP address) using the dot “.” delimiterIf the sub strings are containing any non-numeric character, then return falseIf the number in each token is not in range 0 to 255, then return falseIf there are exactly three dots and four ... Read More

C program to print characters without using format specifiers

George John
Updated on 30-Jul-2019 22:30:25

563 Views

In this article we will see how we can print some characters without using any kind of format specifiers. The format specifiers in C are %d, %f, %c etc.These are used to print characters and numbers in C using the printf() function.Here we will see another way to print characters without using %c format specifier. This can be done by putting ASCII values directly in hexadecimal form.Example Code#include main () { printf("\x41 "); //41 is ASCII of A in Hex printf("\x52 "); //41 is ASCII of A in Hex printf("\x69 ... Read More

Write a C program that does not terminate when Ctrl+C is pressed

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section we will see how to write a program in C that cannot be terminated by the Ctrl + C key.The Ctrl + C generates the keyboard interrupt, and it stops the execution of the current process. Here when we will press the Ctrl + C key, it will print a message then continues the execution. To use this functionality, we will use the signal handling technique in C. When the Ctrl + C is pressed it generates SIGINT signal. There are some other signals and their functionalities in the following list.SignalDescriptionSIGABRTIndicates Abnormal terminationSIGFPE Indicates floating point exceptionSIGILL Indicates invalid ... Read More

C Program to print “Hello World!” without using a semicolon

Chandu yadav
Updated on 30-Jul-2019 22:30:25

4K+ Views

Let us see how to write a C program in which we can print the text “Hello World” without using any semicolon.We can simply write the text by using the line printf(“Hello World”); in the main() function.But there is a semicolon at the end of the line. To avoid the semicolon, we can follow some trick. We can use the same printf() statement inside if condition. As the printf() statement returns the length of the text, so it is non zero value, so the if statement will be true. Thus the text will be written on screen.Example Code#include main() { ... Read More

Program to print numbers from 1 to 100 without using loop

George John
Updated on 30-Jul-2019 22:30:25

544 Views

Here we will see how to write a C program that can print numbers from 1 to 100 without using any kind of loops.This problem can be solved using the recursion. We will create a function that will be called recursively. As we know that a recursive function has basically two sections. The base case and the recursive call and other operation. In this function the base case is the argument n is greater than 1. Until it reaches 1, the function will be called recursively. Now at the end it will print the value of n. Thus the entire ... Read More

C Program to print “Even” or “Odd” without using Conditional statement

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

9K+ Views

In this section we will see how to check whether a number is odd or even without using any kind of conditional statements like (=, ==).We can easily check the odd or even by using the conditional statements. We can divide the number by 2, then check whether the remainder is 0 or not. if 0, then it is even. Otherwise we can perform AND operation with the number and 1. If the answer is 0, then it is even, otherwise odd.Here no conditional statements can be used. We will see two different methods to check the odd or even.Method ... Read More

Does Ternary operation exist in MySQL just like C or C++?

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

49 Views

Yes, let us first see the working of ternary operator in C or C++ language.X=(X > 10 && ( X-Y) < 0) ?: X:(X-Y);Here is the demo code in C language. After that we will check in MySQL. The C code is as follows −#include int main() {    int X;    int Y;    int result;    printf("Enter the value for X:");    scanf("%d", &X);    printf("Enter the value for Y:");    scanf("%d", &Y);    result=( X > 1 && (X-Y) < 0) ? X: (X-Y);    printf("The Result is=%d", result);    return 0; }The snapshot of C ... Read More

Why array index starts from zero in C/C++ ?

Ankith Reddy
Updated on 26-Jun-2020 14:18:25

6K+ Views

An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0 index element. So *(arr+i) means the element at i distance from the first element of the array. So array index starts from 0 as initially i is 0 which means the first element of the array.A program that demonstrates this in C++ is as follows.Example Live Demo#include using namespace std; int main() {    int arr[] = {5,8,9,3,5};    int i;    for(i = 0; i

Why do we assume strncpy insecure in C/C++?

Ankith Reddy
Updated on 26-Jun-2020 14:04:29

468 Views

The function strncpy() is used to copy the specified number of characters to the destination from the source.The following is the syntax of strncpy()char *strncpy( char *destination, char *source, size_t n);Here, destination is the pointer to the destination array where the source string is to be copied, source is the string is to be copied and n is the maximum number of characters to be copied from the source string.The strncpy() function is insecure because if the NULL character is not available in the first n characters in the source string then the destination string will not be NULL terminated.A ... Read More

Accessing array out of bounds in C/C++

Ankith Reddy
Updated on 26-Jun-2020 14:08:50

1K+ Views

In a language such as Java, an exception such as java.lang.ArrayIndexOutOfBoundsException may occur if an array is accessed out of bounds. But there is no such functionality in C and undefined behaviour may occur if an array is accessed out of bounds.A program that demonstrates this in C is given as follows.Example Live Demo#include int main() {    int arr[] = {1,2,3,4,5};    printf("The elements of array : ");    for(int i = 0; i

Advertisements