0% found this document useful (0 votes)
2 views10 pages

C Programming

C programming is a powerful high-level language developed by Dennis Ritchie, initially for UNIX operating system development, and is widely used for application and system programming. Key features include its structural and modular programming capabilities, support for graphics, and a rich library of functions. The document covers data types, variables, operators, control structures, and provides examples of C programs for various applications.

Uploaded by

anshikashaha0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

C Programming

C programming is a powerful high-level language developed by Dennis Ritchie, initially for UNIX operating system development, and is widely used for application and system programming. Key features include its structural and modular programming capabilities, support for graphics, and a rich library of functions. The document covers data types, variables, operators, control structures, and provides examples of C programs for various applications.

Uploaded by

anshikashaha0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C programming.

Introduction: C programming is one of the popular and powerful high-level language developed by Denish Ritchie. It was
originally developed for development of UNIX operating system. Later its uses expand to application development as well as
system program development. So, it is also known as middle level language. In brief,
Some of the implementation of C language are:
1. It is used to develop Operating System e.g., UNIX.
2. 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)]
3. It is used to develop popular web browser such as Mozilla Firefox, Thunderbird etc.
4. It is used in development of various GUIs (Graphical User Interface) and IDEs (Integrated Development Environments).
5. Popular Database Management System (DBMS) like MYSQL was developed by using C.
Some of the features of C language are:
1. It is a structural programming language. (i.e., it supports various control structure like sequence, branching, looping)
2. It is a modular programming language. (i.e., big program can be reduced into simpler small program blocks called
module)
3. It supports graphics.
4. It has huge library function.
5. It is case-sensitive language.
Data type: C support both numeric as well as alphanumeric data. Frequently used Data types in C are:

Type Data type used Format Specifier

Numeric int (for non-decimal numbers) %d

Numeric float (for decimal numbers) %f

Alphanumeric char (for string) %s or %c

[Note: There are more data type which we will discuss later in need]
Variable: Those entities which holds either numeric or alphanumeric values in program and may change its value throughout the
time of program execution are known as variables.
Rule for writing variable:
1. Variable name should not start will number. E.g., 1age is invalid, age1 is valid
2. Variable name should not have blank space. E.g., first name is invalid, firstname is valid
3. Keywords cannot be used as variable name. E.g., printf = 2; is invalid, num = 2; is valid
4. Uppercase variable name is different from lowercase variable name. E.g., age = 2 is different from AGE = 2
[Note: try to use relevant word as variable name without using any special symbol]
Operators: The special symbol or sign used to perform some specific function or operation are called operators.
Types of operators:
a) Arithmetic operator
+,- ,*, /, %
Note that here, if c = 5/2
The result will be 2 if we initialize ‘c’ as int c.
The result will be 2.5 if we initialize ‘c’ as float c.
If c = 5%2 then result will be 1. Since, % gives remainder after division.
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 C library with .h extension and contains several functions declaration and definition. Such as we use.
#include for standard input output function e.g., printf, scanf etc.
#include for mathematical function e.g., pow, sin, cos etc.
#include for graphical element e.g., circle, rectangle, line etc.
#include for string handling function e.g., strcpy (), strlen (), strupr () etc. And more. [header file can be added according to
requirements]
Sample structure of C program:
#include
#include
void main ()
{
getch ();
}
Explanation: C program always execute from top left to right bottom, so program execution starts from header file which is
followed by main () function. Remember that main () is always executed first. Since, main () is a function it will return some
values so we write void in front of main () as a return type which mean null value is returned. getch () will hold our output on the
screen unless an until user press some keys. It means get character. All other statements written inside main () function will be
executed sequentially. If you are using void and getch don’t forget to also include conio.h as header file.
Same program can be written as
#include
int main ()
{

return 0;
}
Here, since we have written int main () The main () function should return integer value so we write return 0 instead of getch ()
here. While using int main () and return 0, we don’t need to include conio.h
Output statement:
Output in C program can be displayed by using ‘printf’ statement.
A) In order to display only character, we can use following syntax
printf (“Sample text”);
This will display anything that is written inside double quotation mark (““)
B) In order to display values of variable we can use following syntax
printf (“format specifier”, list of variables…);
E.g.,
int a = 2, b = 3, c;
c = a+b;
printf (“Sum is %d”, c);
It will display: Sum is c
[Note that any number of format specifier and variable can be displayed]
Same program can be display as
printf (“Sum of %d and %d is %d”, a, b, c);
It will display: Sum of 2 and 3 is 5
[Note: if a, b and c was initialized as float than every %d should be replaced by %f]
Input statement:
Input in C program can be taken by using ‘scanf’ statement.
In order to take input from user we can use following syntax
scanf (“format specifiers”, &variablename1, &variablename2, ….);
Suppose we want to take a and b as input from user. To take number input we can write
int a, b;
scanf (“%d %d”, &a, &b);
In this example two numeric variable are initialized as integer. So, we write two %d inside double quotation followed by
&variable name. & denotes address of that variable.
Another example,
If we want to take principal, time and rate than it can be written as
float p, t, r;
scanf (“%f %f %f”, &p, &t, &r);
In this example three numeric variable are initialized as float. Since, their values may be in decimal So, we write three %f inside
double quotation followed by &variable name. & denotes address of that variable.
If we want to take character or string as an input than first string variable should be initialized as follows:
char fname [10];
Remember if we initialized variable as ‘char’ than variable used become string. Since string in C is array of character, we should
suffix variable name with size [size] i.e., maximum length of character that the variable can hold.
[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);
Since variable are initialized as array of character we don’t need to mention & while using scanf.
Control structure in C:
Since 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 structure used in C.
Control Structure used in C

