0% found this document useful (0 votes)
22 views

Notes of C Language

C language note

Uploaded by

rauniyarchanda01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Notes of C Language

C language note

Uploaded by

rauniyarchanda01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Notes of C Language Identifiers are the names given tot eh entities such as variables, functions,

C programming is general-purpose high-level structured programming langue that was arrays etc.
originally developed Dennis Ritchie for the Unix operating system. Later its uses expand For example:
to application development as well as system program development. int price;
In order to accomplish any task, C-language divide the problem into smaller float rate;
modules called functions or procedure each of which handles a particular job. That is why Here, price and rate are called identifiers.
C-language is also called as the structured programming language. Format Specifier:
Some of the implementation of C language are: The format specifier is used during input and output operation. It tells the
o It is used to develop Operating System eg. UNIX. compiler what type of data is stored in a variable during the input and output
o It is used to develop popular web browser such as Mozilla Firefox, Thunderbird operation such as taking data from keyboards and display data on the screen.
etc.
o It is used in development of various GUIs (Graphical User Interface) and IDEs Data type Format Specifier Data type Format Specifier
(Integrated Development Environments). Int %d Flolat %f
o Popular Database Management System (DBMS) like MYSQL was developed by Double %1f Char %c or %s
using C.
Some of the features of C language are: Variable:
o It is a structural programming language. Like QBASIC, we break down a Those entities which holds either numeric or alphanumeric values in program
program in several small modules in C. and may change its value throughout the time of program execution are known
o It supports graphics. as variables. Each variable has to parts, a name and a data type.
o It has huge library function. Rule for writing variable:
o It is case-sensitive programming language. o Variable name should not start will number. Eg, 1age is invalid, age1 is valid
o Variable name should not have blank space. Eg, first name is invalid, firstname is
Data type: valid
C support both numeric as well as alphanumeric data. Frequently used Data o Keywords cannot be used as variable name. Eg, printf = 2; is invalid, num = 2; is
types in C are: valid
o Uppercase variable name are different from lowercase variable name. Eg, age =
Type Data type used Format Specifier 2 is different from AGE = 2
[Note: try to use relevant word as variable name without using any special symbol]
Numeric int (for non-decimal numbers) %d Operators:
The special symbol or sign used to perform some specific function or operation
Numeric float (for decimal numbers) %f are called operators.
Types of operators:
a) Arithmetic operator
Alphanumeric char (for string) %s or %c +,-,*,/,%
[Note: There are more data type which we will discuss later in need] Note that Here, if c = 5/2
The result will be 2 if we initialize ‘c’ as int c.
C Token: The result will be 2.5 if we initialize ‘c’ as float c.
C token are the basic building blocks in C language which are constructed If c = 5%2 then result will be 1. Since, % gives remainder after division.
together to write a C program. Each and every smallest individual unit in a C b) Relational operator
program are known as C tokens. > , < , >= , <= , != (not equal to) , == (equal to)
C tokens are of six types. They are : [Note: Here in C language if you want to compare the equal to is referred by ==
- Keywords (eg: auto, int, char) (two equals)]
- Identifiers (eg: price, total) c) Assignment operator
- Constants (eg: 8,9) = is an assignment operator.
- Strings(eg: “Price”, “Hello World”) [Note If a = 2 then it mean 2 is assigned to variable a, it does not mean a also
- Special symbols (eg: ( ) , { } has value 2 and both are equal.]
- Operators (eg: +,/-,*) d) Logical operator
C Keywords (reserved word): For logical AND use &&
Keyword is a set of special words which are already defined for some tasks C has For logical OR use ||
only a set of 32 keywords, which have their predefined meanings and cannot be For logical NOT use !
used s as variable name.
Header files in C
It is a file in C library with .h extension and contains several functions
Identifiers: declaration and definition. Such as we use.
#include <stdio.h> for standard input output function eg, printf, scanf etc
#include <math.h>for mathematical function eg, pow, sin, cos etc char fname[10], lname[10];
#include <string.h> for string handling function eg, strcpy(), strlen(),strupr() scanf(“%s %s”, fname, lname);
etc Since variable are initialized as array of character we don’t need to mention &
And more. [header file can be added according to requirements] while using scanf.
Sample structure of C program: Control structure in C:
Same program can be written as Since C is a structural programming language, we can change the flow of
#include<stdio.h> program execution according to the requirement of the user. Following are the
int main( ) control structure used in C.
{
// block of statements Control Structure used in C
return 0;
} Sequence Branching
Output statement:
Output in C program can be displayed by using ‘printf’ statement. Program flows from top to bottom Conditional Branching: if, if else, else
a) In order to display only character, we can use following syntax sequentially. switch
printf(“Sample text”);
This will display anything that is written inside double quotation mark (“ “)
Unconditional Branching: goto
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; Sequence: Program flows from top to bottom sequentially with out changing the
printf(“Sum is %d”, c); flow of program execution
It will display: Sum is c [Note: Don't forget to add header file in each program]
[Note that any number of format specifier and variable can be displayed]
Same program can be display as Branching:
printf(“Sum of %d and %d is %d”, a, b, c); Program flows can be changed as per the requirement of the user with or
It will display: Sum of 2 and 3 is 5 without condition.
[Note: if a, b and c was initialized as float than every %d should be replaced by Conditional Branching:
%f] Flow of program execution changes according to the condition supplied by the
Input statement: user.
Input in C program can be taken by using ‘scanf’ statement. a) if statement
In order to take input from user we can use following syntax b) if else statement
scanf(“format specifiers”, &variablename1, &variablename2, ….); c) else if ladder
Suppose we want to take a and b as input from user. To take number input we
can write a) if statement :
int a, b; If statement is used to test one or more condition and execute
scanf(“%d %d”, &a, &b); statement if the given condition is true.
In this example two numeric variable are initialized as integer. So, we write two Syntax of if statement:
%d inside double quotation followed by &variablename. & denotes address of if (condition)
that variable. {
Another example, // block of statements;
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 b) if else statement:
may be in decimal So, we write three %f inside double quotation followed by This statement will execute block of statement1 if the condition is true otherwise
&variablename. & denotes address of that variable. will execute block of statement2 if the condition is false. syntax:
If we want to take character or string as an input than first string variable if (condition)
should be initialized as follows: {
char fname[10]; // block of statements1;
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 else
[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] // block of statements2;
Example }
else
c) else if ladder: {
else if statement is used when multipath decisions are required. The general printf("You are fail");
syntax of how else if ladder is constructed in “C” programming is as follows: }
if (condition1) return 0;
{ }
block of statements1; 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
else if (condition2) greater than 100.
{ In this program example, our program will ask user to input percentage between 0 to
block of statements2; 100. If user input value greater than 100 than it will ask again for input. This is the use of
} goto statement in C.
else if (condition3)
{ Looping in C:
block of statements3; 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
else statement.
{ for loop
default statement; while loop
} do loop
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;

Important C Program example:


1) WAP to calculate the area of a rectangle in c. [Hints: a = l x b]
#include<conio.h>
Here flow of program execution will go up directly from goto to label without any #include<stdio.h>
condition. int main()
#include<conio.h> int l, b, a;
#include<stdio.h> {
int main() printf (“Enter Length: ”);
{ scanf (“%d”, &l);
float p; printf (“Enter Breadth: “);
label: scanf (“%d”, &b);
printf (“Enter percentage ”); a = l * b;
scanf (“%f”, &p); printf (“The area is %d”, a);
if (P>100) return 0;
{ }
printf("Please enter value between 0-100"); 2) WAP to calculate the simple interest in c. [Hints: i= (ptr)/100]
goto label; #include<conio.h>
} #include<stdio.h>
if (p>=40) int main()
{ {
printf (“You are Pass"); float p, t, r, i;
} printf (“Enter principal time and rate: ”);
scanf (“%f %f %f”, &p, &t, &r); else
i= (p*t*r)/100; {
printf (“The interest is %f”, i); printf (“%d is greatest", b);
return 0; }
} return 0;
3) WAP to convert days into respective years, months and days. }
#include<conio.h>
#include<stdio.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); 6) Write a program to check whether given number is odd or even.
return 0; #include<conio.h>
} #include<stdio.h>
int main()
{
int n, r ;
printf (“Enter number”);
scanf (“%d”, &n);
r = n%2;
4) WAP to enter percentage and check whether you are pass or fail. if (r ==0)
#include<conio.h> {
#include<stdio.h> printf (“%d is even", n);
int main() }
{ else
float p; {
printf (“Enter percentage ”); printf (“%d is odd", n);
scanf (“%f”, &p); }
if (p>=40) return 0;
{ }
printf (“You are Pass"); 7) Write a program to check whether given number is positive, negative or zero.
} #include<conio.h>
else #include<stdio.h>
{ int main()
printf("You are fail"); {
} int n;
return 0; printf (“Enter number”);
} scanf (“%d”, &n);
5) Write a program to find greatest among two number. if (n>0)
#include<conio.h> {
#include<stdio.h> printf (“%d is positive", n);
int main() }
{ else if (n<0)
int a,b; {
printf (“Enter two number”); printf (“%d is negative", n);
scanf (“%d %d”, &a, &b); }
if (a>b) else
{ {
printf (“%d is greatest", a); printf("%d is zero", n);
} }
return 0; else
} {
printf (“%f is Fail", p);
}
return 0;
}

10) Calculate Total electricity bill on the basis of following data.

8) Write a program to find greatest among three number.


