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

C Programming

This document provides an overview of the C programming language, including a brief history, key features, advantages, and basic elements like tokens, identifiers, keywords, constants, and operators. It describes the basic structure of a C program and the different types of operators used in C.

Uploaded by

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

C Programming

This document provides an overview of the C programming language, including a brief history, key features, advantages, and basic elements like tokens, identifiers, keywords, constants, and operators. It describes the basic structure of a C program and the different types of operators used in C.

Uploaded by

13 Smarty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PRO<SMMIY1ING CoN c.

EPTS

C-bMP,I.LATioN DF A C PRalRAfvl r.-


" IDw) *I
#~~.t>

{~-f{'~');
}
AsSE.M BLE R. .,
. 0

· I" #w:, DAf', Din )j, l,lA.W\, :W 0

j)) -
&f
~.(y.,
~~te~j{,U/Yy~ ~r~-
• .f'r,-v µ, IX,
· rnwwnL
-~ k ¾~ .
_a , ~ if',~ ~1
o'~ cunoL l'.s I ~ un ~ o &cr9-'-·na.nr•L,0%--
&~ :-
00100100
laj(\a,,.1~ . A ~ ?' 11001 ,010 ·
-~ - 1010101
0010100
c e,:rrc -r co DE
r

oo II oo 00100

I
OOlD I I
10 }0 11 0}0010 10 IOll
00 ~· 00 O l l l 00 · t, 0011

l
Overview of C

A brief history

C is a programming language developed at "AT & T's Bell Laboratories" of USA in 1972.
It was written by Dennis Ritchie (fig 2).

~I.
'

Fig 2. Dennis Ritchie


· The programming lc;1nguage C was first given by Kernighan and Ritchie, in a classic book
called "The C Programming Language, 1st edition". For several years the book "The C
Programming Language, 1st edition" was the standard on the C programming. In 1983 a
committee was formed by the American National Standards Institute (ANSI)
to develop a modern definition for the programming language C . In 1988 they ~elivered the final
standard definition ANSI C.

Features of C

• Portability

• Modularity

• Extensible

• Speed

• Mid-level programming language

• Flexibility

• Rich Library

Advantages of C

1. C programming is the building block for many other high level programming
languages existing today
2. AC program written in one comp~ter can easily run on another computer without
making any change

3. It has variety of data types and powerful operators

4. AC program is a collection of functions supported by the C library. So we can easily


add our own functions to C library. Hence we can extend any existing C program
according to the need of our applications

5. Since C is a structured language, we can split any big problem into several sub
modules. Collection of these modules makes up a complete program. This modular
concept makes the testing and debugging easier

Structure of a C program

I
C Tokens

C tokens, Identifiers and Keywords are the basic elements of a C program. C tokens are the
basic buildings blocks in C. Smallest individual units in a C program are the C tokens. C tokens
are of six types. They are,

1. Keywords (eg: int, while),


2. Identifiers (eg: main, total),
3. Constants (eg: 10, 20),
4. Strings (eg: "total", "hello"),
5. Special symbols (eg: (), {}),
6. Operators (eg: +, /,-,*)

1. . Keywords

Keywords are those ·words whose meaning is already defined by Compiler. Th1:y cannot be
used as Variable Names. · There are 32 Keywords in C. C Keywords are also called
as Reserved words. There are 32 keywords in C. They are given below:

auto double int struct

break else long switch

case enum register typedef

char extern return union


~--·-
const float short unsigned
-
continue for signed void

default goto sizeof · volatile

do if static while

2. Identifiers

Identifiers me tile names given to various program elements sL_ 1c~ as variables , arrays &
functions. Basically identifiers are the sequences of alphabets or d1gIts. .
Rules for forming identifier name

The first character must be an alphabet (uppercase or lowercase) or an underscore.


All succeeding characters must be letters or digits.
No space and special symbols are allowed between the identifiers.
No two successive underscores are allowed.
Keywords shouldn't be used as identifiers.
3. Constants

. _The constants refer to fixed values that the program may not change or modify during
its execution. Constants can be of any of the basic data types like an integer constant, a
floating c?nstant and a character constant. There is also a special type of constant called
enumeration constant. · ·
Eg:

Integer Constants- 45, 215u

Floating Constants- 3.14, 4513E-5L

Character Constants- \t, \n


4. Strings

A string in C is actually a one-dimensional array of characters whi<;h is terminated by


a null character '\O' .
Eg:
c.har str = {'S' 'A' 'T' 'H' 'Y' 'A' 'B' 'A' 'M' 'A'}
I I I I I I I I I

5. Special Symbols

The symbols other than alphabets, digits and white spaces for example - [] () O, ; : * ... = #
are the special symbols.

6. Operators

An Operator is a symbol that specifies an operation to be performed on the operands. The


data items that operators act upon are called operands. Operators which require two operands
are called Binary operators. Operators which require one operand are called Unary Operators.

Types of Operators

Depending upon their operation they are classified as

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise ·operators
8. Sizeof() Operators

Arithmetic Operators

Arithmetic Operators are used to perform mathematical calculations like addition


subtraction, multiplication, division and modulus. '

S.NO Operators Operation Example


1 + Addition A+B
2 - Subtraction A-B
3 * multiplication A*B
4 I Division A/8

5 % Modulus · A%B

Rules For Arithmetic Operators

1. C allows only one variable on left hand side of= eg. c=a*b is legal, but a*b=c is not
legal.
2. Arithmetic operations are performed on the ASCII values of the characters and not on
characters themselves
3. Operators must be explicitly written.
4. Operation between same type of data yields same type of data, but operation between
integer and float yields a float result.

Example Program

#include <stdio.h>
int main()
{
int m=40,n=20, add,sub,mul,div,mod;
add = m+n;
sub = m-n;
mul = m*n; .
div = m/n;
mod = m%n;
printf("Addition of m, n is : %d\n", add);
printf("Subtraction of m, n is: %d\n", sub);
printf("Multiplication of m, n is : %d\n", mul);
printf("Division of m, n is : %d\n", div);
printf("Modulus of m, n is : %d\n", mod);
}
Output

Addition of m, n is : 60
Subtraction of m, n is : 20
Multiplication of m, n is : 800
Division of m, n.is: 2
Modulus of m, n is : 0

Relational Operators

Relational Operators are used to compare two or more operands. Operands may
be variables, constants or expression

S.NO Operators Operation Example

1 > is greater m>n


than

2 < is less than rn <n

3 >= is greater m>= n


than or equal
to

4 <= is less.than m<-n


or equal to

5 -- is equal to m==n

6 != is not equal m!=n


to
Example Program

#include <stdio.h>
int main() ·
{
int m=40;n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}

Output

m and n are not equal

Logical Operators

Logical Operators are used to combine the results of two or more conditions. It is
also used to test more than one condition and make decision.

S.NO Operators Operation Example Description

1 && logical (m>5)&&(n<5) It returns true when both


AND conditions are true

2 II logical OR (m>=10)ll(n>=10) It returns true when at-


least one of the condition
is true

3 ! logical !((m>5)&&(n<5)) It reverses the state of the


NOT operand "((m>5) &&
(n<5))"

