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;
}
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();
}
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.