#include<stdio.h>// Main Functionintmain(){// Variable Declarationinta=10;// Integerfloatb=20.5;// Floatcharc='A';// Charactercharstr[]="Hello World";// Stringintd=1;// Boolean (no native bool type, using int)// Outputprintf("Hello, World!\n");return0;}
Operators
// Arithmeticintsum=1+2;// Additionintdiff=5-3;// Subtractionintprod=2*3;// Multiplicationfloatquot=10/2;// Divisionintmod=10%3;// Modulus// Assignmentintx=10;x+=5;// x = x + 5x-=3;// x = x - 3x*=2;// x = x * 2x/=2;// x = x / 2// ComparisonintisEqual=(1==1);// trueintisNotEqual=(1!=2);// trueintisGreater=(5>3);// trueintisLesser=(3<5);// true
Control Structures
// Conditional Statementsif(a>b){// code if true}elseif(a==b){// code if equal}else{// code if false}// Switch Statementswitch(a){case1:// code for case 1break;case2:// code for case 2break;default:// default code}// Loops// For Loopfor(inti=0;i<5;i++){// code}// While Loopwhile(a<10){// codea++;}// Do-While Loopdo{// codea++;}while(a<10);
Functions
// Function Definitionintadd(intx,inty){returnx+y;}// Function Callintresult=add(2,3);
Arrays
intarr[3]={1,2,3};// Access ElementsintfirstElement=arr[0];// Loop through Arrayfor(inti=0;i<3;i++){printf("%d\n",arr[i]);}
#include<stdio.h>// Write to FileFILE*outFile=fopen("file.txt","w");if(outFile!=NULL){fprintf(outFile,"Hello, World!\n");fclose(outFile);}// Read from FileFILE*inFile=fopen("file.txt","r");if(inFile!=NULL){charcontent[100];while(fgets(content,100,inFile)!=NULL){printf("%s",content);}fclose(inFile);}