SlideShare a Scribd company logo
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

More Related Content

What's hot

9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
Sowri Rajan
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman 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 2
Rumman 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 c
BUBT
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
Rumman Ansari
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
Prabhu 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 c
BUBT
 
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
BUBT
 
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
BUBT
 
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
Mainak 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
 

What's hot (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’
 

Similar to C programming

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
Tushar 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 NOTES
LeahRachael
 
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 Functions
DhivyaSubramaniyam
 
C Programming
C ProgrammingC Programming
C Programming
Raj vardhan
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana 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 C
Yi-Hsiu Hsu
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
Rumman Ansari
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
rakshithatan
 
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++ ________lecture2
Warui Maina
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
MaryJacob24
 
Basics of c
Basics of cBasics of c
Basics of c
DebanjanSarkar11
 
Function in c
Function in cFunction in c
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
Nithya K
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
cricketreview
 

Similar to 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
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
 
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
 

More from 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 protocol
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
Samsil 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 application
Samsil 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 inserting
Samsil Arefin
 
Number theory
Number theoryNumber theory
Number theory
Samsil 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 order
Samsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
Samsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Stack
StackStack
Sorting
SortingSorting
Sorting
Samsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
Samsil Arefin
 
Cyber security
Cyber securityCyber security
Cyber security
Samsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
Samsil Arefin
 
Structure and union
Structure and unionStructure and union
Structure and union
Samsil 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 java
Samsil Arefin
 
String
StringString

More from 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
 

Recently uploaded

Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
nikitacareer3
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
RicletoEspinosa1
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
dxobcob
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 

Recently uploaded (20)

Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 

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