KEMBAR78
C_Tutorial_with_examples | PDF | Integer (Computer Science) | Computer Engineering
0% found this document useful (0 votes)
27 views14 pages

C_Tutorial_with_examples

Real life examples for C

Uploaded by

arunsrl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

C_Tutorial_with_examples

Real life examples for C

Uploaded by

arunsrl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

#include <stdio.

h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

/* This is a basic program


This program is printing
Hello World
*/
int main()
{
printf("Hello, World!\n");
return 0;
}

/* Basic data type and variable declaration


Variable cannot start with number, $ or
have spaces or C function/keywords
*/
int main()
{
int age = 29;
float pi = 3.14;
char my_char = 'a';
double large_pi = 3.1415926535897;
printf("The age is = %d\n", age);
printf("The value of pi is = %f\n", pi);
printf("The char is = %c\n", my_char);
printf("The value of large_pi is = %lf\n", large_pi);
return 0;
}

/* Multiplicative and Additive Operators


+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Integer Division)
% (Modulus)
*/
int main()
{
int a = 9, b = 2;
float div_res = a / b;
float mod_res = a % b;
printf("The sum is = %d\n", a + b);
printf("The sub is = %d\n", a - b);
printf("The prod is = %d\n", a * b);
printf("The int div is = %f\n", div_res);
printf("The mod is = %f\n", mod_res);
return 0;
}

/* If-Else, Nested if-else statements */


int main()
{
int age;
printf ("Please enter the age:\n");
scanf("%d", &age);
if (age > 18)
{
if (age < 24)
{
printf("The age is greater than 18 but less than 24\n");
}
else
{
printf("The age is greater than 24");
}
}
else if (age == 18)
{
printf("The age is equal to 18\n");
}
else
{
printf("The age is less than 18\n");
}
return 0;
}

/* Ternary Opertor */
int main()
{
int a = 19, b = 21;
int c;
c = (a > b) ? a : b;
printf("The greater of %d and %d is %d\n", a, b, c);
return 0;
}

/* Switch Case */
int main()
{
int marks = 60;
switch (marks)
{
case 95:
printf ("Excellent\n");
case 90:
printf ("Excellent\n");
break;
case 80:
printf ("Very Good\n");
break;
case 60:
printf ("Good\n");
break;
case 40:
printf ("Ok\n");
break;
case 30:
printf ("Not Ok\n");
break;
default:
printf ("Grade Unavailable\n");
break;
}
return 0;
}

/* (Do) While Loop */


int main()
{
int i = 0, j = 0;

while (i > 5)
{
printf ("The value of i = %d\n", i);
i++;
}

printf ("\n*******\n\n");

do
{
printf ("The value of j = %d\n", j);
j++;
}
while (j > 5);

return 0;
}

/* For Loop */
int main()
{
for (int i = 0; i < 5; i++)
{
printf ("Value of i = %d\n", i);
}
return 0;
}

/* Functions */
void myFunction()
{
printf ("Inside the function: myFunction\n");
}

int main()
{
myFunction();
return 0;
}

/* Prototype and declare Functions later */


void myFunction();

int main()
{
myFunction();
return 0;
}

void myFunction()
{
printf ("Inside the function: myFunction\n");
}

/* Passing Parameters/Arguements in Function and calling multiple times */


void sum();

int main()
{
sum(50, 60);
sum(1123, 4545);
sum(45, 3214);
return 0;
}

void sum(int a, int b)


{
int sum = a + b;
printf ("Inside the function: Sum is %d\n", sum);
}

/* Passing Parameters/Arguements in Function using variables */


void sum();
int main()
{
int x, y;
printf ("Enter the num 1\n");
scanf ("%d", &x);
printf ("Enter the num 2\n");
scanf ("%d", &y);
sum(x, y);
return 0;
}

void sum(int a, int b)


{
int sum = a + b;
printf ("Inside the function: Sum is %d\n", sum);
}

