Basic Syntax
#include <stdio.h>
int main() {
//code goes here
return 0;
}
printf
printf("Hello World!)
scanf
scanf("formatSpecifier", &varName)
//for int
scanf("%d", &varName)
//for float
scanf("%f", &varName)
//for string
scanf("%s", &varName)
//for single character
scanf("%c", &varName)
Comments
//this is a single lined comment.
/*this is a
multi-lined comment*/
Escape Sequences
//escape sequences
\n //adds a new line
\t //adds four spaces
\b //backspace
\a //beep sound
\f //form feed
\r //carriage return
\\ //adds backslash
\' //adds single quote
\? //adds question mark
\0 //null character
Variables
int sum = 10;
Data Types
int sum = 10; //stores integer
float average = 90.33; //stores numbers with decimal
double percentage = 80.3452; //stores numbers with decimal but precision is double as compared to float
char letter = 'A'; //stores single character
bool isTrue = true; //stores true or false
string intro = "My name is Sajeel"; //stores sequence of characters
void myFunc();//it represent the absence of data type. Mostly used with functions and pointers
String and its methods
//declaring string
char fname[] = "John";
char lname[] = "Doe";
//concatenating strings
char name[100];
strcpy(name, fname);
strcat(name, lname);
//finding length of string
#include <string.h>
size_t length = strlen(myString);
printf("Length: %zu\n", length);
//accessing characters of string
printf("%c\n", myString[0]);
//Changing characters
myString[0] = 'A'; // Change the first character to 'A'
printf("%c\n", myString[0]);
//taking string input ignoring spaces
char myString[100];
scanf("%s", myString)
Operators
Arithmetic Operators
+ //addition
- //subtraction
* //multiplication
/ //division
% //modulus
++ //increment
-- //decrement
Assignment Operators
= //assign value
+= //add and assign
-= //subtract and assign
/= //divide and assign
*= //multiply and assign
%= //taking modulus and assign
Comparison Operators
== //checks equality
!= //checks unequality
> //checks greater than
>= //checks greater than or equal to
< //check less than
<= //checks less than or equal to
Logical Operators
&& //returns true if all are expressions true
|| //returns true if only one expression is true
! //returns true if false and vice versa
Bitwise Operators
& //bitwise AND
| //bitwise OR
~ //bitwise NOT
>> //shift right
<< //shift left
Conditional statements
//if statement
if(condition){
//code
}
//if-else statement
if(condition){
//code
}
else{
//code
}
//else-if statement
if(condition){
//code
}
else if(condition){
//code
}
else{
//code
}
//goto statement
int main(){
//code
label:
// code
goto label;
// code
}
//Ternary Operator
variable = condition ? ifTrue : ifFalse;
Switch Statements
switch(expression){
case const-exp:
//code
break;
case const-exp:
//code
break;
.
.
.
default:
//code
break;
}
Loops
//for Loop:
for( initialization ; condition ; change-variable-value ){
//code
}
//while Loop:
initialize variable
while(condition){
//code
//change-variable-value
}
//do-while Loop:
initialize variable
do{
//code
//change-variable-value
}while(condition);
Continue And Break Statements
//break statement
for( initialization ; condition ; change-variable-value ){
if(condition){
break; //moves out of the loop when condition is true
}
}
//continue statement
for( initialization ; condition ; change-variable-value ){
if(condition){
continue; //skip the iteration when condition is true
}
}
Arrays
//initializing arrays
int arrayName[size] = {data1, data2,.....}
//printing array
for(int i=0 ; i < ( sizeof(array)/ sizeof(int) ) ; i++){
printf("%d\n", array[i]);
}
//taking input in array
for(int i=0 ; i < ( sizeof(array)/ sizeof(int) ) ; i++){
printf("Enter data at %d index ", i+1);
scanf("%d",&array[i]);
}
//changing array elements at specified index
int myArray [3] = {1, 2, 3};
myArray[0] = 5; //now array has 5 at first index
Structures and Unions
//initializing struct
struct struct_name{
//data members and member functions
};
//initializing union
union union_name{
//data members
};
Pointers
int var = 10;
//initialization of pointer
int *ptr = &var;
// &var is the reference to variable named var
Functions
//declaring a function
returnType functionName(dataType parameters){
//code
}
//calling a function
functionName(arguments);
//recursion
returnType functionName(dataType parameters){
//code
functionName(arguments);
//code
}
//Function Overloading
int sum(int a, int b){
return a+b;
}
float sum(float a, float b, int c){
return a+b;
}
int main(){
printf("%d", sum(5, 10));
printf("%f", sum(5.0, 10.0, 20));
}
Built-in Functions
// Input and Output functions
printf() //used for formatted output
scanf() //used for formatted input
getchar() //used for character input
putchar() //used for character output
fgets() //used for reading strings
fputs() //used for writing stings
//string functions
strlen(string);
strcmp(string1 , string2 );
strncmp (string1, string2, n);
strcpy(string1, string2);
strncpy(string1, srting2, n);
strcat(string1,string2);
strncat(string1, string2, n);
strlwr(string_name);
strupr(string_name);
strrev(string_name);
char *strchr(const char *str, int character);
char *strrchr(const char *str, int character);
//math functions
pow(x , y);
sqrt(x);
floor(x);
ceil(x);
round(x);
fmod(x, y);
cos(x);
sin(x);
tan(x);
log(x);
log10(x);
//time and date functions
#include <stdio.h>
#include <time.h>
int main() {
time_t start_time, end_time;
time(&start_time);
// Perform some task or wait
time(&end_time);
double difference = difftime(end_time, start_time);
printf("Time elapsed: %.2f seconds\n", difference);
return 0;
}
Exceptions
try {
// here goes the code and if there is an error throw exception
throw exception;
}
catch () {
// Block of code to handle errors
}
File Handling
//file pointer
FILE* filePointer
//Opening files
FILE* file = fopen("example.txt", "w"); //opened in write mode
//reading from files
int character = fgetc(file); // Read a character
char buffer[100];
fgets(buffer, sizeof(buffer), file); // Read a line
fscanf(file, "%d", &some_variable); // Read using fscanf
//writing to files
fputc('A', file); // Write a character
fputs("Hello, World!", file); // Write a string
fprintf(file, "Formatted data: %d", some_variable); // Write formatted data
//closing files
fclose(file);
Memory Allocation
// Allocate memory for an integer
int* intPtr = (int*)malloc(sizeof(int));
// Allocate memory for an array of integers
int* intArray = (int*)calloc(10, sizeof(int));
//deallocate memory
free(intPtr);
free(intArray);
//reallocate memory using realloc
int* resizedArray = (int*)realloc(intArray, 20 * sizeof(int));
if (resizedArray != NULL) {
intArray = resizedArray; // Update the pointer
}