SlideShare uma empresa Scribd logo
1 de 14
Structure of C Program
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
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;
}
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
}
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;
}
#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;
}
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);
}
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)
{
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;
}
Structure
struct person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct person person1, person2, person3[20];
return 0;
}
Difference between while & do
statement
do {
printf("Word length... ");
scanf("%d", &wdlen);
} while(wdlen<2);
while(wdlen<2){
printf("Word length... ");
scanf("%d", &wdlen);
}
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;
}
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;
}
•
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
}
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;
}

Mais conteúdo relacionado

Semelhante a C programm.pptx

presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
I am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdfI am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdf
fantoosh1
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
A10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptxA10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptx
ranjangamer007
 

Semelhante a C programm.pptx (20)

EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
7 functions
7  functions7  functions
7 functions
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
I am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdfI am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdf
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
A10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptxA10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptx
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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; }
  • 9. Structure struct person { char name[50]; int citNo; float salary; }; int main() { struct person person1, person2, person3[20]; 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; }