C BASICS

The best way to learn C programming is by practicing examples. The page contains examples on basic concepts of C programming. You are advised to take the references from these examples and try them on your own. All the programs on this page are tested and should work on all platforms.

Example 1: Displaying Hello World

#include<stdio.h>
int main()
    {
    printf("Hello World");
    return 0;
    }
 

Example 2:C Program to Multiply two Floating Point Numbers

#include<stdio.h>
int main(){
    float num1, num2, product;
    printf("Enter first Number: ");
    scanf("%f", &num1);
    printf("Enter second Number: ");
    scanf("%f", &num2);
    //Multiply num1 and num2
    product = num1 * num2;
    // Displaying result up to 3 decimal places. 
    printf("Product of nums is:%.3f",product);
    return 0;
    }

Example 3: C Program to Find ASCII Value of a Character

#include<stdio.h>
int main()
    {
    char ch;
    printf("Enter any character:");

    scanf("%c", &ch);
    printf("ASCII value of %c:%d",ch,ch);
    return 0;
        }

C enums

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Example:ENUM

#include<stdio.h>
enum week {Sun,Mon,Tue,Wed,Thu,Fri,Sat};
int main()
    {
    // creating today variable of enum week type
    enum week today;
    today = Wed;
    printf("Day %d",today+1);
    return 0;
    }
            

C if...else Statement

The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.

There are the following variants of if statement in C language.

Example 1:The if..else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition doesn’t meet then display a different message “You are not eligible for voting”.

#include <stdio.h>
int main()
{
    int age;
    printf("Enter your age:");
    scanf("%d",&age);
    if(age >=18)
    {
    printf("You are eligible for voting");
    }
    else
    {
    printf("You are not eligible for voting");
    }
    return 0;
    }
            

Example 2:ROOTS OF QUADRATIC EQUATIONS

#include <stdio.h>
#include <math.h>

void main()
{
    int a,b,c,d;
    float x1,x2;
 
    printf("Input the value of a,b & c : ");
    scanf("%d%d%d",&a,&b,&c);
    d=b*b-4*a*c;
    if(d==0)
    {
    printf("Both roots are equal.\n");
    x1=-b/(2.0*a);
    x2=x1;
    printf("First  Root Root1= %f\n",x1);
    printf("Second Root Root2= %f\n",x2);
    }
    else if(d>0)
    {
    printf("Both roots are real and diff-2\n");
    x1=(-b+sqrt(d))/(2*a);
    x2=(-b-sqrt(d))/(2*a);
    printf("First  Root Root1= %f\n",x1);
    printf("Second Root root2= %f\n",x2);
    }
	else
	    printf("Root are imaginary;");
    }

Example 4:C Program to Check Leap Year

#include <stdio.h>
#include <math.h>
int main(){
    
    int year;      
    printf("Enter a year\n");
    scanf("%d", &year);
    if ( year%400 == 0)      
    printf("%d is a leap year.\n", year);  
    else if ( year%100 == 0)      
    printf("%d is not a leap year.\n", year); 
    else if ( year%4 == 0 )      
    printf("%d is a leap year.\n", year);
    else      
    printf("%d is not a leap year.\n", year);      
    return 0;    
        }

Example 5:C Program to Check Number is Multiple of 7 or not

#include <stdio.h>
#include <math.h>
int main()
    {
    int number;
    printf("Enter a number: ");
    scanf("%d",&number);
    if(number%7==0){
    printf("%d is multiple of 7.",number);
    }
   else
   {
     printf("%d is not multiple of 7.",number);
   }

   return 0;
 }