echo '' ;

Category Archives: C Examples

C Example – Little Endian and Big Endian


Little and Big Endian are two different byte orderings used to represent multibyte data types, such as integers, in computer memory.

Pre-Request to read this first: What is Little and Big Endian in System

The choice between Both is crucial when data is transferred between systems with different byte orderings. Network protocols and file formats often specify the byte order to ensure compatibility between systems. Many modern architectures, especially those based on x86 and x86-64, use Little-Endian by default.

Read more… →

C Example – Swapping

C Example -In programming, swapping values is a fundamental operation that can find use in various applications across different domains.

Specific use cases-1 of C Example – Swapping

Database Management:

  • Reordering Data: You can use swapping rows in a database table to change the order of records based on certain criteria, such as alphabetical order or chronological order.
  • Optimization: Swapping rows can sometimes be part of database optimization strategies, like minimizing disk I/O by reorganizing data to reduce fragmentation.

Matrix Operations:

  • Matrix Transposition: Swapping rows with columns is fundamental in transposing a matrix, where the rows become columns and vice versa.
  • Row Reduction: Techniques like Gaussian elimination in linear algebra require swapping rows to solve systems of linear equations or find matrix inverses.

Sorting Algorithms:

  • Heap Sort: During the beatification process, swapping rows might be necessary. This process involves reordering elements to satisfy the heap property.
  • Radix Sort: For multidimensional data, swapping entire rows or columns may be part of the sorting process.

Data Analysis and Manipulation:

  • Reorganizing Data: Swapping rows or columns can help in rearranging data for better analysis, such as grouping similar data together or aligning data for comparison.
  • Data Cleaning: In data preprocessing tasks, swapping rows or columns might be necessary to remove duplicates or standardize data formats.

Specific use cases-2 of C Example – Swapping

Graphics and Visualization:

  • Image Processing: Swapping rows or columns of pixel data is common in image processing tasks like rotation, flipping, or resizing images.
  • Data Visualization: In tools like spreadsheets or data visualization software, users may swap rows or columns to customize the presentation of data for better interpretation.

Parallel Processing:

  • Data Partitioning: When distributing data across multiple processing units in parallel computing, swapping rows or columns can be used to redistribute data chunks for load balancing or efficient computation.
  • Data Merging: After parallel processing tasks, swapping rows or columns may be necessary to merge results from different processing units into a single coherent dataset.

Encryption and Compression:

  • Data Obfuscation: Swapping rows or columns can be part of encryption or compression algorithms to scramble data for security or reduce redundancy for compression.

C Program to Swap Two Numbers

Swapping Two Numbers (Folder)


C Program to Swapping Strings (Folder)

C Examples – Addition

In this “C Examples Addition” example, we’ll explore a simple C program that demonstrates addition. Addition is one of the fundamental arithmetic operations, and understanding how to perform it in C can be useful for building more complex programs.
Our program will prompt the user to enter two integers, then it will add them together and display the result. We’ll achieve this using basic input/output functions (printf() and scanf()) for user interaction and a simple loop to perform the addition.
Let’s dive into the code and understand how it works!

Read more… →

C Examples – Time Delay

Time delay is necessary for some cases in programming. for example, if you wanna print some string with some delay or wanna write some value in file frequently with some delay or need to check the excel file value with some delay like many use cases. To make a delay in application execution, we have two options like can write own custom user to define a function as well as can use some pre-defined library functions in C. In many ways, users can write the time delay functions like using for loop, while loop, do-while loop as well as a separate function in all. Here will discuss about C Examples Time Delay.

Read more… →

C Example – Hello World

Here below are many C Example Hello World codes that are available related to Hello World if you have any doubts please post in the comment section. The “Hello World” example in the C programming language is often the first program beginners encounter when learning to code. It’s a simple program that prints “Hello, World!” to the screen. This program serves as a basic introduction to programming syntax and structure in C.

Example: Hello World

This “Hello world” C Example is one of the good in anything startup. Here we are going to print the "ArunEworld" string in the console.

For the below code, you should know the following

Read more… →

C Examples – Bitwise Operators

C Bitwise Operators (Can only be used on integrals, no floats, and doubles)

C BitWise Logic Operator

  • & AND
  • | OR
  • ^ XOR
  • ~ ONE’S COMPLEMENT (Unary Operator)
  •  

& AND

x = 193;            /*1100 0001*/
y = x & 0xf0;        /*1111 0000*/
y = 192;            /*1100 0000*/

| OR

x = 193;            /*1100 0001*/
y = x | 0xf0;        /*1111 0000*/
y = 241;            /*1111 0001*/

^ XOR

x = 193;            /*1100 0001*/
y = x ^ 0xf0;        /*1111 0000*/
y = 62;                /*0011 0001*/

~ ONE’S COMPLEMENT (Unary Operator)

x = 193;            /*1100 0001*/
y = ~x;                /*0011 1110*/
y = 62;                /*0011 1110*

Bit-wise Shift

  • << LEFT SHIFT
    x = 193; /*1100 0001*/
    y = x << 2;
    y = 4; /*0000 0100*/
  • >> RIGHT SHIFT
    x = 193;            /*1100 0001*/
    y = x >> 2;
    y = 48;                /*0011 0000*/
  • Example : A = 0101 1000 >>1 Output : 000010000 .
  • Reference Videos:
    1. https://www.youtube.com/watch?v=d0AwjSpNXR
  1. Special Operators


