SlideShare a Scribd company logo
1 of 63
KHANAL PRALHAD
Navodit , Samakhusi, Kathmandu
Structure of C programming
Header Files and C pre-processors
Header Files:
 A header file is a file containing C declarations and macro definitions to be
shared between several source files. You request the use of a header file in your
program by including it, with the C pre-processing directive ‘#include’.
 The usual convention is to give header files names that end with .h
 For example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI
#define area 100
Pre-Processors:
 Is a macro processor that is used automatically by the C compiler to
transform your program before actual compilation (Pre-processor directives
are executed before compilation.).
 It is called a macro processor because it allows you to define macros, which
are brief abbreviations for longer constructs. Macro is defined by #define
directive.
 Pre-processing directives are lines in your program that start with #. For
example, #define is the directive that defines a macro. Whitespace is also
allowed before and after the #.
 For Examples:
#include
#define, etc.
Assignment 1
• What is pre-processor? List any two pre-processor.
• Write down the importance of header files and explain about any
three of them .
4.2 Fundamentals of C
Character Set
• As every language contains a set of characters used to construct words, statements,
etc., C language also has a set of characters which include alphabets, digits, and
special symbols. C language supports a total of 256 characters.
• Every C program contains statements. These statements are constructed using
words and these words are constructed using characters from C character set. C
language character set contains the following set of characters...
1. Alphabets/Letters
2. Digits
3. Special Symbols
1. Alphabets
C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
lower case letters - a to z
UPPER CASE LETTERS - A to Z
2. Digits
C language supports 10 digits which are used to construct numerical values in C
language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3. Special Symbols
C language supports a rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, backspaces, and other
special symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , <  | tab newline
space NULL bell backspace vertical tab etc.,
Use of Comments
• They are ignored by the compiler
• This section is used to provide a small description of the program. The
comment lines are simply ignored by the compiler, that means they are not
executed.
• In C, there are two types of comments.
(a) Single Line Comments: Single line comment begins with // symbol. We can
write any number of single line comments.
(b) Multiple Lines Comments: Multiple lines comment begins with /* symbol
and ends with */. We can write any number of multiple lines comments in a
program.
• The comment lines are optional. Based on the requirement, we write comments.
All the comment lines in a C program just provide the guidelines to understand the
program and its code.
For Example
// C program to illustrate use of single-line comment
#include <stdio.h>
int main()
{
// Single line comment
printf("Welcome to Online Classes");
getch();
}
For Example
/* C program to illustrate
use of multi-line comment */
#include <stdio.h>
int main()
{
/* Multi-line comment
written to demonstrate comments
in C */
printf("Welcome to Online Class");
getch();
}
Statements: Simple and Compound
Simple Statements:
 is an expression (followed by a semicolon, the terminator for all simple statements). Its value is computed and
discarded.
 For example
#include <stdio.h>
int main()
{
int x, y, z;
printf("Enter two numbers to addn");
scanf("%d%d", &x, &y);
z = x + y;
printf("Sum of the numbers = %dn", z);
return 0;}
 Other examples of simple statements are the jump statements return, break, continue, and goto.
Compound Statements
• Several individual statements enclosed in a pair of braces {}.
• It provides capabilities for enabling statements within other statements.
if(houseIsOnFire) {
/* ouch! */
scream();
runAway();
}
• Compound statements come in two varieties: conditionals and loops.
Example
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %dn", a);
}
return 0;
}
4.3 Operators and Expressions
Operators
• Arithmetical Operator(Binary Operator)
• Relational Operator(Comparison Operator)
• Logical Operator(Boolean Operator)
• Assignment Operator
• Unary Operator(Increment/decrement Operator)
• Conditional Operators(Ternary Operator)
• Comma Operators
• Size of Operator
Type Casting And
Conversion
 The type conversion is the
process of converting a
data value from one data
type to another data type
automatically by the
compiler.
 Sometimes type conversion is
also called implicit type
conversion. The implicit type
conversion is automatically
performed by the compiler.
Type Casting And
Conversion
 Typecasting is also called an
explicit type conversion.
Compiler converts data from one
data type to another data type
implicitly.
 When compiler converts
implicitly, there may be a data
loss. In such a case, we convert
the data from one data type to
another data type using
explicit type conversion.
 Also termed as local
conversion.
4.4 Input/Output (I/O) Functions
Write a c program to add two numbers.
Write a c program to input four numbers and find their average and sum
Write a c program to add two numbers using both implicit and explicit method.
Write a c program to define compound statement.
WAP to find square of numbers.
WAP to calculate cube of number.
WAP to calculate area and circumference of circle.
WAP to find area and perimeter of rectangle
WAP to calculate distance using S= 𝑢𝑡 + 1/2𝑎𝑡2
WAP to find SI and A.
Write a c program to input four numbers and find their average
and sum
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d,sum,avg;
printf("enter four numbers");
scanf("%d%d%d%d",&a,&b,&c,&d);
sum=a+b+c+d;
printf("sum is %d: ", sum);
avg=(sum)/4;
printf("average is %d ",avg);
getch();
}
Write a c program to add two numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,sum;
printf("enter two numbers");
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum is %d: ", sum);
getch();
}
Write a c program to add two numbers using implicit method.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
float sum;
printf("enter two numbers");
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum is %f: ", sum);
getch();
}
Write a c program to add two numbers using explicit method.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
float sum;
printf("enter two numbers");
scanf("%d%d",&a,&b);
sum=(float)a+b;
printf("sum is %f: ", sum);
getch();
}
Differentiate between printf() and scanf()
Printf() Scanf()
Ternary Operator
 Ternary operator allows executing different code depending on the
value of a condition, and the result of the expression is the returned
value of the executed code.
 The main advantage of using ternary operator is to reduce the
number of line codes and improve the performance of application.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter any two numbers n");
scanf("%d%d", &a , &b);
if(a>b) {
printf("%d",a);
printf(" is largest number of given
numbers n"); }
else{
printf("%d",b);
printf(" is largest number of given
numbers n"); }
return 0;
}
#include<stdio.h>
int main() {
int a, b, max;
printf("Enter any two numbers
n");
scanf("%d%d", & a, & b);
max = (a > b) ? a : b;
printf("%d", max);
printf("is the largest number of
given numbers");
return 0;
}
4.5 Control Structure
If cost price and selling price of an item is input through the keyboard,
WAP to determine whether the seller has made profit or incurred loss. Also
determine how much profit he made or loss be incurred.
#include<stdio.h>
#include<conio.h>
void main()
{
int cp,sp,tl,tp;
printf("Enter the Cost pricen");
scanf("%d",&cp);
printf("Enter the Selling pricen");
scanf("%d",&sp);
if(sp>cp)
{
tp=sp-cp;
printf("The profit is %d",tp);
}
else if (sp<cp)
{
tl=cp-sp;
printf("The loss is %d",tl);
}
else
printf("There is neither profit nor loss");
}
Control structure
• Are those programming constructs that controls the flow of programming
statements.
• It specifies the order of statements in the program.
• Are of three types.
A. Decision making(Selective/Branching) Control Structure
a) Conditional Statements
Are those statements that makes decision based on the condition and its
outcome.
Are of different forms
i. If Statement
ii. If else statement
iii. If else if statement(if-else ladder)
iv. Nested if else statement
a) Switch-Case Statements
It makes decision based upon the value of expression which is included in
the switch statement and branches.
i. If Statement
• if statement is used to verify the given condition and executes the
block of statements based on the condition result.
• The simple if statement evaluates specified condition.
• If it is TRUE, it executes the next statement or block of statements. If
the condition is FALSE, it skips the execution of the next statement or
block of statements.
• Simple if statement is used when we have only one option that is
executed or skipped based on a condition.
// Program to check given number is divisible by 5 or not
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5n") ;
printf("statement does not belong to if!!!") ;
getch();
}
1. WAP to check given number is positive or not.
2. WAP to check whether the given number is even or not.
3. WAP to check whether the given number is divisible by 10 or
not.
4. WAP to check whether the given number is 3 or not.
ii. If else statement
• The if-else statement is used to verify the given condition and executes
only one out of the two blocks of statements based on the condition
result.
• The if-else statement evaluates the specified condition.
• If it is TRUE, it executes a block of statements (True block). If the
condition is FALSE, it executes another block of statements (False
block).
• The if-else statement is used when we have two options and only
one option has to be executed based on a condition result (TRUE
or FALSE).
// To Test whether given number is even or odd.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%2 == 0 )
printf("Given number is EVENn") ;
else
printf("Given number is ODDn") ;
}
1. WAP to read two numbers and display smallest one.
2. WAP to check given number is odd or even.
3. WAP to check given number is positive or negative.
4. WAP to check given number is divisible by 5 and not by
10.
iii. If else if statement(if else ladder)
• Writing a if statement inside else of an if statement is called if-else-if statement.
• The if-else-if statement can be defined using any combination of simple if & if-
else statements.
• Conditions are checked from top of the ladder to downwards. As soon as the true
conditions are found, the statements associated with it are executed and the control
is transferred to outside of the ladder skipping the remaining part of the ladder.
#include<stdio.h>
#include<conio.h>
void main(){
int a, b, c ; // largest of three numbers
printf("Enter any three integer numbers: ") ;
scanf("%d%d%d", &a, &b, &c) ;
if( a>=b && a>=c)
printf("%d is the largest number", a) ;
else if (b>=c)
printf("%d is the largest number", b) ;
else
printf("%d is the largest number", c) ;
}
1. WAP to find the largest among four
numbers.
2. WAP to find smallest among four numbers.
3. WAP to check whether the given number is
positive, negative or zero.
iv. Nested if else statement
• Writing a if statement inside another if statement is called nested if statement.
• The if-else-if statement can be defined using any combination of simple if & if-
else statements.
• IT is used when a condition is to be checked inside another condition at a time in
the same program to make decision.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n < 100 ) //nested if else statement checking odd or even
{
printf("Given number is below 100n") ;
if( n%2 == 0)
printf("And it is EVEN") ;
else
printf("And it is ODD") ;
}
else
printf("Given number is not below 100") ;
}
b. Switch Case Statements
• the number of options increases, the complexity of the program also gets
increased.
• This type of problem can be solved very easily using a switch statement. Using the
switch statement, one can select only one option from more number of options
very easily.
• In the switch statement, we provide a value that is to be compared with a value
associated with each option. Whenever the given value matches the value
associated with an option, the execution starts from that option.
• In the switch statement, every option is defined as a case.
 The switch statement contains one or more cases and each case has a value