Sequence Branching Looping

Program flows from top to bottom Conditional Branching: if, if else, else if,
for
sequentially. switch

Unconditional Branching: go to while

do

Sequence: 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]
Important C Program example
1. WAP to calculate the area of a rectangle in c. [Hints: a = l x b]
#include<stdio.h>
#include<conio.h>
void main ()
{
int l, b, a;
printf (“Enter Length:”);
scanf (“%d”, &l);
printf (“Enter Breadth: “);
scanf (“%d”, &b);
a = l * b;
printf (“The area is %d”, a);
getch ();
}
2. WAP to calculate the simple interest in c. [Hints: i= (ptr)/100]
#include<stdio.h>
#include<conio.h>
int main ()
{
float p, t, r, i;
printf (“Enter principal time and rate:”);
scanf (“%f %f %f”, &p, &t, &r);
i= (p*t*r)/100;
printf (“The interest is %f”, i);
return 0;
}
3. WAP to calculate the average of 3 number in c. [Hints: av= (a+b+c)/3]
#include<stdio.h>
#include<conio.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);
return 0;
}
4. WAP to calculate the volume of cylinder in c. [Hints: v= pi*r*r*h]
#include<stdio.h>
#include<conio.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);
return 0;
}
5. WAP to convert days into respective years, months and days.
#include<stdio.h>
#include<conio.h>
int main()
{
int days, y, m,d, rd;
printf (“Enter days”);
scanf (“%d”, &days);
y = days/365;
rd = days%365;
m = rd/30;
d = rd%30;
printf (“Year = %d Month = %d Day = %d", y, m, d);
return 0;
}
Branching: Program flows can be changed as per the requirement of the user with or without condition.
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;
}
1. Program example of if statement [Note: Don't forget to add header file]
#include<stdio.h>
#include<conio.h>

int main ()
{
float p;
printf (“Enter percentage”);
scanf (“%f”, &p);
if (p>=40)
{
printf (“You are Pass");
}
return 0;
}
int main ()
{
int a;
printf (“Enter your age”);
scanf (“%d”, &a);
if (a>=18)
{
printf (“You are eligible to vote");
}
return 0;
}
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;
}
1. Program example of if statement [Note: Don't forget to add header file]
#include<stdio.h>
#include<conio.h>

int main()
{
float p;
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (p>=40)
{
printf (“You are Pass");
}
else
{
printf("You are fail");
}
return 0;
}
int main()
{
int a;
printf (“Enter your age ”);
scanf (“%d”, &a);
if (a>=18)
{
printf (“You are eligible to vote");
}
else
{
printf("You are not eligible to vote");
}
return 0;
}
1. Write a program to find greatest among two number
#include<stdio.h>
#include<conio.h>

int main()
{
int a,b;
printf (“Enter two number”);
scanf (“%d %d”, &a, &b);
if (a>b)
{
printf (“%d is greatest", a);
}
else
{
printf (“%d is greatest", b);
}
return 0;
}
1. Write a program to check whether given number is odd or even
#include<stdio.h>
#include<conio.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);
}
return 0;
}
1. Write a program to check whether given number is positive or negative
#include<stdio.h>
#include<conio.h>

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 w ill
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;
}
Program example of if statement [Note: Don't forget to add header file]
1.Write a program to check whether given number is positive, negative or zero.
#include<stdio.h>
#include<conio.h>

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;
}
2.Write a program to find greatest among three number.
#include<stdio.h>
#include<conio.h>

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;
}
3.Write a program to input percentage and check whether he/she secure distinction, first division, second division, third division or
fail.
#include<stdio.h>
#include<conio.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);
}
return 0;
}
Q) 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
#include<stdio.h>
#include<conio.h>
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.
1.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.
#include<stdio.h>
#include<conio.h>
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 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy