0% found this document useful (0 votes)
30 views29 pages

C Language Introduction New

C is a procedural programming language developed by Dennis Ritchie in 1972, primarily for system programming like UNIX. It features portability, low-level memory access, fast speed, and clean syntax, making it foundational for many modern languages. The document covers various aspects of C, including comments, program structure, tokens, data types, variables, storage classes, and operators.

Uploaded by

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

C Language Introduction New

C is a procedural programming language developed by Dennis Ritchie in 1972, primarily for system programming like UNIX. It features portability, low-level memory access, fast speed, and clean syntax, making it foundational for many modern languages. The document covers various aspects of C, including comments, program structure, tokens, data types, variables, storage classes, and operators.

Uploaded by

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

C Language Introduction

C is a procedural programming language initially developed by Dennis Ritchie in the year


1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language
to write the UNIX operating system.
The main features of the C language include:
General Purpose and Portable
Low-level Memory Access
Fast Speed
Clean Syntax
These features make the C language suitable for system programming like an operating system or
compiler development.

Why Should We Learn C?


Many later languages have borrowed syntax/features directly or indirectly from the C
language like the syntax of Java, PHP, JavaScript, and many other languages that are mainly based on
the C language. C++ is nearly a superset of C language (Only a few programs may compile in C, but
not in C++).
So, if a person learns C programming first, it will help them to learn any modern programming
language as well.
C Comments
A comment makes the program easier to read and understand. These are the statements that are not
executed by the compiler or an interpreter.
Types of comments in C
In C there are two types of comments in C language:
 Single-line comment
 Multi-line comment
1. Single-line Comment in C
A single-line comment in C starts with ( // ) double forward slash. It extends till the end of the line and
we don’t need to specify its end.

Syntax of Single Line C Comment


// This is a single line comment

2. Multi-line Comment in C
The Multi-line comment in C starts with a forward slash and asterisk ( /* ) and ends with an asterisk
and forward slash ( */ ). Any text between /* and */ is treated as a comment and is ignored by the
compiler.
It can apply comments to multiple lines in the program.
Syntax of Multi-Line C Comment
/*Comment starts
continues
continues
.
.
Comment ends*/
-------------------------------------------------------------------------------------------------------------------------
https://www.geeksforgeeks.org/tokens-in-c/?ref=lbp

Structure of a ‘C’ Program


A ‘C’ program may contain one or more sections below.
documentation section
preprocessor section
definition section
global declaration section
void main()
{
declaration part;
execution part;
}
sub program section
{
body of the subprogram;
}
Simple Addition Program

/* Addition of two numbers*/


#include <stdio.h>
void main()
{
int a=5, b=8, c;
c=a+b;
printf(“%d”,c);
}

Output:
Addition of a and b is …13

1. Documentation Section
It consists a set of comment lines used to specify the name of program, the author and
other details etc.,

2. Comments
Comments are very helpful in identifying the program features and under lying logic
of the program. The lines begins with ‘/*’ and ending with ‘*/’are known as comment lines.
These are not executable, the compiler is ignored any thing between /* and */

3.Preprocessor Section
It is used to link system library files, for defining the macros and for defining conditional
inclusion.
Eg:
#define pi 3.24
#if def
#endif

4. Global declaration section


The variables that are used in more than one function throughout the program are
called global variables and declared outside of all the function i.e., before main( ).
5. Declaration part
This part is used to declare all the variables that are used in the executable part of the
program and these are called local variables.

6. Executable part
It contains atleast one valid statement .
The execution of the program begins with open brace ‘{‘ and ends with closing brace
‘}’.

Note:
All the statements in the program ends with semicolon except conditional and control
statements.
----------------------------------------------------------------------------------------------------------------

Tokens in C
A token in C can be defined as the smallest individual element of the C programming language that
is meaningful to the compiler. It is the basic component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions they are used to
perform. The types of C tokens are as follows:

1.Identifiers
2.Keywords
3.Constants
4.Strings
5.Special Symbols
6.Operators
Identifiers
Identifiers are name given to various program elements such as variable, functions and array
etc.,
Rules for naming an Identifiers
 Identifiers consist of letters and digits in any order.
 The first character must be letter/character or may begin with underscore(_).
 The underscore ‘_’ can also be used and considered as a letter.
 An identifier can be any length but C compiler recognizes only the first 31 characters.
 No space and special symbols are allowed between the identifier.
 The identifier can not be a keyword.

Some valid identifiers are


STDNAME, SUB, TOT_MARKS_TEMP, Y2K.

Some invalid identifiers are


Return, $stay, 1RECORD, STD NAME
----------------------------------------------------------------------------------------------------------------------

KEYWORDS (32)
 Reserved words are called Keywords, that have standard and predefined meaning.
 Which cannot be changed and they are the basic building blocks for program statements.
 All keywords must be written in lower case.

auto break case char const continue default do double enum else
extern float int signed unsigned struct for long short
switch void goto return static typedef volitile
if register sizeof union while

---------------------------------------------------------------------------------------------------------------------------
DATA TYPES
 Data types is the type of data, that we are going to access within the program.
 Each data type may have predefined memory requirement and storage representation
 C supports 4 classes of Data types

C Data Types

Primary UserDefined Derived Empty

1) char 1) typedef 1) arrays 1) void


2) int 2) enum 2) pointer
3) float 3) structures
4) double 4) union

The bytes occupied by each of the primary data types are

Data type Description Memory Requirements Range

int Integer Quantity 2 bytes -32768 to 32767

char Single Character 1 byte -128 to 127

float Floating Point 4 bytes 3.4E-38 to 3.4E+38


Number( decimal
point and exponent)

double Double Precision 8 bytes 1.7E-308 to 1.7E+308


Floating point number
( exponent which may
be larger than
magnitude)

All C compilers supports the five fundamental data types called int, char, float, double, and void.
The Primary data types are divided into the following.

INTEGER TYPE
(16 or 32 bits)

Signed Unsigned

Int - 2 bytes unsigned int - 2 bytes

short int - 1 byte unsigned short int - 1 byte

long int - 4 bytes unsigned long int - 4 bytes


CHARACTER TYPE
( 8 bits )

Char- 1 byte signed char- 1 byte unsigned char - 1 byte

FLOAT TYPE
(32 bits with 6 digits of precision)

Float - 4 bytes Double - 8 bytes Long double -10 bytes

EMPTY DATA TYPE


( Null Data type )

void -is the null data type , generally specified with the function which has no arguments

---------------------------------------------------------------------------------------------------------------------------

Constants
The values of the variable name can not be changed during the execution of the program.
CONSTANTS
NUMERIC CHARACTER
1) Integer 1) Character
2) Real 2) String

1) Numeric constant
A) Interger Constant:
It is formed with sequence of digit.
Example:
Marks=98
Percentage=85
Rules for defining Integer Constant
 It must have atleast one digit.
 Decimal point is not allowed
 It can be either positive or negative
 No comma or blank spaces are allowed
 The range limit will be -32768 to +32767

B) Real constants
 A real constant formed with the sequence of numeric digits with decimal point.

 It is used to represent quantities that varies continuously such as distance, heights, temperature
etc.,

Eample:

Distance=126.4

Height=3.9

Speed=3e11

Rules for defining the real constant


 The real constant atleast have one digit.

 Real constant must have one decimal point

 It can be either positive or negative

 No comma or blank space are allowed

2) Character Constant
A) Character Constant
The character constant contains a single character enclosed within a pair of single inverted comma
both pointing to the left.

Eg: ‘s’ ‘M’ ‘3’ ‘_’

The character constant ‘5’ is different from the number 5


B) String Constants
 A string constant is a sequence of characters enclosed in double quotes.

 The characters may be letters, numbers, special characters and blank space etc.,

 At the end of the string ‘\0’ is automatically placed.

Eg:
“HI”, “CSE”, “32.66” , “30”
---------------------------------------------------------------------------------------------------------------------------

Variables
 Variable is an identifier that is used to represent some specified type of information within a
designated portion of the program.

 Variable can take different values at different times during the execution. ( It is named as memory
location).

Rules for naming the variables

1.Variable name can be any combination of 1 to 8 alphabets, digits and underscore.

2.The first character must be an alphabet or an underscore ( _ ).

3.Length of the variable name can not exceed up to 8 characters long, some C compiler accepts 32
characters long.

4.No comma or blank space or special symbols are allowed with in a variable name.

Variable Declaration

It tells the compiler what the variable name and type of the data that the variable will hold.

Syntax:

data_type v1,v2,v3,……………..,vn;
where data_type is the type of the data
v1,v2,v3,….vn are the list of variables
Eg: int code
float price
char name[10]
Here code is the type of integer, price is the type of float and name[10] is defined as an
array of character.

User Defined Variables


a) Type declaration: It allows users to define an identifier that would represents an
existing data type and this can later be used to declare variables.

Syntax: typedef data_type identifier;


Where typedef is the user defined type declaration

data_type is the existing data type

identifier is the identifier refers to the new name given to the data type.

Eg:

typedef int marks;

marks m1, m2, m3;

Here marks is the type of int and this can be later used to declare variables.

b) Enumerated data type: The C language provides another user defined data type is
called enumerated data type.

Syntax:

enum identifier { value 1, value 2, ……, value n};

where identifier is the user defined enumerated data type.

value 1, value 2, ……..value n are the enumeration constants

Eg 1:
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

In this example, Sun will be 0, Mon will be 1 and so on.

Note: the compiler assigns integer values from 0 to all the enumeration constants
automatically.

Eg 2:

enum Level {
LOW = 5,
MEDIUM, // Now 6
HIGH // Now 7
};

Scope of variables
Scope of a variable implies the availability of variables within the program. Variables have
two types of scopes described below.

i) Local variables:

The variables which are defined inside a function block or inside a compound
statement of a function sub-program are called local variables.

Eg:
function( )
{
int i, j;
/* body of the function */
}

The integer variables i, j are defined inside the function block of function( ).. Hence I,
j are called local variables.
ii) Global / External variable:
The variables that are declared before the main( ) are called the global/external
variables.

Eg.

int a=5, b=2;

main( ) fun ( )

{ {

.…. int sum;

fun( ); sum= a + b;

} }

The integer variables a,b are global / external variables, as they are declared before the
main ( ). these variables are used later in the fun( ).

----------------------------------------------------------------------------------------------------------------

Storage Classes
 In C language through declaration statements memory is allocated temporarily for all the
variables defined.

 The size of the allocated memory varies with respect to the type of variable.

 C language supports 4 types of storage classes and these are used to specify the scope of
different variables defined with in function blocks and programs.

 The 4 types of storage class specifications are

1) auto (automatic)

2) static

3) extern

4) register
1) auto

 Memory space is automatically allocated as the variable is declared.

 These variables are given only temporary memory space.

 After the execution all the automatic variables will get disposed.

Syntax:

storage_class_type data_type var1, var2, ………var n;


Eg:

auto int a, b; (same as int a,b)

auto float x, y; (same as float x, y)

auto char sex; (same as char sex)

2) static

 The static variables are the variables for which the contents of the variables will be
retained through out the program.

 These are permanent within the function in which they are declared.

Syntax:

Storage_class type data_type var1, var2,….varn;

Eg;

static int a, b;

static float x,y;

static char sex;

3) extern:
 The external variables are declared out of the main() function.

 The availability of these variables are through out the program and that is both in

main program and inside the user defined functions.

Syntax:

Storage_class_type data_type var1, var2,….varn;


Eg;

extern int a, b;

extern float x,y;

extern char sex;

4) Register

 Registers are special storage class which resides within a Central Processing Unit.

 The actual arithmetic and logical operations are carried out within these register.

Syntax:

Storage_class type data_type var1, var2,….varn;

Eg;

register int a, b;

-> The above declaration specifies that the variables a, b will be integer variables
with storage class register.Hence the values of a,b will be stored within the registers of
computer CPU rather than memory.

The following table illustrates the different features of storage classes.

Features static auto extern register


Storage Memory Memory Memory CPU register

Default value Zero Garbage value Zero Garbage value

Scope Local Local Global Local

Delimeters
 These are the symbols, which have some syntactic( grammatical arrangement) meaning.

 These symbols will not specify any operations.

C language delimeters list is given below.

Symbol Name Meaning

# Slash Pre-processor Directive

, Comma Variable delimeters to separate list of variables

: Colon Label Delimeter

; Semicolon Statement Delimeter

() Parenthesis Used in expressions or in functions

{} Curly braces Used for blocking ‘C’ structures

[] Square braces Used along with arrays

----------------------------------------------------------------------------------------------------------------

OPERATORS AND EXPRESSION


OPERATORS

 An operator is a symbol that specifies an operation to be performed on the operands.

 The data items that operators acts upon are called operands.

 Some operators require two operands called binary operators, while other acts upon
only one operand called unary operator.
Eg:

a+b

where ‘+; is Operator and ‘a’, ‘b’ are the Operands.

Types of operators

‘C’ provides a rich set 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) Special operators

1. Arithmetic Operators

‘C’ allows to carry out basic Arithmetic operations like addition, subtraction,
multiplication and division.

The following table shows the Arithmetic Operators and their meaning.

Operator Meaning

+ Addition

- Subtraction
* Multiplication

/ Division

