The document provides code snippets demonstrating different programming concepts in C including:
1) Structures, if statements, switch statements, while loops, do-while loops, functions for sorting numbers in ascending order, matrix multiplication, pointers, and error handling.
2) It includes examples of basic syntax for these concepts like defining a structure, if/else conditions, looping, passing arguments to functions, and accessing memory addresses with pointers.
3) The error handling section shows how to check for errors with errno values after operations like opening files that do not exist.
This PowerPoint helps students to consider the concept of infinity.
C programm.pptx
1. Structure of C Program
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
2. If Statement
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Test expression is true if number is less than 0
if (number < 0)
{
printf("You entered %d.n", number);
}
printf("The if statement is easy.");
return 0;
}
3. Switch
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
4. While
// Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n
#include <stdio.h>
int main()
{
int number;
long long factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
// loop terminates when number is less than or equal to 0
while (number > 0)
{
factorial *= number; // factorial = factorial*number;
--number;
}
printf("Factorial= %lld", factorial);
return 0;
}
5. #include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %dn", a);
a = a + 1;
}while( a < 20 );
return 0;
}
6. Ascending Order
/*
* C program to accept numbers as an input from user
* and to sort them in ascending order.
*/
#include <stdio.h>
void sort_numbers_ascending(int number[], int count)
{
int temp, i, j, k;
for (j = 0; j < count; ++j)
{
for (k = j + 1; k < count; ++k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
printf("Numbers in ascending order:n");
for (i = 0; i < count; ++i)
printf("%dn", number[i]);
}
void main()
{
int i, count, number[20];
printf("How many numbers you are gonna enter:");
scanf("%d", &count);
printf("nEnter the numbers one by one:");
for (i = 0; i < count; ++i)
scanf("%d", &number[i]);
sort_numbers_ascending(number, count);
}
7. Matrix Multiplication
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.nn");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("nEnter elements of matrix 1:n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
8. printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("nEnter elements of matrix 2:n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("nOutput Matrix:n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("nn");
}
return 0;
}
10. Difference between while & do
statement
do {
printf("Word length... ");
scanf("%d", &wdlen);
} while(wdlen<2);
while(wdlen<2){
printf("Word length... ");
scanf("%d", &wdlen);
}
11. Understanding Pointers
#include <stdio.h>
int main()
{
int num = 10;
printf("Value of variable num is: %d", num);
/* To print the address of a variable we use %p
* format specifier and ampersand (&) sign just
* before the variable name like &num.
*/
printf("nAddress of variable num is: %p", &num);
return 0;
}
12. Declaring, Initializing and using a
pointer variable
int *ip // pointer to integer variable
float *fp; // pointer to float variable
double *dp; // pointer to double variable
char *cp; // pointer to char variable
#include<stdio.h>
void main()
{
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
}
#include <stdio.h>
int main()
{
int i = 10; // normal integer variable storing value 10
int *a; // since '*' is used, hence its a pointer variable
/*
'&' returns the address of the variable 'i'
which is stored in the pointer variable 'a'
*/
a = &i;
/*
below, address of variable 'i', which is stored
by a pointer variable 'a' is displayed
*/
printf("Address of variable i is %un", a);
/*
below, '*a' is read as 'value at a'
which is 10
*/
printf("Value at the address, which is stored by pointer variable a is %dn", *a);
return 0;
}
•
13. List of Errors
Syntax of a basic construct is written wrong. For example : while loop
// C program to illustrate
// syntax error
#include<stdio.h>
int main(void)
{
// while() cannot contain "." as an argument.
while(.)
{
printf("hello");
}
return 0;
}
// C program to illustrate
// syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
14. Error Handling Mechanism
// C implementation to see how errno value is
// set in the case of any error in C
#include <stdio.h>
#include <errno.h>
int main()
{
// If a file is opened which does not exist,
// then it will be an error and corresponding
// errno value will be set
FILE * fp;
// opening a file which does
// not exist.
fp = fopen("GeeksForGeeks.txt", "r");
printf(" Value of errno: %dn ", errno);
return 0;
}