Unit Consumed Charge per unit
#include<conio.h>
#include<stdio.h>
int main() ≤ 50 unit Rs 10/unit
{
int a, b, c; >50 and ≤100 Rs 12/unit
printf (“Enter 3 number”);
scanf (“%d %d %d”, &a, &b, &c); >100 Rs 15/unit
if (a>b && a>c)
{ #include<conio.h>
printf (“%d is greatest", a); #include<stdio.h>
} int main()
else if (b>a && b>c) {
{ float u, rs;
printf (“%d is greatest", b); printf (“Enter unit consumed”);
} scanf (“%f”, &u);
else if (u<=50)
{ {
printf (“%d is greatest", c); rs = u*10;
} printf (“Total amount is %f", rs);
return 0; }
} else if (u>50 && u<=100)
9) Write a program to input percentage and check whether he/she secure {
distinction, first division, second division, third division or fail. rs = 50*10 + (u-50)*12;
#include<conio.h> printf (“Total amount is %f", rs);
#include<stdio.h> }
int main() else
{ {
float p; rs = 50*10 + 50*12 + (u-100)*15;
printf (“Enter percentage”); printf (“Total amount is %f", rs);
scanf (“%f”, &p); }
if (p>=80) return 0;
{ }
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); 11) WAP to display first 50 natural number.
}
Using While Using for
else if (p>=40 && p<50)
#include<conio.h> #include<conio.h>
{
#include<stdio.h> #include<stdio.h>
printf (“%f is Third division", p);
int main() int main()
}
{ { }
Int c=1; Int c; printf("%d",rev);
While (c<=50) cor (c=1;c<=50;) }
{ {
Printf(%d”,c); Printf(%d”,c);
c=c+1; c=c+1;
} }
Return 0; Return 0;
} }

12) WAP to check entered number is Prime or Composite.

#include<conio.h>
#include<stdio.h>
int main()
{
int n,c=0,i;
printf("Enter any number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("%d is prime number", n);
}
else
{
printf("%d is composite no", n);
}
return 0;
}

13) WAP to enter any multidigit number and display it in reverse order.
#include<conio.h>
#include<stdio.h>
int main()
{
int n,rev,r;
printf("Enter any number:");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;

You might also like