/* Return values in Function */


int prod();

int main()
{
int x = 10, y = 23;
int product = prod (x, y);
printf ("The prod of %d and %d is %d\n", x, y, product);
return 0;
}

int prod(int a, int b)


{
return (a * b);
}

/* Scope of Variables */
int g; // Global variable
// Int is already initialized to zero
// Char is initialized to Null Character
// Pointer is initialized to Null Pointer

int NumPrint()
{
printf ("Global variable = %d\n", g);
}

int main()
{
int a; // Not initialized, takes garbage value
NumPrint();
printf ("Local variable = %d\n", a);
return 0;
}

/* Arrays */
int main()
{
int myNumArray [3] = {20, 30, 50};
char myCharArray [3] = {'a', 'b', 'c'};

int numArrayElement_1 = myNumArray[1];


printf ("Value of Element at index 1 of myNumArray is %d\n", numArrayElement_1);

myNumArray[1] = 45;

for (int i = 0; i < 3; i ++)


{
printf ("Element %d of myNumArray = %d\n", i, myNumArray[i]);
}

for (int i = 0; i < 3; i ++)


{
printf ("Element %d of myCharArray = %c\n", i, myCharArray[i]);
}

return 0;
}

/* Multi Dimensional Arrays */


int main()
{
int myNumArray [2] [3] = {
{10, 30, 50}, // row 0
{40, 60, 80} // row 1
// column 0 column 1 column 2
};

for (int i = 0; i < 2; i ++)


{
for (int j = 0; j < 3; j ++)
{
printf ("myCharArray[%d][%d] = %d\n", i,j, myNumArray[i][j]);
}
}

return 0;
}

/* Passing arrays to functions */


int ArraySum (int myArray [], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += myArray[i];
}
return sum;
}

int main()
{
int myNumArray [6] = {10, 30, 50, 40, 60, 80};
printf ("Sum of all elements of array = %d", ArraySum(myNumArray, 6));
return 0;
}

/* Pointers Basic */
int main()
{
int var = 30;
int *ptr;
ptr = &var;
printf ("**** Using Direct Value ****\n");
printf ("Address of var = %x\n", &var);
printf ("Value of var = %d\n", var);

printf ("**** Using Pointer ****\n");


printf ("Address of var = %x\n", ptr);
printf ("Value of var = %d\n", *ptr); // dereferencing
printf ("Address of pointer = %x\n", &ptr);
return 0;
}

/* Pointers Array */
int main()
{
int array [6] = {10, 30, 50, 40, 60, 80};
int *ptr[6];

for (int i = 0; i < 6; i++)


{
ptr[i] = &array[i];
}

for (int i = 0; i < 6; i++)


{
printf ("Value of array[%d] is %d\n", i, *ptr[i]);
}
return 0;
}

/* Use single pointer to point array */


int main()
{
int array [6] = {10, 30, 50, 40, 60, 80};
int *ptr;
ptr = array;

for (int i = 0; i < 6; i++)


{
printf ("Value of array[%d] is %d\n", i, *(ptr+i));
}
return 0;
}

/* Multi-D array (self pointer) */


int main()
{
int myNumArray [2] [3] = {
{10, 30, 50}, // row 0
{40, 60, 80} // row 1
// column 0 column 1 column 2
};

for (int i = 0; i < 2; i ++)


{
for (int j = 0; j < 3; j ++)
{
printf ("myCharArray[%d][%d] = %d\n", i,j, *(*(myNumArray + i) +j));
}
}

return 0;
}

/* Passing Variable Pointers to function */


void getValue (int *ptr)
{
*ptr = 1000;
return;
}
int main()
{
int myVar;
getValue(&myVar);
printf ("Value of myVar = %d\n", myVar);
return 0;
}
/* Passing arrays as pointers to functions */
int ArraySum (int *myArray, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += myArray[i];
}
return sum;
}