% Modulo Division

All the above operators are called ‘Binary’ operators as they act upon two operands
at a time.

Eg:

main( )

int a=10, b=3,c;

c=a%b;

printf(“The output will be:%d\n”,c);

OUTPUT

The output will be: 1

Operation Result Example

int/int int 2/5=0

real/int real 5.0/2=2.5

int /real real 5/2.0=2.5

real/real real 5.0/2.0=2.5


Arithmetic operators can be classified as

i) Unary Arithmetic

It requires only one operand.

Eg: +x, -y

ii) Binary Arithmetic

It requires two operands.

Eg: a+b, a-b, a/b, a*b, a%b.

iii) Integer Arithmetic

It requires both operands whose values are integer values for arithmetic operation.

Eg: a=5, b=4

Expression Result

a+b 9

a-b 1

iv) Floating Point Arithmetic

It requires both operands which are float type for arithmetic operation.

Eg: a=6.5, b=3.5

Expression Result

a+b 10.0

a-b 3.0

2. Relational Operators
 Relational operators are used to compare two or more operands.

 Operands may be variables, constants or expression.

The following table shows the Relational Operators.

Operators Meaning

< is less than

<= is less than or equal to

> is greater than

>= is greator than or equal to

== is equal to

!= is not equal to

Syntax:

AE1 relational operator AE2


where AE1, AE2 are constants or an expression variable.

Suppose the ‘a’, ‘b’ and ‘c’ are integer variables, whose values are 1, 2, 3 respectively.

Expression Interpretation Value

a<b True 1

(a+b)>=c True 1

(b+c)>(a+5) False 0

c!=3 False 0

b= =2 True 1

 The value of relational expression is either one or zero.

 Relational operators are used in decision making process.


 They are generally used in conditional and control statements

3. Logical Operators

Logical operators are used to combine the results of two or more conditions or
expressions.

Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT

&& - This operator returns true if both operands are non-zero (true).

Eg: (exp1)&&(exp2)

|| - This operator returns true if at least one of the operands is non-zero (true).

Eg: (exp1) || (exp2)

! - This operator reverses (inverts) the truth value of its operand. If the operand is non-zero
(true), it returns false, and vice versa.

Eg: !(exp1)

Eg:

‘i’ is an integer variable, whose value is 7

‘f’ is a float variable, whose value is 5.5

‘c’ is a character variable that represents a character ‘w’.

Expression Interpretation Value

(i >= 6) && (c = = ‘w’) True 1

(f < 11) && (i >100) False 0


(c != ’p’) || (i<=100) True 1

4. Assignment Operator

 Assignment operator used to assign a value or expression or a value of a variable to


another variable.

 Assignment operator can be either Compound Assignment or Multiple Assignment.

Syntax:

Variable = expression (or)value;


Eg:

x=10;

x=a+b;

x=y;

i) Compound Assignment

It is used to assign a value to a variable in order to assign a new value to a variable


after performing a specified operation.

Some of the compound Assignment operators and their meaning are given in the table.

Operator Example Meaning

+= x += y x=x+y

-= x -= y x=x-y

*= x *= y x=x*y

/+ x /= y x=x/y

%= x %= y x=x%y
ii) Nested (or) Multiple Assignments

It can assign a single value or an expression to multiple variables.

Syntax:

Var1=var2=var3=…..=varn=single variable or
expression;
Eg:

i = j = k = 1;

x = y = z = (i + j + k);

5. Increment and Decrement Operators (Unary Operators)

C has two way useful operators , these are increment ( ++ ) and decrement (-- )
operators.

The ‘ ++ ‘ adds one to the variables and ‘ -- ‘ subtracts one from the variable.

Operator Meaning

++ x Pre increment

--x Pre decrement

x++ Post increment

x-- Post decrement

Eg:

main( )

{
int a=10, b=10;

printf(“ a++=%d\n)”, a++);

printf(“ ++b=%d\n)”, ++b);

printf(“ --a=%d\n)”, --a);

printf(“ b--=%d\n)”, b--);

Output:

a ++ = 10

++b = 11

--a = 10

b-- = 11

Where

a ++; Post increment, first do the operation and then increment.

++ b ; Pre increment, first increment and then do the operation.

-- a; Pre decrement, first decrements and then do the operation.

b -- Post decrement, first do the operation and then decrement.

Note: Do not use increment and decrement to floating point variables.

6. Conditional Operator (or )Ternary Operator

Conditional operator itself checks the condition and executes the statement depending
on the condition.

Syntax:

Condition ? exp1 : exp2;


The ‘ ? ‘ operator acts as a ternary operator, it first evaluates the condition, if it is
true (non zero) then the ‘exp1’ is evaluated, if the condition is false (zero) then the ‘exp2 is
evaluated.

Eg:

main()

int a=5, b=3, big;

big = a>b ? a : b;

printf(“Big is %d”, big);

Output

Big is ……5

In the above case, it checks the condition ‘a>b’ if it is true, then the value of ‘a’ is stored in
‘big’ otherwise the value of ‘b’ is stored in ‘big’.

7. Bitwise Operator

 Bitwise operators are used to manipulate the data at bit level.

 It operates on integers only.

The operators and its meaning are given below.

Operator Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise XOR
<< Shift left

>> Shift right

~ One’s complement

Bitwise AND (&):

This operator is represented as ‘&’ and operates on two operands of integer type.

While operating upon these two operands they are compared on a bit by bit basis.
(Both the operands must be of same type).

Truth table for ‘&’ is shown below.

& 0 1

0 0 0

1 0 1

Eg:

x=7 0000 0111

y=8 0000 1000

x&y 0000 0000

Bitwise OR ( | ):

This operator gives if either of the operand bit is ‘1’ then result is ‘1’ or both operands
are 1’s then also given ‘1’.

Truth table for ‘|’ is shown below.

| 0 1

0 0 1

1 1 1
Eg: x=7 0000 0111

y=8 0000 1000

x|y 0000 1111

Bitwise Exclusive OR( ^ ):

Here integer gives if either of the operand bit is high (1) then it gives high (1)result.

If both operand bits are same then it gives low ( 0 )result.

Truth table for ‘^’ is shown below.

^ 0 1

0 0 1

1 1 0

Eg: x = 13 0000 1101

y=8 0000 1000

x^y 0000 0101

One’s Complement

 The 1's complement is used to represent the negative of a given binary number.
 To find the 1's complement of a binary number by changing all the 0s to 1s and all the 1s
to 0s in the number.

Eg: x = 14 1110

~x = 1 0001The output will be:

8. The Special Operator


The ‘C’ language supports some of the special operators given below.

Operators Meaning

, Comma Operator (used to separate the statement elements such as


variables, constants or expression etc., )

Sizeof Sizeof operator (It is a unary operator that returns the length in
bytesof the specified variable.

&, * Pointer operator

. --> Member Access (Selcetion) Operator

Eg:

main()

int a;

printf(“%d”, sizeof(a));

Expressions
An expression represents data item such as variables, constants and are interconnected
with operators. An expression is evaluated using assignment operator.

Syntax:

Variable=expression;

Eg: x=a*b-c;

In the above statement the expression is evaluated first from left to right. After the
evaluation of the expression the final value is assigned to the variable from right to left.
Arithmetic Operators Precedence

Usually the Arithmetic Operators are evaluated from the left to right using the
precedence of operators when the expression is written the parameters.

Arithmetic operator precedence

Precedence Operators

High * / %

Low + -

The Arithmetic expression evaluation is carried out using the two phases from the left to right
through the expression.

First phase: The highest priority operators are evaluated in the expression.

Second Phase: the lowest priority operators are evaluated in the expression.

Eg:

result = x - y / 3 + z * 3 - 1

where x = 3, y = 9, z = 77

result = 3 - 9 / 3 + 77 * 3 - 1

First phase:

result = 3 - 3 + 77 * 3 - 1

result = 3 - 3 + 231 - 1

Second phase:

result = 0 + 231 - 1

result = 231 - 1

result = 230
However, during the evaluation of expression, the order of evaluation can be changed by
using the parenthesis in an expression.

Eg:

( 3 - 9 ) / 3 + 77 * ( 3 - 1 )

= - 6 / 3 + 77 * ( 3 - 1 )

= - 6 / 3 + 77 * 2

= - 2 + 154

= 152

Rules for evaluation of expression

1) Evaluate the subexpressions from left to right if parenthesized

2) Evaluate the arithmetic expression from left to right using the rules of precedence.

3) The highest precedence is given to the expressions with in parenthesis.

4) Apply associative rule if more operators of the same precedence occurs.

5) Evaluate the inner most sub expression if the parenthesis are nested.

The associative property in mathematics refers to the property of some binary operations
where rearranging the parentheses in an expression does not change the result 12345.
Specifically:

 In addition: (a + (b + c) = (a + b) + c)
 In multiplication: (a(bc) = (ab)c)

You might also like