SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
Welcome to Programming World!
C Programming
Topic: Keywords and Identifiers
Keywords are predefined, reserved words used in programming that have a
special meaning. Keywords are part of the syntax and they cannot be used as an
identifier.
For example: int money;
Here, int is a keyword that indicates 'money' is a variable of type integer.
Keywords List:
Samsil Arefin
Identifiers : Identifier refers to name given to entities such as variables,
functions, structures etc.
int money;
double accountBalance;
Here, money and accountBalance are identifiers.
Topic: Constants and Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name
(identifier). Variable names are just the symbolic representation of a memory
location.
For example: int potato=20;
Here potato is a variable of integer type. The variable is assigned value: 20
Constants/Literals :
A constant is a value or an identifier whose value cannot be altered in a program.
For example: const double PI = 3.14
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this
program.
Escape Sequences :
Data Types :
Programming Operators :
Increment and decrement operators:
C programming has two operators increment ++ and decrement -- to change the
value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value
by 1. These two operators are unary operators, meaning they only operate on a
single operand.
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
Output:
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
Ternary Operator (?:) :
conditionalExpression ? expression1 : expression2
The conditional operator works as follows:
1.The first expression conditionalExpression is evaluated at first. This expression
evaluates to 1 if it's and evaluates to 0 if it's false.
2.If conditionalExpression is true, expression1 is evaluated.
3.If conditionalExpression is false, expression2 is evaluated.
For example:
#include<stdio.h>
int main(){
int a=10,b=15;
// If test condition (a >b) is true, max equal to 10.
// If test condition (a>b) is false,max equal to 15.
int max=(a>b)?a:b;
printf("%d",max);
return 0;
}
Topic: if, if...else and Nested if...else Statement
if statement :
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
Samsil Arefin
Example :
#include <stdio.h>
int main () {/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 ) {/* if condition is true then print the following */
printf("a is less than 20n" );
}
printf("value of a is : %dn", a); return 0; }
Output:
a is less than 20
value of a is : 10
If...else if...else Statement :
if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
}
else {
/* executes when the none of the above condition is true */
}
#include <stdio.h>
void main () { int a = 100; /* local variable definition */
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10n" );
}
else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20n" );
}
else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30n" );
}
else {
/* if none of the conditions is true */
printf("None of the values is matchingn" ); } }
Topic: Switch statement
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
}
Samsil Arefin
For example :
#include <stdio.h>
void main () {
char grade = 'B'; /* local variable definition */
switch(grade) {
case 'A' :
printf("Excellent!n" ); break;
case 'B' :
printf("Well donen" ); break;
case 'C' :
printf("You passedn" ); break;
case 'D' :
printf("Better try againn" ); break;
default : printf("Invalid graden" ); } }
Topic : LOOP
There are three loops in C programming:
1.for loop
2.while loop
3.do..while loop
Samsil Arefin
For loop :
Example :
#include <stdio.h>
void main () { int a;
/* for loop execution */
for( a =0; a <10; a++ ){
printf("value of a: %dn", a); } }
Output:
value of a: 0
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
while loop :
while(condition) {
statement(s); }
Example:
#include <stdio.h>
Void main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %dn", a);
a++;
} }
Output same.
DO..while loop :
do {
statement(s);
} while( condition );
Example:
#include <stdio.h>
Void main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %dn", a);
a++;
}while( a < 20 ); }
Output same.
Nested for loop :
for (initialization; condition; increment/decrement)
{
statement(s);
for (initialization; condition; increment/decrement)
{
statement(s);
... ... ...
}
... ... ...
}
Samsil Arefin
#include <stdio.h>
int main()
{
int i,j;
for( i=1;i<=5;i++)
{
for( j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("n");
}
return 0;
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Nested while loop :
#include <stdio.h>
int main()
{
int i=1,j;
while (i <= 5)
{
j=1;
while (j <= i )
{
printf("%d ",j);
j++;
}
printf("n");
i++;
}
return 0;
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Samsil Arefin
break statement :
Example :
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %dn", a);
a++;
if( a > 15) {
/* terminate the loop using break statement */
break;
}
}
return 0;
}
Result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
continue Statement :
Example:
#include <stdio.h>
int main()
{ int i;
for( i=0;i<=8;i++)
{
if(i==5) continue;
printf("value of i: %dn", i);
}
return 0;
}
Output:
value of i: 0
value of i: 1
value of i: 2
value of i: 3
value of i: 4
value of i: 6
value of i: 7
value of i: 8
Topic: Function
Example #1: No arguments passed and no return Value
#include <stdio.h>
void add()
{
int a=10,b=20,c;
c=a+b;
printf("C= %d",c);
// return type of the function is void becuase no value is returned from the
function
}
int main()
{
add(); // no argument is passed to add()
return 0;
}
Output: c= 30
Example #2: No arguments passed but a return value
#include <stdio.h>
int get()
{
int n=10,m=10;
return m+n; //m+n return value
}
void main()
{
int c;
c= get(); // no argument is passed to the function
// the value returned from the function is assigned to c
printf("C= %d",c);
}
Output: C= 20
Example #3: Argument passed but no return value
#include <stdio.h>
// void indicates that no value is returned from the function
void get(int c)
{
printf("C= %d",c) ;
}
int main()
{
int a=10,b=10,c;
c=a+b;
get(c); // c is passed to the function
return 0;
}
Example #4: Argument passed and a return value
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
// a,b are passed to the checkPrimeNumber() function
// the value returned from the function is assigned to ret variable
ret = max(a, b);
printf( "Max value is : %dn", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Pass by Value: In this parameter passing method, values of actual
parameters are copied to function’s formal parameters and the two
types of parameters are stored in different memory locations. So any
changes made inside functions are not reflected in actual parameters
of caller.
#include <stdio.h>
void fun(int x)
{
x = 30;
}
int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
}
Output: x = 20
Pass by Reference Both actual and formal parameters refer to same
locations, so any changes made inside the function are actually reflected in
actual parameters of caller.
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);
return 0;
}
x = 30
Topic: Recursion
A function that calls itself is known as a recursive function. And, this
technique is known as recursion. The recursion continues until some
condition is met to prevent it. To prevent infinite recursion, if...else
statement (or similar approach) can be used where one branch makes the
recursive call and other doesn't
Example: Sum of Natural Numbers Using Recursion
#include <stdio.h>
int sum(int n);
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum=%d", result);
}
int sum(int num)
{
if (num!=0)
return num + sum(num-1); // sum() function calls itself
else
return num;
}
Output : Enter a positive integer: 3
sum=6
Samsil Arefin
Exercise 1)
Write a program and call it calc.c which is the basic calculator and receives
three values from input via keyboard.
The first value as an operator (Op1) should be a char type and one of (+, -, *,
/, s) characters with the following meanings:
o ‘+’ for addition (num1 + num2)
o ‘-’ for subtraction (num1 - num2)
o ‘*’ for multiplication (num1 * num2)
o ‘/’ for division (num1 / num2)
o ‘s’ for swap
*Program should receive another two operands (Num1, Num2) which could
be float or integer
.
*The program should apply the first given operator (Op1) into the operands
(Num1, Num2) and prints the relevant results with related messages in the
screen.
* Swap operator exchanges the content (swap) of two variables, for this task
you are not allowed to use any further variables (You should use just two
variables to swap).
Exercise 2)
Write a C program and call it sortcheck.cpp which receives 10 numbers
from input and checks whether these numbers are in ascending order or
not. You are not allowed to use arrays. You should not define more than
three variables.
e.g Welcome to the program written by Your Name:
Please enter 10 Numbers: 12 17 23 197 246 356 790 876 909 987
Fine, numbers are in ascending order.
Exercise 3)
Write a C program to calculates the following equation for entered numbers
(n, x). 1+ (nx/1!) - (n(n-1)x^2/2!)......................................
Exercise 4)
Write the C program for processing of the students structure. Define the
array of a structure called students including following fields:
* “First name”
* “Family Name”
* “Matriculation Number”
You should first get the number of students from input and ask user to
initialize the fields of the structure for the entered amount of students. Then
delete the students with the same “Matriculation Number” and sort the list
based on “Family Name” and print the final result in the screen.
Samsil Arefin
161-15-7197

Weitere ähnliche Inhalte

Was ist angesagt?

9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
Program flowchart
Program flowchartProgram flowchart
Program flowchartSowri Rajan
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function웅식 전
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programmingPrabhu Govind
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 

Was ist angesagt? (20)

9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Ansi c
Ansi cAnsi c
Ansi c
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Functions
FunctionsFunctions
Functions
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 

Ähnlich wie C programming

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESLeahRachael
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribdAmit Kapoor
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about CYi-Hsiu Hsu
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5Rumman Ansari
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxNithya K
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptxcricketreview
 

Ähnlich wie C programming (20)

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
C Programming
C ProgrammingC Programming
C Programming
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
C important questions
C important questionsC important questions
C important questions
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
Basics of c
Basics of cBasics of c
Basics of c
 
Function in c
Function in cFunction in c
Function in c
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
 
6. function
6. function6. function
6. function
 

Mehr von Samsil Arefin

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolSamsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution PhylogeneticSamsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution PhylogeneticSamsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysisSamsil Arefin
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)Samsil Arefin
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat applicationSamsil Arefin
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting insertingSamsil Arefin
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderSamsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdataSamsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracingSamsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuitSamsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1Samsil Arefin
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 

Mehr von Samsil Arefin (20)

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocol
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
 
Number theory
Number theoryNumber theory
Number theory
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Stack
StackStack
Stack
 