int main()
{
int myNumArray [6] = {10, 30, 50, 40, 60, 80};
printf ("Sum of all elements of array = %d", ArraySum(myNumArray, 6));
return 0;
}

/* Strings */
int main()
{
char my_str[12] = "Hello";
char my_str1[12] = "World";
char my_str2[12];

strcpy(my_str2, my_str);
strcat(my_str, my_str1);
int length = strlen(my_str2);

printf ("strcpy = %s\n", my_str2);


printf ("strcat = %s\n", my_str);
printf ("strlen = %d\n", length);
return 0;
}

/* Struct - memory = total of properties' size */


struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record1;
record1.id = 1;
strcpy(record1.name, "John");
record1.percentage = 98.40;
printf ("ID = %d\nName = %s\nPercentage = %.2f\n", record1.id, record1.name,
record1.percentage);
return 0;
}

// Another way of declaring struct


struct student
{
int id;
char name[20];
float percentage;
} record1;
int main()
{
record1.id = 1;
strcpy(record1.name, "John");
record1.percentage = 98.40;
printf ("ID = %d\nName = %s\nPercentage = %.2f\n", record1.id, record1.name,
record1.percentage);
return 0;
}

// Union - Properties Share the memory, Memory allocated is the max size among
properties
/* Union */
union student
{
int id;
char myChar;
} ;

int main()
{
union student record1;
record1.id = 10;
record1.myChar = 9;
printf ("ID = %d\n", record1.id);
return 0;
}

// Bubble Sort
int main()
{
int array [5] = {50, 10, 40, 70, 20};
printf ("***** Bubble Sort *****");
printf ("Before: ");
int n = sizeof(array)/ sizeof(int);
for (int i = 0; i < n; i++)
{
printf ("%d ", array[i]);
}
printf ("\n");
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++){
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf ("After: ");
for (int i = 0; i < n; i++)
{
printf ("%d ", array[i]);
}
return 0;
}

// Selection Sort
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

int main()
{
int array [5] = {50, 10, 40, 70, 20};
printf ("***** Selection Sort *****");
printf ("Before: ");
int n = sizeof(array)/ sizeof(int);
for (int i = 0; i <n; i++)
{
printf ("%d ", array[i]);
}
printf ("\n");
for (int i = 0; i < n - 1; i++)
{
int min_id = i;
for (int j = i + 1; j < n; j++){
if (array[min_id] > array[j]){
min_id = j;
}
}
swap (&array[i], &array[min_id]);
}
printf ("After: ");
for (int i = 0; i < n; i++)
{
printf ("%d ", array[i]);
}
return 0;
}

// Insertion Sort
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

int main()
{
int array [5] = {50, 10, 40, 70, 20};
printf ("Before: ");
int n = sizeof(array)/ sizeof(int);
for (int i = 0; i <n; i++)
{
printf ("%d ", array[i]);
}
printf ("\n");
for (int i = 1; i < n ; i++)
{
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key)
{
array[j+1] = array[j];
j--;
}
array[j+1] = key;
}
printf ("After: ");
for (int i = 0; i < n; i++)
{
printf ("%d ", array[i]);
}
return 0;
}

// Malloc - Allocates memory but does not iniatilize


int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);

ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements of array: ");


for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);
return 0;
}

// Calloc - Allocates and Initializes memory


int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: \n");
scanf("%d", &num);

ptr = (int*) calloc(num, sizeof(int));


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements of array: \n");


for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);
return 0;
}

// Realloc - If allocated not sufficient or new size needed, reallocates memory


int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);

ptr = (int*) malloc(n1 * sizeof(int));

printf("Address of previously allocated memory: ");


for(i = 0; i < n1; ++i)
printf("%u\t",ptr + i);

printf("\nEnter new size of array: ");


scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; ++i)
printf("%u\t", ptr + i);
return 0;
}

You might also like