associated with it.
 At first switch statement compares the first case value with the switchValue,
if it gets matched the execution starts from the first case. If it doesn't match
the switch statement compares the second case value with the switchValue
and if it is matched the execution starts from the second case. This process
continues until it finds a match.
 If no case value matches with the switchValue specified in the switch
statement, then a special case called default is executed.
 When a case value matches with the switchValue, the execution starts from
that particular case.
 This execution flow continues with the next case statements also. To avoid
this, we use the "break" statement at the end of each case. That means the
break statement is used to terminate the switch statement. However, it is
optional.
#include<stdio.h>
#include<conio.h>
int main(){
int n ;
printf("Enter any digit: ") ;
scanf("%d", &n) ;
switch( n )
{
case 0: printf("ZERO") ;
break ;
case 1: printf("ONE") ;
break ;
case 2: printf("TWO") ;
break ;
case 3: printf("THREE") ;
break ;
case 4: printf("FOUR") ;
break ;
case 5: printf("FIVE") ;
break ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
getch() ;
}
1. WAP to display the name of the day in a week, using switch case
statement.
2. WAP to display the names of the month in a year, using switch
case statements.
3. WAP to read any two integer values from users and calculate sum,
difference, and product using seitch case.
C programming
C programming

More Related Content

What's hot

Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerVineet Kumar Saini
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpen Gurukul
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c languageIndia
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageAbhishek Dwivedi
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 

What's hot (20)

Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Casa lab manual
Casa lab manualCasa lab manual
Casa lab manual
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Structure
StructureStructure
Structure
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
C tutorial
C tutorialC tutorial
C tutorial
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C Language
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Cnotes
CnotesCnotes
Cnotes
 

Similar to C programming

C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubeykiranrajat
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...RSathyaPriyaCSEKIOT
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdfShivamYadav886008
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxsaivasu4
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxPragatheshP
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 

Similar to C programming (20)

C programming
C programmingC programming
C programming
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Chapter3
Chapter3Chapter3
Chapter3
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
C programming language
C programming languageC programming language
C programming language
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdf
 
UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

C programming

  • 1. KHANAL PRALHAD Navodit , Samakhusi, Kathmandu
  • 2. Structure of C programming
  • 3.
  • 4. Header Files and C pre-processors Header Files:  A header file is a file containing C declarations and macro definitions to be shared between several source files. You request the use of a header file in your program by including it, with the C pre-processing directive ‘#include’.  The usual convention is to give header files names that end with .h  For example: #include<stdio.h> #include<conio.h> #include<math.h> #define PI #define area 100
  • 5. Pre-Processors:  Is a macro processor that is used automatically by the C compiler to transform your program before actual compilation (Pre-processor directives are executed before compilation.).  It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. Macro is defined by #define directive.  Pre-processing directives are lines in your program that start with #. For example, #define is the directive that defines a macro. Whitespace is also allowed before and after the #.  For Examples: #include #define, etc.
  • 6. Assignment 1 • What is pre-processor? List any two pre-processor. • Write down the importance of header files and explain about any three of them .
  • 8. Character Set • As every language contains a set of characters used to construct words, statements, etc., C language also has a set of characters which include alphabets, digits, and special symbols. C language supports a total of 256 characters. • Every C program contains statements. These statements are constructed using words and these words are constructed using characters from C character set. C language character set contains the following set of characters... 1. Alphabets/Letters 2. Digits 3. Special Symbols
  • 9. 1. Alphabets C language supports all the alphabets from the English language. Lower and upper case letters together support 52 alphabets. lower case letters - a to z UPPER CASE LETTERS - A to Z 2. Digits C language supports 10 digits which are used to construct numerical values in C language. Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 3. Special Symbols C language supports a rich set of special symbols that include symbols to perform mathematical operations, to check conditions, white spaces, backspaces, and other special symbols. Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < | tab newline space NULL bell backspace vertical tab etc.,
  • 10. Use of Comments • They are ignored by the compiler • This section is used to provide a small description of the program. The comment lines are simply ignored by the compiler, that means they are not executed. • In C, there are two types of comments. (a) Single Line Comments: Single line comment begins with // symbol. We can write any number of single line comments. (b) Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends with */. We can write any number of multiple lines comments in a program. • The comment lines are optional. Based on the requirement, we write comments. All the comment lines in a C program just provide the guidelines to understand the program and its code.
  • 11. For Example // C program to illustrate use of single-line comment #include <stdio.h> int main() { // Single line comment printf("Welcome to Online Classes"); getch(); }
  • 12. For Example /* C program to illustrate use of multi-line comment */ #include <stdio.h> int main() { /* Multi-line comment written to demonstrate comments in C */ printf("Welcome to Online Class"); getch(); }
  • 13. Statements: Simple and Compound Simple Statements:  is an expression (followed by a semicolon, the terminator for all simple statements). Its value is computed and discarded.  For example #include <stdio.h> int main() { int x, y, z; printf("Enter two numbers to addn"); scanf("%d%d", &x, &y); z = x + y; printf("Sum of the numbers = %dn", z); return 0;}  Other examples of simple statements are the jump statements return, break, continue, and goto.
  • 14. Compound Statements • Several individual statements enclosed in a pair of braces {}. • It provides capabilities for enabling statements within other statements. if(houseIsOnFire) { /* ouch! */ scream(); runAway(); } • Compound statements come in two varieties: conditionals and loops.
  • 15. Example #include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ){ printf("value of a: %dn", a); } return 0; }
  • 16.
  • 17. 4.3 Operators and Expressions
  • 18. Operators • Arithmetical Operator(Binary Operator) • Relational Operator(Comparison Operator) • Logical Operator(Boolean Operator) • Assignment Operator • Unary Operator(Increment/decrement Operator) • Conditional Operators(Ternary Operator) • Comma Operators • Size of Operator
  • 19. Type Casting And Conversion  The type conversion is the process of converting a data value from one data type to another data type automatically by the compiler.  Sometimes type conversion is also called implicit type conversion. The implicit type conversion is automatically performed by the compiler.
  • 20.
  • 21. Type Casting And Conversion  Typecasting is also called an explicit type conversion. Compiler converts data from one data type to another data type implicitly.  When compiler converts implicitly, there may be a data loss. In such a case, we convert the data from one data type to another data type using explicit type conversion.  Also termed as local conversion.
  • 23. Write a c program to add two numbers. Write a c program to input four numbers and find their average and sum Write a c program to add two numbers using both implicit and explicit method. Write a c program to define compound statement. WAP to find square of numbers. WAP to calculate cube of number. WAP to calculate area and circumference of circle. WAP to find area and perimeter of rectangle WAP to calculate distance using S= 𝑢𝑡 + 1/2𝑎𝑡2 WAP to find SI and A.
  • 24. Write a c program to input four numbers and find their average and sum #include<stdio.h> #include<conio.h> int main() { int a,b,c,d,sum,avg; printf("enter four numbers"); scanf("%d%d%d%d",&a,&b,&c,&d); sum=a+b+c+d; printf("sum is %d: ", sum); avg=(sum)/4; printf("average is %d ",avg); getch(); }
  • 25. Write a c program to add two numbers. #include<stdio.h> #include<conio.h> int main() { int a,b,sum; printf("enter two numbers"); scanf("%d%d",&a,&b); sum=a+b; printf("sum is %d: ", sum); getch(); }
  • 26. Write a c program to add two numbers using implicit method. #include<stdio.h> #include<conio.h> int main() { int a,b; float sum; printf("enter two numbers"); scanf("%d%d",&a,&b); sum=a+b; printf("sum is %f: ", sum); getch(); }
  • 27. Write a c program to add two numbers using explicit method. #include<stdio.h> #include<conio.h> int main() { int a,b; float sum; printf("enter two numbers"); scanf("%d%d",&a,&b); sum=(float)a+b; printf("sum is %f: ", sum); getch(); }
  • 28. Differentiate between printf() and scanf() Printf() Scanf()
  • 30.  Ternary operator allows executing different code depending on the value of a condition, and the result of the expression is the returned value of the executed code.  The main advantage of using ternary operator is to reduce the number of line codes and improve the performance of application.
  • 31. #include<stdio.h> int main() { int a,b; printf("Enter any two numbers n"); scanf("%d%d", &a , &b); if(a>b) { printf("%d",a); printf(" is largest number of given numbers n"); } else{ printf("%d",b); printf(" is largest number of given numbers n"); } return 0; } #include<stdio.h> int main() { int a, b, max; printf("Enter any two numbers n"); scanf("%d%d", & a, & b); max = (a > b) ? a : b; printf("%d", max); printf("is the largest number of given numbers"); return 0; }
  • 33. If cost price and selling price of an item is input through the keyboard, WAP to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss be incurred.
  • 34. #include<stdio.h> #include<conio.h> void main() { int cp,sp,tl,tp; printf("Enter the Cost pricen"); scanf("%d",&cp); printf("Enter the Selling pricen"); scanf("%d",&sp); if(sp>cp) { tp=sp-cp; printf("The profit is %d",tp); } else if (sp<cp) { tl=cp-sp; printf("The loss is %d",tl); } else printf("There is neither profit nor loss"); }
  • 35. Control structure • Are those programming constructs that controls the flow of programming statements. • It specifies the order of statements in the program. • Are of three types.
  • 36.
  • 37. A. Decision making(Selective/Branching) Control Structure a) Conditional Statements Are those statements that makes decision based on the condition and its outcome. Are of different forms i. If Statement ii. If else statement iii. If else if statement(if-else ladder) iv. Nested if else statement a) Switch-Case Statements It makes decision based upon the value of expression which is included in the switch statement and branches.
  • 38.
  • 39. i. If Statement • if statement is used to verify the given condition and executes the block of statements based on the condition result. • The simple if statement evaluates specified condition. • If it is TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips the execution of the next statement or block of statements. • Simple if statement is used when we have only one option that is executed or skipped based on a condition.
  • 40.
  • 41.
  • 42. // Program to check given number is divisible by 5 or not #include<stdio.h> #include<conio.h> void main(){ int n ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n%5 == 0 ) printf("Given number is divisible by 5n") ; printf("statement does not belong to if!!!") ; getch(); }
  • 43. 1. WAP to check given number is positive or not. 2. WAP to check whether the given number is even or not. 3. WAP to check whether the given number is divisible by 10 or not. 4. WAP to check whether the given number is 3 or not.
  • 44. ii. If else statement • The if-else statement is used to verify the given condition and executes only one out of the two blocks of statements based on the condition result. • The if-else statement evaluates the specified condition. • If it is TRUE, it executes a block of statements (True block). If the condition is FALSE, it executes another block of statements (False block). • The if-else statement is used when we have two options and only one option has to be executed based on a condition result (TRUE or FALSE).
  • 45.
  • 46. // To Test whether given number is even or odd. #include<stdio.h> #include<conio.h> void main(){ int n ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n%2 == 0 ) printf("Given number is EVENn") ; else printf("Given number is ODDn") ; }
  • 47. 1. WAP to read two numbers and display smallest one. 2. WAP to check given number is odd or even. 3. WAP to check given number is positive or negative. 4. WAP to check given number is divisible by 5 and not by 10.
  • 48. iii. If else if statement(if else ladder) • Writing a if statement inside else of an if statement is called if-else-if statement. • The if-else-if statement can be defined using any combination of simple if & if- else statements. • Conditions are checked from top of the ladder to downwards. As soon as the true conditions are found, the statements associated with it are executed and the control is transferred to outside of the ladder skipping the remaining part of the ladder.
  • 49.
  • 50.
  • 51. #include<stdio.h> #include<conio.h> void main(){ int a, b, c ; // largest of three numbers printf("Enter any three integer numbers: ") ; scanf("%d%d%d", &a, &b, &c) ; if( a>=b && a>=c) printf("%d is the largest number", a) ; else if (b>=c) printf("%d is the largest number", b) ; else printf("%d is the largest number", c) ; }
  • 52. 1. WAP to find the largest among four numbers. 2. WAP to find smallest among four numbers. 3. WAP to check whether the given number is positive, negative or zero.
  • 53. iv. Nested if else statement • Writing a if statement inside another if statement is called nested if statement. • The if-else-if statement can be defined using any combination of simple if & if- else statements. • IT is used when a condition is to be checked inside another condition at a time in the same program to make decision.
  • 54.
  • 55.
  • 56. #include<stdio.h> #include<conio.h> void main(){ int n ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n < 100 ) //nested if else statement checking odd or even { printf("Given number is below 100n") ; if( n%2 == 0) printf("And it is EVEN") ; else printf("And it is ODD") ; } else printf("Given number is not below 100") ; }
  • 57. b. Switch Case Statements • the number of options increases, the complexity of the program also gets increased. • This type of problem can be solved very easily using a switch statement. Using the switch statement, one can select only one option from more number of options very easily. • In the switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. • In the switch statement, every option is defined as a case.
  • 58.
  • 59.  The switch statement contains one or more cases and each case has a value associated with it.  At first switch statement compares the first case value with the switchValue, if it gets matched the execution starts from the first case. If it doesn't match the switch statement compares the second case value with the switchValue and if it is matched the execution starts from the second case. This process continues until it finds a match.  If no case value matches with the switchValue specified in the switch statement, then a special case called default is executed.  When a case value matches with the switchValue, the execution starts from that particular case.  This execution flow continues with the next case statements also. To avoid this, we use the "break" statement at the end of each case. That means the break statement is used to terminate the switch statement. However, it is optional.
  • 60. #include<stdio.h> #include<conio.h> int main(){ int n ; printf("Enter any digit: ") ; scanf("%d", &n) ; switch( n ) { case 0: printf("ZERO") ; break ; case 1: printf("ONE") ; break ; case 2: printf("TWO") ; break ; case 3: printf("THREE") ; break ; case 4: printf("FOUR") ; break ; case 5: printf("FIVE") ; break ; case 6: printf("SIX") ; break ; case 7: printf("SEVEN") ; break ; case 8: printf("EIGHT") ; break ; case 9: printf("NINE") ; break ; default: printf("Not a Digit") ; } getch() ; }
  • 61. 1. WAP to display the name of the day in a week, using switch case statement. 2. WAP to display the names of the month in a year, using switch case statements. 3. WAP to read any two integer values from users and calculate sum, difference, and product using seitch case.