Sorting
SortingSorting
Sorting
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
 
Cyber security
Cyber securityCyber security
Cyber security
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
String
StringString
String
 

Kürzlich hochgeladen

Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxachiever3003
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 

Kürzlich hochgeladen (20)

Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 

C programming

  • 1. Welcome to Programming World! C Programming Topic: Keywords and Identifiers Keywords are predefined, reserved words used in programming that have a special meaning. Keywords are part of the syntax and they cannot be used as an identifier. For example: int money; Here, int is a keyword that indicates 'money' is a variable of type integer. Keywords List: Samsil Arefin
  • 2. Identifiers : Identifier refers to name given to entities such as variables, functions, structures etc. int money; double accountBalance; Here, money and accountBalance are identifiers. Topic: Constants and Variables
  • 3. In programming, a variable is a container (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example: int potato=20; Here potato is a variable of integer type. The variable is assigned value: 20 Constants/Literals : A constant is a value or an identifier whose value cannot be altered in a program. For example: const double PI = 3.14 Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.
  • 6. Programming Operators : Increment and decrement operators: C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
  • 7. #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } Output: ++a = 11 --b = 99 ++c = 11.500000 ++d = 99.500000
  • 8. Ternary Operator (?:) : conditionalExpression ? expression1 : expression2 The conditional operator works as follows: 1.The first expression conditionalExpression is evaluated at first. This expression evaluates to 1 if it's and evaluates to 0 if it's false. 2.If conditionalExpression is true, expression1 is evaluated. 3.If conditionalExpression is false, expression2 is evaluated. For example: #include<stdio.h> int main(){ int a=10,b=15; // If test condition (a >b) is true, max equal to 10. // If test condition (a>b) is false,max equal to 15. int max=(a>b)?a:b; printf("%d",max); return 0; }
  • 9. Topic: if, if...else and Nested if...else Statement if statement : if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } Samsil Arefin
  • 10. Example : #include <stdio.h> int main () {/* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) {/* if condition is true then print the following */ printf("a is less than 20n" ); } printf("value of a is : %dn", a); return 0; } Output: a is less than 20
  • 11. value of a is : 10 If...else if...else Statement : if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } #include <stdio.h> void main () { int a = 100; /* local variable definition */ /* check the boolean condition */ if( a == 10 ) { /* if condition is true then print the following */ printf("Value of a is 10n" ); } else if( a == 20 ) {
  • 12. /* if else if condition is true */ printf("Value of a is 20n" ); } else if( a == 30 ) { /* if else if condition is true */ printf("Value of a is 30n" ); } else { /* if none of the conditions is true */ printf("None of the values is matchingn" ); } } Topic: Switch statement
  • 13. 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 } Samsil Arefin
  • 14.
  • 15. For example : #include <stdio.h> void main () { char grade = 'B'; /* local variable definition */ switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : printf("Well donen" ); break; case 'C' : printf("You passedn" ); break; case 'D' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } } Topic : LOOP There are three loops in C programming: 1.for loop 2.while loop 3.do..while loop Samsil Arefin
  • 16. For loop : Example : #include <stdio.h> void main () { int a; /* for loop execution */ for( a =0; a <10; a++ ){
  • 17. printf("value of a: %dn", a); } } Output: value of a: 0 value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 while loop : while(condition) { statement(s); } Example: #include <stdio.h> Void main () { /* local variable definition */
  • 18. int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; } } Output same. DO..while loop : do { statement(s); } while( condition );
  • 19. Example: #include <stdio.h> Void main () { /* local variable definition */ int a = 10; /* do loop execution */ do { printf("value of a: %dn", a); a++; }while( a < 20 ); }
  • 20. Output same. Nested for loop : for (initialization; condition; increment/decrement) { statement(s); for (initialization; condition; increment/decrement) { statement(s); ... ... ... } ... ... ... }
  • 22. for( i=1;i<=5;i++) { for( j=1;j<=i;j++) { printf("%d ",j); } printf("n"); } return 0; } Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Nested while loop :
  • 23. #include <stdio.h> int main() { int i=1,j; while (i <= 5) { j=1; while (j <= i ) { printf("%d ",j); j++;
  • 24. } printf("n"); i++; } return 0; } Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Samsil Arefin
  • 25. break statement : Example : #include <stdio.h> int main () { /* local variable definition */
  • 26. int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; } Result: value of a: 10 value of a: 11 value of a: 12
  • 27. value of a: 13 value of a: 14 value of a: 15 continue Statement : Example: #include <stdio.h> int main() { int i; for( i=0;i<=8;i++) { if(i==5) continue; printf("value of i: %dn", i); } return 0; } Output: value of i: 0 value of i: 1 value of i: 2 value of i: 3
  • 28. value of i: 4 value of i: 6 value of i: 7 value of i: 8 Topic: Function
  • 29. Example #1: No arguments passed and no return Value #include <stdio.h> void add() { int a=10,b=20,c; c=a+b; printf("C= %d",c);
  • 30. // return type of the function is void becuase no value is returned from the function } int main() { add(); // no argument is passed to add() return 0; } Output: c= 30 Example #2: No arguments passed but a return value #include <stdio.h> int get() { int n=10,m=10; return m+n; //m+n return value } void main() { int c;
  • 31. c= get(); // no argument is passed to the function // the value returned from the function is assigned to c printf("C= %d",c); } Output: C= 20 Example #3: Argument passed but no return value #include <stdio.h> // void indicates that no value is returned from the function void get(int c) { printf("C= %d",c) ; } int main() { int a=10,b=10,c; c=a+b; get(c); // c is passed to the function return 0; }
  • 32. Example #4: Argument passed and a return value #include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; // a,b are passed to the checkPrimeNumber() function // the value returned from the function is assigned to ret variable ret = max(a, b); printf( "Max value is : %dn", ret ); return 0;
  • 33. } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of caller.
  • 34. #include <stdio.h> void fun(int x) { x = 30; } int main(void) { int x = 20; fun(x); printf("x = %d", x); return 0; } Output: x = 20 Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. # include <stdio.h> void fun(int *ptr) { *ptr = 30; } int main() { int x = 20; fun(&x); printf("x = %d", x); return 0; } x = 30
  • 35. Topic: Recursion A function that calls itself is known as a recursive function. And, this technique is known as recursion. The recursion continues until some condition is met to prevent it. To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't
  • 36. Example: Sum of Natural Numbers Using Recursion #include <stdio.h> int sum(int n); int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum=%d", result); } int sum(int num) { if (num!=0) return num + sum(num-1); // sum() function calls itself else return num; } Output : Enter a positive integer: 3 sum=6
  • 38. Exercise 1) Write a program and call it calc.c which is the basic calculator and receives three values from input via keyboard. The first value as an operator (Op1) should be a char type and one of (+, -, *, /, s) characters with the following meanings: o ‘+’ for addition (num1 + num2) o ‘-’ for subtraction (num1 - num2) o ‘*’ for multiplication (num1 * num2) o ‘/’ for division (num1 / num2) o ‘s’ for swap *Program should receive another two operands (Num1, Num2) which could be float or integer . *The program should apply the first given operator (Op1) into the operands (Num1, Num2) and prints the relevant results with related messages in the screen. * Swap operator exchanges the content (swap) of two variables, for this task you are not allowed to use any further variables (You should use just two variables to swap). Exercise 2) Write a C program and call it sortcheck.cpp which receives 10 numbers from input and checks whether these numbers are in ascending order or not. You are not allowed to use arrays. You should not define more than three variables.
  • 39. e.g Welcome to the program written by Your Name: Please enter 10 Numbers: 12 17 23 197 246 356 790 876 909 987 Fine, numbers are in ascending order. Exercise 3) Write a C program to calculates the following equation for entered numbers (n, x). 1+ (nx/1!) - (n(n-1)x^2/2!)...................................... Exercise 4) Write the C program for processing of the students structure. Define the array of a structure called students including following fields: * “First name” * “Family Name” * “Matriculation Number” You should first get the number of students from input and ask user to initialize the fields of the structure for the entered amount of students. Then delete the students with the same “Matriculation Number” and sort the list based on “Family Name” and print the final result in the screen. Samsil Arefin 161-15-7197