If "((m>5) && (n<5))" is


true, logical NOT operator
makes it false
Example f>rogram

#include <stdio.h>
int main()
{
int a=40,b=20,c=30;
if ((a>b )&& (a >c))
{
printf(" a is greater than band c");
}
else
if(b>c)
priritf("b is greater than a and c");
else
prinf("c is greater than a and b");
}

Output

a is greater than b and c . .

Conditional Operator

It itself checks the condition and executed the statement depending on the condition.

Syntax:

Condition? Exp1 :Exp2

Example:

X=(a>b)?a:b

The '?:' operator acts as ternary operator. It first evaluate the condition, if it is true then
exp1 is evaluated, if condition is false then exp2 is evaluated. The drawback of
Assignment operator is tliat after the ? or : only one statement can occur. ·
Example Program

#include <stdio.h>
int main()
{
int x,a=5,b=3;
x = (a>b) ? a : b;
printf("x value is %d\n", x);
}

Output

x value is 5

. Bitwise Operators

Bitwise Operators are used for manipulation of data at bit level. It operates on integer
only.

S.NO Operators Operation Example Description

1 & Bitwise AND X&Y Will give 1 only when both


inputs are 1
2 I Bitwise OR XIY Will give 1 when either of
input is 1

3 I\
Bitwise XOR XAY . Will give 1 when one input
is 1 and other is O