Exercise in C bitwise operators

1) Which is not a bit-wise operator?

  1. &
  2. |
  3. <<
  4. &&

Ans : 4


2) Predict the output of following program.

#include <stdio.h>
int main()
{
    int a=10;
    int b=2;
    int c;
    
    c=(a & b);
    printf("c= %d",c);
    return 0;
}
  1. c= 12   
  2. c= 10
  3. c= 2
  4. c= 0

Correct answer: 3
c= 2

Bitwise AND (&) operator copies bit(s), if they are exist both of the operands. Here, binary of a is “1010” and binary of b is “0010”. Thus, result of expression (a & b) is “0010” which is equivalent to 2 in Decimal.


3) Predict the output of following program.

#include <stdio.h>

#define MOBILE  0x01
#define LAPPY   0x02

int main()
{
    unsigned char  item=0x00;

    item |=MOBILE;
    item |=LAPPY;

    printf("I have purchased ...:");
    if(item & MOBILE){
        printf("Mobile, ");
    }
    if(item & LAPPY){
        printf("Lappy");
    }

    return 1;
}
  1. I have purchased …:
  2. I have purchased …:Mobile, Lappy
  3. I have purchased …:Mobile,
  4. I have purchased …:Lappy

Correct answer: 2
I have purchased …:Mobile, Lappy

Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is “0001” and binary of Macro LAPPY (0x02) is “0010”, then result of the expression item |=MOBILE; will be “0001” and second expression item |=LAPPY; will return “0011”. Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.


4) Predict the output of following program.

#include <stdio.h>

int main()
{
    char var=0x04;

    var = var | 0x04;
    printf("%d,",var);
    var |= 0x01;
    printf("%d",var);
    
    return 0;
}
  1. 8,9
  2. 4,5   
  3. 8,8
  4. 4,4

Correct answer: 2
4,5

Value of var is 0x04 (0100), Consider the expression var = var | 0x04 The OR (|) of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.


5) Predict the output of following program.

#include <stdio.h>

int main()
{
    char flag=0x0f;

    flag &= ~0x02;
    printf("%d",flag);

    return 0;
}
  1. 13
  2. d
  3. 22
  4. 10

Correct answer: 1
13

Consider the expression flag &= ~0x02 => flag = flag & (~0x02) => flag = 0x0f & (~0x02) => flag = D => flag =13.


6) Consider the given statement:

int x = 10 ^ 2What will be the value of x?

  1. 5
  2. 6
  3. 7
  4. 8

Correct answer: 4
8

XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:

a       b       (a^b)
_______________________

0       0       0
0       1       1
1       0       1
1       1       0

Here, binary of 10 is “1010” and binary of 2 is “0010”, then the result of statement (10 ^ 2) will be “1000”, which is equivalent to 8 in decimal.


7) Predict the output of following program.

#include <stdio.h>

int main()
{
    int x=10;
    
    x &= ~2;
    printf("x= %d",x);
    
    return 0;
}
  1. x= 10
  2. x= 8
  3. x= 12
  4. x= 0

Correct answer: 2
x= 8

The statement x &= ~2; will clear second bit from the value of 10, binary of x is “1010” and the binary of 2 is “0010”, thus this statement will clear second bit and returns “1000” that is equivalent to 8 in Decimal.


8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

  1. Bitwise AND (&)   
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise NOT (~)

Correct answer: 1
Bitwise AND (&)

Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

Consider the following program:

#include <stdio.h>

int main()
{
    int count;
    
    for(count=1; count<=10; count+=1)
        if(count & 1)
            printf("%2d is ODD number\n",count);
        else
            printf("%2d is EVEN number\n",count);
    
    return 0;
}

Output

 1 is ODD number
 2 is EVEN number
 3 is ODD number
 4 is EVEN number
 5 is ODD number
 6 is EVEN number
 7 is ODD number
 8 is EVEN number
 9 is ODD number
10 is EVEN number

9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

  1. (num & (1<<3))
  2. (num & 0x08)
  3. (num & 0x03)
  4. Both (1) and (2)

Correct answer: 4
Both (1) and (2)

The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3rd bit of num is High (set) or not.

Consider this program:

#include <stdio.h>

int main()
{
    int num;
    
    printf("Enter an integer number: ");
    scanf("%d",&num);
    
    if(num & (1<<3))
        printf("3rd bit is High (Set)\n");
    else
        printf("3rd bit is Low\n");
        
    return 0;
}

Output

First run:
Enter an integer number: 15 
3rd bit is High (Set) 

Second run:
Enter an integer number: 7
3rd bit is Low

Binary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3rd bit is high and in second case 3rd bit is low. [Count from 0]


10) Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

Choose the correct words…

  1. Multiplication and Division
  2. Division and Multiplication
  3. Multiplication and Remainder
  4. Remainder and Multiplication

Correct answer: 1
Multiplication and Division

Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.

Consider this program:

#include <stdio.h>

int main()
{
    int num;
    
    printf("Enter an integer number: ");
    scanf("%d",&num);
    
    printf("Multiplication by 2 = %d\n", (num<<1));
    printf("Division by 2 = %d\n",(num>>1));
        
    return 0;
}

Output

Enter an integer number: 100
Multiplication by 2 = 200 
Division by 2 = 50


NEXT of C Bitwise Operators

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example