Notes of C Language
Notes of C Language
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;
#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;