4 ~ 1's ~X Change all 1 to O and all 0


Complement to 1

5 << Shift left X«Y X gets multiplied by


2Ynumber of times

6 >> Shift right X>>Y X gets divided by 2r


number of times
Example Program

#include <stdio.h>
main()
{ .
int Cl=l,c2;
C2=cl«2;
printf("Left shift by 2 bits c1<<2=%d~,c2);
}

Output

Left shift by 2 bits c1 «2=4

Special operators:

sizeof () operator:

1. Sizeof operator is used to calcualte the size of data type or variables.


2. Sizeof operator will return the size in integer format.
3. Sizeof operator syntax looks more like a function but it is considered as an operator in c
programming

Example of Size of Variables

#include~stdio.h>
int main()
{
int ivar = 100;
char cvar = 'a';
float fvar = 10.10;
printf("%d", sizeof(ivar));
printf("%d", sizeof(cvar));
printf("%d", sizeof(fvar));
return 0;
}

Output:

214

In the above example we have passed a variable to size of operator. ·It will print the value of
variable using sizeof() operator.
Example of Sizeof Data Type

#include<stdio.h>
int main()
{
printf("%d", siz,eof(int));
printf("%d", sizeof(char));
printf("%d", sizeof(float));
return 0;
}

Output:_

2 14

In this case we have directly passed an data type to an sizeof.


Data Types in c

definitio~ has a c~ncept_ of 'd~ta types' which are used to define a variable before its use The
held in th:ro:~:~1~.ble will assign storage for the variable and define the type of data that ~ill be

The value of a variable can be changed any time.

C has the following basic built-in datatypes ..

• int
• float
• double
• char

The bytes occupied by each of the primary data types are

Data type Description Memory bytes Control String Example


Int Integer Quantity 2 bytes %dor%i int a=12;
Char Single Character 1 bytes %C char s='n';

float Floating Point 4 bytes %f float f=29.777

Double Double precision 8 bytes %If double d=


floating pointing 5843214
no's

Scope of a variable

A scope in any programming is a region of the program where a defined variable can have
its existence and beyond that variable cannot be accessed. There are three places where
variables can be declared in C programming language:

1. Inside a function or a block is called local variable,

2. Outside of all functions is called global variable.


3. In the definition of function parameters which is called formal parameters.

Local Variables
Variables that are declared inside a function or block are call~d local variables. They
can be used only by statements that are inside that function. Local variables are not known to
functions outside their own. Following is the example using local variables. Here all the
variables a, b und care local to main() function.

#include <stdio.h>
main ()
{

/* local variable declaration*/


int a, b, c;

/* actual initialization*/
a= 10·
. J

b = 20;
c =a+ b;
printf ("value of a= %d, b = %d and c = %d\n", a, b, c);
}

Global Variables
Global variables are defined outside of a function, usually on top of the program. The global
variables will hold their value throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is availabla for
use throughout your entire program after its declaration. Following is the example using global
and local variables:

#include <stdio.h>
/* global variable declaration*/
int g;
main ()
{
/* local variable declaration*/
int a, b;
/* actual initialization*/
a = 10;
b = 20;
g = a + b;
printf ("value of a= %d, b = %d and g = %d\n", a, b, g);
}

You might also like