0% found this document useful (0 votes)
31 views24 pages

Presentation 4

The document provides an introduction to the C programming language, covering topics such as what a program is, the history and basics of C, variables and data types, conditional statements like if-else and switch statements, looping statements like for, while and do-while loops, functions, and a simple program to add two numbers.

Uploaded by

RAJ KUMAR M
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)
31 views24 pages

Presentation 4

The document provides an introduction to the C programming language, covering topics such as what a program is, the history and basics of C, variables and data types, conditional statements like if-else and switch statements, looping statements like for, while and do-while loops, functions, and a simple program to add two numbers.

Uploaded by

RAJ KUMAR M
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/ 24

C Programming and

Data Structures
Introduction

 Program/Software

A set of instructions or code written in


a specific programming language that
tells a computer what tasks to
perform
Example programs.....
What is Programming language?

 A programming language is a formalized set of


rules and syntax used to write computer
programs.
 It serves as a means of communication between
humans and computers.
History of c language
 C was developed by Dennis Ritchie at Bell Labs in the early 1970s. It
was created as a successor to the B programming language, which was
developed by Ken Thompson.

 In 1972, Dennis Ritchie built the first C compiler. This compiler


allowed C code to be translated into machine-readable instructions.

 C's simplicity, portability, and efficiency have contributed to its


enduring popularity, and it continues to be an important language in
computer science and software development.
First program
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Hello World!.");
getch();

return 0;
}
C program execution steps...
Variables and Datatypes
Variables
➢ A variable is a symbolic name or identifier used to represent a storage
location in computer memory where data can be stored, manipulated,
and retrieved during the execution of a program.
Datatype

➢ Data types define the type of data that a variable can


hold.
➢ Data types specify the size and format of values that
can be stored in variables
➢ It determines how operations are performed on those
values
Program to add two numbers...
#include <stdio.h>
int main() {
Output??
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Keywords in c
Branching Statements...

Branching statements are used to


change the flow of a program's
execution by making decisions based on
conditions
If-else Statement
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
Else-if ladder Statement
#include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
Switch Statement
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Average.\n");
break;
default:
printf("Needs improvement.\n");
}

return 0; Output??
}
Goto Statement
#include <stdio.h>
int main() {
int num = 5;
if (num == 5) {
goto label;
}
printf("This won't be printed.\n");
label:
printf("This will be printed.\n");
return 0;
}
Looping Statements...

For loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
While loop
#include <stdio.h>
int main() {
int count = 1;
while (count <= 5) {
printf("Iteration %d\n", count);
count++;
}
return 0;
}
Do-While loop
#include <stdio.h>
int main() {
int count = 1;
do {
printf("Iteration %d\n", count);
count++;
} while (count <= 5);

return 0;
}
Functions In c
In C, a function is a self-contained block of
code that performs a specific task. Functions
allow you to modularize your code, making it
more organized, readable, and maintainable
Syntax:
return_type function_name(parameters)//Header
{
// Function body
// Code to perform the task
return return_value; // Optional return statement
}
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 3); // Calling the add function
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
int sum = a + b;
return sum; // Return the sum as the result of the function
}

You might also like