Important notes and programs
- Introduction of C language
- Uses / Implementation of C language
- Features of C language
- Data type with format specifiers
- Variable and Rules for writing variables
- Operators and its types in C
- Header files in C
- Structure of C program
- c) Looping Statement in C:
C programming is one of the popular and powerful high level languages developed by Dennis Ritchie. It was originally developed for the development of the UNIX operating system. Later its uses expanded to application development as well as system program development. C language is also known as a middle level language because it supports both assembly and high-level languages.
Some of the implementation of C language are:
- It is used to develop Operating System eg. UNIX.
- It is used to develop new programming platform such as C++, C# [C++ is used in Blockchain Technology (Bitcoin, Etherium etc), also C++ is used in various game engine (Unreal engine and many more)]
- It is used to develop popular web browsers such as Mozilla Firefox, Thunderbird etc.
- It is used in development of various GUIs (Graphical User Interface) and IDEs (Integrated Development Environments).
- Popular Database Management System (DBMS) like MYSQL was developed by using C.
Some of the features of C language are:
- It is a structural programming language. (i.e. it support various control structure like sequence, branching, looping)
- It is a modular programming language. (i.e. big program can be reduced into simpler small program blocks called module)
- It supports graphics.
- It has a huge library function.
- It is a case-sensitive language. Etc.
Data type:
C supports both numeric as well as alphanumeric datas. Frequently used Data types in C are:
| Data type | Declaration keyword | Format Specifier |
| Numeric (for non-decimal numbers) | int | %d |
| Numeric (for decimal numbers) | float | %f |
| Alphanumeric char (for string) | char | %c, %s |
[Note: There are more data type which are not important for SEE if required we will discuss later]
Variable:
Those entities which hold either numeric or alphanumeric values in a program and may change its value throughout the time of program execution are known as variables. It is the character set used store value temporarily.
Rule for writing variable:
- Variable names start with alphabet or underscore sign only, it doesn’t start with any number.
- Variable names should not have blank space in between. Eg, first name is invalid, firstname is valid
- Keywords cannot be used as variable name. Eg, printf = 2; is invalid, num = 2; is valid
- Uppercase variable names are different from lowercase variable names. Eg, age = 2 is different from AGE = 2
[Note: try to use relevant word as variable name without using any special symbol]
Operators:
Operators are the special symbol or sign used to instruct for calculation or perform some specific function or operation on operands or values.
Types of operators:
a) Arithmetic operator: + , – , * , / , %
b) Relational operator: > , < , >= , <= , != (not equal to) , == (equal to)
[Note: Here in C language if you want to compare the equal to is referred by == (two equals)]
c) Assignment operator: = is an assignment operator.
[Note If a = 2 then it means 2 is assigned to variable a, it does not mean a also has value 2 and both are equal.]
d) Logical operator: For logical AND use && , For logical OR use ||, For logical NOT use !
Header files in C
It is a file in the C library with .h extension and contains several functions declaration and definition to execute related functions. These header files come inside angle brackets <> after preprocessor #include. Some popular header files are:
#include<stdio.h> for standard input output function eg, printf, scanf etc.
#include<math.h> for mathematical function eg, pow, sin, cos etc.
#include<graphic.h> for graphical element eg, circle, rectangle, line etc.
#include<string.h> for string handling function eg, strcpy(), strlen(),strupr() etc.
And more. [header file can be added according to requirements]
Sample structure of C program:
// or /* Document Section */
#include<headerfile.extension>
#include<headerfile.extension>
main ( )
{
statements;
}
Explanation: C language is a structured programming language that executes top to bottom, so program execution starts from the header file which is followed by main( ) function. Remember that main( ) is always executed first.
Same program can be written as
#include<stdio.h>
main( )
{
printf(“This is my first C Program”);
}
Output statement:
Output in the C program can be displayed by using ‘printf()’ statement.
A) In order to display only character we can use following syntax
printf(“Sample text/ prototype message”);
Message or display text will be written inside double quotation mark (“ “)
B) In order to display values of variable we can use following syntax
printf(“format specifier”, list of variables,…);
Eg,
int a = 2, b = 3, c;
c = a+b;
printf(“Sum is %d”, c);
It will display: Sum is 5
[Note that any number of format specifier and variable can be displayed]
printf(“Sum of %d and %d is %d”, a, b, c);
It will display: Sum of 2 and 3 is 5
Input statement:
Input in the C program can be taken by using the ‘scanf()’ statement.
Syntax:
scanf(“format specifiers”, &variablename1, &variablename2, ….);
To take number input we can write as:
int a, b;
scanf(“%d %d”, &a, &b);
Here two numeric variables are initialized as integers. So, we write two %d inside double quotation followed by &variablename. & denotes address of that variable.
If we want to take float number such as principal, time and rate than it can be written as
float p, t, r;
scanf(“%f, %f , %f”, &p, &t, &r);
If we want to take character or string as an input than first string variable should be initialized as follows:
char fname[10];
[Note: While taking string as an input we don’t need to write & in variable name]
Example char fname[10], lname[10];
scanf(“%s, %s”, fname, lname);
printf(“%s %s”,fname,lname)
Since variables are initialized as an array of characters we don’t need to mention & while using scanf.
Control structure in C:
C is a structural programming language, we can change the flow of program execution according to the requirement of the user. Following are the control structures used in C.
Control Structure used in C are:
Sequential Statement
Branching Statement
Looping Statement
- Sequential Statement : Program flows from top to bottom sequentially without changing the flow of program execution. [Note: Don’t forget to add header file in each program]
Some important C Programs:
- /* WAP to calculate the area of a rectangle in c. [Hints: a = l x b] */
#include<stdio.h>
main(){
int l, b, a; //declaration of integer data types
printf (“Enter Length: ”);
scanf (“%d”, &l); //to take value of length from keyboard in variable l
printf (“Enter Breadth: “);
scanf (“%d”, &b); //to take value of breadth from keyboard in variable l
a = l * b;
printf (“The area is %d”, a); //to display the value of a on the screen
}
2. WAP to calculate the simple interest in c. [Hints: i= (ptr)/100]
#include<stdio.h>
int main() {
float p, t, r, i; //declaration of float data types
printf (“Enter principal time and rate: ”);
scanf (“%f , %f , %f ”, &p, &t, &r);
i= (p * t * r ) / 100;
printf (“The interest is %f”, i);
}
3. WAP to calculate the volume of a cylinder in c. [Hints: v= pi*r*r*h]
#include<stdio.h>
int main(){ float pi=3.14, r, h, v;
printf (“Enter radius and height: ”);
scanf (“%f %f ”, &r, &h);
v= pi*r*r*h;
printf (“The volume is %f”, v);
}
4. WAP to calculate the average of 3 numbers in c. [Hints: av= (a+b+c)/3]
#include<stdio.h>
int main()
{
float a, b, c, av;
printf (“Enter 3 numbers: ”);
scanf (“%f %f %f”, &a, &b, &rc);
av= (a+b+c)/3;
printf (“The average is %f”, av);
}
5. WAP to convert days into respective years, months and days.
#include<stdio.h>
int main()
{
int days, y, m ;
printf (“Enter total number of days”) ;
scanf (“%d”, &days) ;
y = days / 365 ;
days = days % 365 ;
m = days / 30 ;
days = days % 30 ;
printf (“Year = %d Month = %d Day = %d”, y, m, days) ;
}
6. WAP to convert Paisa into Rupees and Paisa.
#include<stdio.h>
int main()
{
int P, Rs;
printf (“Enter amount in Paisa”);
scanf (“%d”, &P);
Rs = P / 100 ;
P = P % 100 ;
printf (“Rs : %d”, Rs);
printf(“Paisa: %d”,P);
printf(“%d : %d”,Rs,P);
}
7. /* WAP to convert Celsius into Fahrenheit */
#include<stdio.h>
main() {
float f,c;
printf(“enter C”);
scanf(“%f”,&c);
f=c*9/5+32;
printf(“%f”,f);
}
Branching Statement: Program flows can be changed as per the requirement of the user with or without condition.
a. Conditional Branching: Flow of program execution changes according to the condition supplied by the user.
a) if statement
b) if else statement
c) else if ladder
a) if statement syntax:
if (condition)
{
block of statements;
}
8. Write C program to take obtained percent and print pass and fail remarks
#include<stdio.h>
main() {
float p;
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (p>=40)
{ printf (“You are Pass”); }
else
{ printf (“You are Pass”); }
}
9. Write a C program that ask user to enter age and displays whether you are eligible to vote or not. (age>=18, eligible for vote)
b) if else statement: This statement will execute block of statement1 if the condition is true other wise will execute block of statement2 if the condition is false. syntax:
if (condition)
{ block of statements1; }
else
{ block of statements2; }
10. Write a C program to take any two numbers and display greater number
#include<stdio.h>
main()
{ int a, b;
printf (“Enter two number”);
scanf (“%d, %d”, &a, &b);
if (a>b)
{ printf (“%d is greater number”, a); }
else
{ printf (“%d is greater than %d “, b, a); }
}
11. Write a program to check whether given number is odd or even
#include<stdio.h>
int main()
{ int n, r;
printf (“Enter number”);
scanf (“%d”, &n);
r = n%2;
if (r ==0)
{ printf (“%d is even”, n); }
else
{ printf (“%d is odd”, n); }
}
12. Write a program to check whether given number is positive or negative
int main()
{
int n;
printf (“Enter number”);
scanf (“%d”, &n);
if (n>0)
{
printf (“%d is positive”, n);
}
else
{
printf (“%d is negative”, n);
}
return 0;
}
c) else if ladder This statement will execute block of statement1 when condition1 is true, similarly will execute block of statement2 when condition2 is true, like wise block of statement_n will execute when condition_n is true. Default statement will be executed when none of the condition is true. Syntax:
if (condition1)
{
block of statements1;
}
else if (condition2)
{
block of statements2;
}
else if (condition3)
{
block of statements3;
}
.
.
else
{
default statement;
}
13. Write a program to check whether given number is positive, negative or zero.
int main()
{
int n;
printf (“Enter number”);
scanf (“%d”, &n);
if (n>0)
{
printf (“%d is positive”, n);
}
else if (n<0)
{
printf (“%d is negative”, n);
}
else
{
printf(“%d is zero”, n);
}
return 0;
}
14. Write a program to find greatest among three number.
int main()
{
int a, b, c;
printf (“Enter 3 number”);
scanf (“%d %d %d”, &a, &b, &c);
if (a>b && a>c)
{
printf (“%d is greatest”, a);
}
else if (b>a && b>c)
{
printf (“%d is greatest”, b);
}
else
{
printf (“%d is greatest”, c);
}
return 0;
}
15. Write a program to input percentage and check whether he/she secure distinction, first division, second division, third division or fail.
#include<stdio.h>
int main()
{ float p;
printf (“Enter percentage”);
scanf (“%f”, &p);
if (p>=80)
{ printf (“%f is Distinction”, p); }
else if (p>=60 && p<80)
{ printf (“%f is First division”, p); }
else if (p>=50 && p<60)
{ printf (“%f is Second division”, p); }
else if (p>=40 && p<50)
{ printf (“%f is Third division”, p); }
else
{ printf (“%f is Fail”, p); }
}
Same program can be written as :
#include<stdio.h>
int main()
{ float p;
printf (“Enter percentage”);
scanf (“%f”, &p);
if (p>=80)
{ printf (“%f is Distinction”, p); }
else if (p>=60 )
{ printf (“%f is First division”, p); }
else if (p>=50 )
{ printf (“%f is Second division”, p); }
else if (p>=40 )
{ printf (“%f is Third division”, p); }
else
{ printf (“%f is Fail”, p); }
}
16. Calculate Total electricity bill on the basis of following data.
Unit Consumed Charge per unit
≤ 50 unit Rs 10/unit
>50 and ≤100 Rs 12/unit
>100 Rs 15/unit
int main()
{
float u, rs;
printf (“Enter unit consumed”);
scanf (“%f”, &u);
if (u<=50)
{
rs = u*10;
printf (“Total amount is %f”, rs);
}
else if (u>50 && u<=100)
{
rs = 50*10 + (u-50)*12;
printf (“Total amount is %f”, rs);
}
else
{
rs = 50*10 + 50*12 + (u-100)*15;
printf (“Total amount is %f”, rs);
}
return 0;
}
Unconditional Branching: Flow of program execution changes without condition.
a) goto statement syntax
goto label;
block of statements;
label:
Here flow of program execution will go down directly from goto to label without any condition skipping all the block of statements within.
OR
label:
block of statements;
goto label;
Here flow of program execution will go up directly from goto to label without any condition.
Suppose we want to take percentage from the user and check whether he/she is pass or fail keeping pass mark to be 40. Let us make our program will not accept percentage greater than 100.
int main()
{
float p;
label:
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (P>100)
{
printf(“Please enter value between 0-100”);
goto label;
}
if (p>=40)
{
printf (“You are Pass”);
}
else
{
printf(“You are fail”);
}
return 0;
}
In this program example, our program will ask user to input percentage between 0 to 100. If user input value greater than 100 than it will ask again for input. This is the use of goto statement in C.
c) Looping Statement in C: The process of repeating same block of statement multiple number of time as per the requirement of the user is called looping or iteration. C supports following looping statement.
for loop
while loop
do loop