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.
#include<stdio.h> int main() { printf("Hello World"); return 0; }
#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; }
#include<stdio.h> int main() { char ch; printf("Enter any character:"); scanf("%c", &ch); printf("ASCII value of %c:%d",ch,ch); return 0; }
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.
#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; }
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.
#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; }
#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;"); }
#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; }
#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; }