C Introduction Examples

C Flow Control?

In this tutorial, you will learn about if statement (including if...else and nested if..else) in C programming with the help of examples.

C Functions?

In this tutorial, you will be introduced to functions (both user-defined and standard library functions) in C programming. Also, you will learn why functions are used in programming.

C Programming Arrays?

In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access elements of an array with the help of examples.

C Programming Pointers?

In this tutorial, you'll learn about pointers; what pointers are, how do you use them and the common mistakes you might face when working with them with the help of examples.

C Programming Strings?

In this tutorial, you'll learn about strings in C programming. You'll learn to declare them, initialize them and use them for various I/O operations with the help of examples.

Structure And Union?

In this tutorial, you'll learn about struct types in C Programming. You will learn to define and use structures with the help of examples.

C Programming Files?

In this tutorial, you will learn about file handling in C. You will learn to handle standard I/O in C using fprintf(), fscanf(), fread(), fwrite(), fseek() etc. with the help of examples.

Additional Topics?

In this tutorial, you will learn about enum (enumeration) in C programming with the help of examples.
C Introduction Examples

In this article, you will find a list of simple C programs such as: displaying a line, adding two numbers, find ASCII value of a character, etc.

 

We have learned about the following topics so far:

  1. Variables and Constants
  2. Data Types
  3. Input and Output in C programming
  4. Operators

To understand these topics better, we have created some examples.

Before you go through these examples, we suggest you to try creating these programs on our own.

We understand that programming can by start If you are just a programming newbie. In that case, go through each example below and see if you can understand them. Once you do that, try writing these programs on your own.


Examples

 

C “Hello, World!” Program

In this example, you will learn to print “Hello, World!” on the screen in C programming.

 

To understand this example, you should have the knowledge of the following C programming topics:


Program to Display “Hello, World!”

#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

Output

Hello, World!

How “Hello, World!” program works?

  • The #include is a preprocessor command that tells the compiler to include the contents of stdio.h (standard input and output) file in the program.
  • The stdio.h file contains functions such as scanf() and printf() to take input and display output respectively.
  • If you use the printf() function without writing #include <stdio.h>, the program will not compile.
  • The execution of a C program starts from the main() function.
  • printf() is a library function to send formatted output to the screen. In this program, printf() displays Hello, World! text on the screen.
  • The return 0; statement is the “Exit status” of the program. In simple terms, the program ends with this statement.

C Program to Print an Integer (Entered by the User)

In this example, the integer entered by the user is stored in a variable and printed on the screen.

 

To understand this example, you should have the knowledge of the following C programming topics:


Program to Print an Integer

#include <stdio.h>
int main() {   
    int number;
   
    printf("Enter an integer: ");  
    
    // reads and stores input
    scanf("%d", &number);

    // displays output
    printf("You entered: %d", number);
    
    return 0;
}

Output

Enter an integer: 25
You entered: 25

In this program, an integer variable number is declared.

int number;

 

 
 

Then, the user is asked to enter an integer number. This number is stored in the number variable.

printf("Enter an integer: ");
scanf("%d", &number);

Finally, the value stored in number is displayed on the screen using printf().

printf("You entered: %d", number);


C Program to Add Two Integers

In this example, the user is asked to enter two integers. Then, the sum of these two integers is calculated and displayed on the screen.

 

To understand this example, you should have the knowledge of the following C programming topics:


Program to Add Two Integers

#include <stdio.h>
int main(){    

    int number1, number2, sum;
    
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // calculating sum
    sum = number1 + number2;      
    
    printf("%d + %d = %d", number1, number2, sum);
    return 0;
}

Output

Enter two integers: 12
11
12 + 11 = 23

In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively.

printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

Then, these two numbers are added using the + operator, and the result is stored in the sum variable.

sum = number1 + number2;

 

 
 
Adding two integers in C programming

C Program to Find ASCII Value of a Character

In this example, you will learn how to find the ASCII value of a character.

 

To understand this example, you should have the knowledge of the following C programming topics:


In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character.

For example, the ASCII value of 'A' is 65.

What this means is that, if you assign 'A' to a character variable, 65 is stored in the variable rather than 'A' itself.

Now, let’s see how we can print the ASCII value of characters in C programming.


Program to Print ASCII Value

#include <stdio.h>
int main() {  
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);  
    
    // %d displays the integer value of a character
    // %c displays the actual character
    printf("ASCII value of %c = %d", c, c);
    
    return 0;
}

 

 
 

Output

Enter a character: G
ASCII value of G = 71

In this program, the user is asked to enter a character. The character is stored in variable c.

When %d format string is used, 71 (the ASCII value of G) is displayed.

When %c format string is used, 'G' itself is displayed.

Finally, the printf() function is used to display the sum of numbers.

printf("%d + %d = %d", number1, number2, sum);

C Program to Demonstrate the Working of Keyword long

In this example, you will learn to demonstrate the working of the long keyword.

 

To understand this example, you should have the knowledge of the following C programming topics:


Program Using the long keyword

#include <stdio.h>
int main(){
    int a;
    long b;   // equivalent to long int b;
    long long c;  // equivalent to long long int c;
    double e;
    long double f;

    printf("Size of int = %zu bytes \n", sizeof(a));
    printf("Size of long int = %zu bytes\n", sizeof(b));
    printf("Size of long long int = %zu bytes\n", sizeof(c));
    printf("Size of double = %zu bytes\n", sizeof(e));
    printf("Size of long double = %zu bytes\n", sizeof(f));
    
    return 0;
}

Output

Size of int = 4 bytes 
Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes

In this program, the sizeof operator is used to find the size of intlonglong longdouble and long double variables.

As you can see, the size of long int and long double variables are larger than int and double variables, respectively.

By the way, the sizeof operator returns size_t (unsigned integral type).

The size_t data type is used to represent the size of an object. The format specifier used for size_t is %zu.

Note: The long keyword cannot be used with float and char types.

C Program to Swap Two Numbers

In this example, you will learn to swap two numbers in C programming using two different techniques.

 

To understand this example, you should have the knowledge of the following C programming topics:


Swap Numbers Using Temporary Variable

#include<stdio.h>
int main() {
      double first, second, temp;
      printf("Enter first number: ");
      scanf("%lf", &first);
      printf("Enter second number: ");
      scanf("%lf", &second);

      // Value of first is assigned to temp
      temp = first;

      // Value of second is assigned to first
      first = second;

      // Value of temp (initial value of first) is assigned to second
      second = temp;

      printf("\nAfter swapping, firstNumber = %.2lf\n", first);
      printf("After swapping, secondNumber = %.2lf", second);
      return 0;
}

Output

Enter first number: 1.20
Enter second number: 2.45

After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20

In the above program, the temp variable is assigned the value of the first variable.

Then, the value of the first variable is assigned to the second variable.

 

 
 

Finally, the temp (which holds the initial value of first) is assigned to second. This completes the swapping process.


Swap Numbers Without Using Temporary Variables

#include <stdio.h>
int main() {
    double a, b;
    printf("Enter a: ");
    scanf("%lf", &a);
    printf("Enter b: ");
    scanf("%lf", &b);

    // Swapping

    // a = (initial_a - initial_b)
    a = a - b;   
 
    // b = (initial_a - initial_b) + initial_b = initial_a
    b = a + b;

    // a = initial_a - (initial_a - initial_b) = initial_b
    a = b - a;

    printf("After swapping, a = %.2lf\n", a);
    printf("After swapping, b = %.2lf", b);
    return 0;
}

Output

Enter a: 10.25
Enter b: -12.5
After swapping, a = -12.50
After swapping, b = 10.25