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

test1 (1)

The document provides an overview of the C programming language, highlighting its importance as a robust, middle-level language suitable for system software and business applications. It covers the structure of C programs, including syntax, execution steps, constants, variables, data types, and the process of assigning values to variables. Additionally, it explains the use of keywords, identifiers, and the declaration of variables, emphasizing the significance of data types in C programming.

Uploaded by

govindanm223
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)
8 views

test1 (1)

The document provides an overview of the C programming language, highlighting its importance as a robust, middle-level language suitable for system software and business applications. It covers the structure of C programs, including syntax, execution steps, constants, variables, data types, and the process of assigning values to variables. Additionally, it explains the use of keywords, identifiers, and the declaration of variables, emphasizing the significance of data types in C programming.

Uploaded by

govindanm223
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/ 25

Unit I

1.1 Overview of C:
1.1.1. Importance of C

 C is called as a robust language, which has so many built-in functions and operations,
which can be used to write any complex program.
 Generally, we use to call C as a middle level language. Because, the ‘C’ compiler
combines the capabilities of an assembly language with the features of a high-level
language. Therefore, it is best for writing both system software and business packages.
 ‘C’ Programs are efficient and fast.
 C is highly portable, that is, ‘C’ programs written on one computer can be run on another
with little (or) no modification.
 ‘C’ language is best for structured programming, where the user can think of a problem in
terms of function modules (or) blocks.
 It has the ability to extend itself.
 It was named ‘C’ because it is an offspring of BCPL (Basic Combined Programming
Language) which was popularly called ‘B’ language.

1.1.2. Sample C program

/* Simple program to add two numbers…………………….*/

#include <stdio.h>

int main (void)

{ int v1, v2, sum; //v1,v2,sum are variables and int is


data type

v1 = 150;

v2 = 25;
Output:

The sum of 150 and 25 is=175

1.1.3. C program structure

Syntax

1) Comment line

2) Preprocessor directive

3) Global variable declaration

4) main function( )

Local variables;

Statements;

User defined function

{
Statements;

Comment line

It indicates the purpose of the program. It is represented as follows

1. //Single Line
2. /*…………Multiple Lines…………..*/
Comment line is used for increasing the readability of the program. It is
useful in explaining the program and generally used for documentation. It is
enclosed within the decimeters. Comment line can be single or multiple lines
but should not be nested. It can be anywhere in the program except inside
string constant & character constant.

Preprocessor Directive:

 #include<stdio.h> tells the compiler to include information about the


standardinput/output library.
 It is also used in symbolic constant such as #define PI3.14(value).
 The stdio.h (standard input output header file) contains
definition&declaration of system defined function such as printf( ),
scanf( ), pow( ) etc.
 Generally printf() function used to display and scanf() function used to
read value

Global Declaration:

This is the section where variable are declared globally so that it can
be access byall the functions used in the program. And it is generally
declared outside the function.

main() function
It is the user defined function and every function has one main()
function fromwhere actually program is started and it is encloses within the
pair of curly braces.The main( ) function can be anywhere in the program but
in general practice it isplaced in the first position.

Syntax :

main()

……..

……..

……..

The main( ) function return value when it declared by data type as

int main( )

return 0

The main function does not return any value when void (means null/empty)
asvoid

main(void ) or void main()

printf (“C language”);

}
Output: C language

The program execution start with opening braces and end with closing
brace.And in between the two braces declaration part as well as executable
part ismentioned. And at the end of each line, the semi-colon is given which
indicatesstatement termination.

1.1.4. Executing C program

Let us look at the image syntax of the steps of execution of C program.

C code:

When you first write the code in the C language, that source code is
sent to the Preprocessing section.

Preprocessing:

 In this section our source code is attached to the preprocessor file.


 Different types of header files are used like the studio.h, math.h, etc.
 Our C source code is attached to these types of files and the final C
Source generates.
 Some of the preprocessor directives are #include, #define.
 After generating the preprocessed C source code, the C source code is
sent to the compiler section.
Compiler:

 The preprocessed source code moves to the compiler, and an


assembly-level code is generated by the compiler after the compilation
of the whole C source code program.
 All the different files which have the C program must be saved with the
.c extension.
 For the compiler to understand whether the program file is a C
program file or not, it is necessary to have the '.c' extension.
 Now, when the file is compiled, the first thing the C compiler does is to
search for any error.
 If there is no error, the C compiler will report for no error, after that the
compiler will store the file as a .obj file of the same name, which is
termed as the object file.
 Although this .obj file will not be executable. After the compilation, the
process is continued by the assembler section.
Assembler:

 This part usually generates the Object code, after taking the assembly-
level code from the compiler.
 This object code is quite similar to the machine code or the set of
binary digits.
 After this assembler part, The Linker continues the process, producing
an executable.exe file at the end.
Linker:

 Before getting started with this, we should know that the library
functions are a part of the C software but not of any C program.
 Hence, the compiler has no idea about the working of the function,
whether it is a printf function or scanf function.
 The information for each of these functions is kept in the
corresponding library, which the compiler must to be able to connect.
The linker does this task.
 So, when the #include is written, it includes the studio.h library, which
is basically used for giving access to the Standard Output and Input.
 The basic goal of the linker is to link the object file to the library
functions so that the programme may be run as an executable file
(.exe).
 In this Linker process, the .exe file will be created and this file is in an
executable format.
 After this process, the next step is the loader process.
Loader:

 Whenever the command is given for the execution of a particular


program, The loader plays an important role.
 With the help of the loader, the .exe file is loaded in the RAM and the
CPU is informed of the starting point of the address of the program
where it is loaded.
 Instruction Register: The current instructions which are executed by
the CPU, is held by this.
 Program Counter: The address of the next instructions which is to be
executed by the CPU is held by this.
 Accumulator: The accumulators usually store all the information
which are related to the calculations.
 The first instruction is informed by the loader to the Program Counter,
and after that, the execution is initiated, and the Program Counter
handles the task.
1.2. Constants, Variables, and Data Types:
1.2.1.Character set

A character denotes any alphabet, digit or special symbol used to


represent information. Valid alphabets, numbers and special symbols
allowed in C are

1.2.2. C tokens

The smallest individual units are known as tokens. C has six types of
tokens as shown in figure

Special symbols [ ] {}

Keywords float, while

Constants 15.0,100

Identifiers main, amount

Strings “ABC”, “YEAR”

Operators +, - , *
1.2.3. Keyword & identifiers

Keyword

There are certain words reserved for doing specific task, these words
are known as reserved word or keywords.

These words are predefined and always written in lower case or small
letter. These keywords can’t be used as a variable name as it assigned with
fixed meaning.

Some examples are int, short, signed, unsigned, default, volatile, float,
long, double, break, continue, typedef, static, do, for, union, return, while,
do, extern, register, enum, case, goto, struct, char, auto, const etc.

Identifiers

Identifiers are user defined word used to name of entities like


variables, arrays, functions, structures etc.

Rules for naming identifiers are:

 Name should only consist of alphabets (both upper and lower case),
digits and underscore (_) sign.
 First characters should be alphabet or underscore
 Name should not be a keyword
 Since C is a case sensitive, the upper case and lower case considered
differently, for example code, Code, CODE etc. are different identifiers.
 Identifiers are generally given in some meaningful name
 An identifier name may be long, some implementation recognizes only
first eight characters, most recognize 31 characters.
1.2.4. Constants

Constant is an any value that cannot be changed during program


execution. In C, any number, single character, or character string is known
as a constant.

These constants are categorized as

 Numeric constant
 Character constant
 String constant
Numeric constant:

 Numeric constant consists of digits.


 It required minimum size of 2 bytes and max 4 bytes.
 It may be positive or negative but by default sign is always positive.
 No comma or space is allowed within the numeric constant and it must
have at least 1 digit.
 The allowable range for integer constants is -32768 to 32767.
 It is categorized as
o Integer constant
o Real constant
Integer constants

 It is a whole number which has no decimal point.


 Types of integer constants are:
o Decimal constant: 0-------9(base 10), The first digit should not be
zero
o Octal constant: 0-------7(base 8), The first digit should be zero
o Hexa decimal constant: 0----9, A------F(base 16), The first two
digit should be 0x/ 0X
Real constant
 It is also called floating point constant.
 Rules are
o Real constant must have at least one digit.
o It must have a decimal point.
o It could be either positive or negative.
o Default sign is positive.
o No commas or blanks are allowed within a real constant. Ex.:
+325.34
Character constant

 It is represented as a single character enclosed within a single quote.


 These can be single digit, single special symbol or white spaces such
as ‘9’,’c’,’$’,‘ ’ etc.
 Every character constant has a unique integer like value in machine’s
character code as if machine using ASCII
o A------------ Z ASCII value (65-90)
o a-------------z ASCII value (97-122)
o 0-------------9 ASCII value (48-59)
o ; ASCII value (59)
String constant

 Set of characters are called string and when sequence of characters


are enclosed within a double quote (it may be combination of all kind
of symbols) is a string constant.
 String constant has zero, one or more than one character and at the
end of the string null character(\0) is automatically placed by compiler.
 Some examples are “,sarathina” , “908”, “3”,” ”, “A” etc. I

1.2.5. Variables
 Variable is a data name which is used to store some data value or
symbolic names for storing program computations and results.
 The value of the variable can be change during the execution.
 The rule for naming the variables is same as the naming identifier.
 Before used in the program it must be declared.
 Declaration of variables specifies its name, data types and range of the
value that variables can store depends upon its data types. Syntax: int
a; char c; float f;

1.2.6. Data types

Data types refer to an extensive system used for declaring variables or


functions of different types before its use. The type of a variable determines
how much space it occupies in storage and how the bit pattern stored is
interpreted.

C has the following 4 types of data types

 Basic built-in data types: int, float, double, char


 Enumeration data type: enum
 Derived data type: pointer, array, structure, union
 Void data type: void
There are two types of type qualifier in c

 Size qualifier: short, long


 Sign qualifier: signed, unsigned
1.2.7. Declarations of variables

Declaration does two things

 It tells the compiler what the variable name is.


 It specifies what type of data the variable will hold.
The declaration of variable must be done before they are used in the
program.

Syntax

data_typevariable_name;
Example:

int x, y, z; char flat, ch;

1.2.8. Assigning values to variables


A variable is a data item whose value can vary. The value of a variable changes
according to the instructions executed. During the execution of a program, the value of a variable
might keep changing.Assigning a value is a process that defines the value of a variable or a
constant.
“Assigning a value to a variable” means writing a value to the variable.
Syntax:
Data_typevar_name = value;
This process involves four entities:
1. A data type
2. A variable
3. The simple assignment operator (=)
4. The value that will be copied to the variable
A typical example of assigning a value to a variable is
int a = 4;
Where:
 “int” is the data type
 “a” is the variable
 "=" is the operator
 “4” is the value

1.2.9. Assignment statement

 C programming provides an assignment operator to assign values to


variable, assigning the value to a variable using assignment operator is
known as an assignment statement.
 The function of this operator is to assign the values or values in
variables on right hand side of an expression to variables on the left
hand side.
Syntax

Variable_name = constant / variable/ expression;


The data type of the variable on left hand side should match the data type
ofconstant/variable/expression on right hand side with a few exceptions
where automatic type conversions are possible.

Examples

b = c ; /* b is assigned the value of c */

a = 9 ; /* a is assigned the value 9*/

b = c+5; /* b is assigned the value of expr c+5 */

The expression on the right hand side of the assignment statement can be:

 An arithmetic expression
 A relational expression
 A logical expression
 A mixed expression.
The above mentioned expressions are different in terms of the type of
operators connecting the variables and constants on the right hand side of
the variable. Arithmetic operators, relational

Examples

int a;

floatb,c ,avg, t;

avg = (b+c) / 2; /*arithmetic expression */

a = b && c; /*logical expression*/

a = (b+c) && (b<c); /* mixed expression*/

1.2.10. Declaring a variable as constant


Variables can be declared as constant using

 The const keyword or


 The #define preprocessor directive.
The const keyword

 Variables can be declared as constants by using the “const” keyword


before the datatype of the variable.
 The constant variables can be initialized once only.
 The default value of constant variables is zero.
Example

#include <stdio.h>

int main()

constint a;

constint b = 12;

printf("The default value of variable a : %d", a);

Output

The default value of variable a : 0

The value of variable b : 12

The #define preprocessor directive


Variables can be declared as constants by using the #define preprocessor directive as it declares
an alias for any value.
Example

#include <stdio.h>

#define num 25

int main()

printf("The value of num is: %d", num);

Output

The value of num is: 25

1.2.11. Declaring a variable as volatile

Volatile is a qualifier that is applied to a variable when it is declared. It


tells the compiler that the value of the variable may change at any time-
without any action being taken by the code the compiler finds nearby.

Syntax

volatile data type var_name;

data type volatile var_name;


 To declare a variable volatile, include the keyword volatile before or after
the data type in the variable definition.
 The volatile keyword can be applied to variables of any type,
including int, float, double, and even struct and union types.
 When a variable is declared volatile, the compiler generates code that
reads its value from memory each time it is accessed. It ensures that the
variable's current value is always used, even if external factors have
changed it.
 The volatile keyword is often used in embedded systems
programming, where hardware devices may modify the value of
memory-mapped registers at any time.
 In multi-threaded programming, the use of volatile is often combined
with other synchronization techniques, such as locks or semaphores, to
ensure that data is accessed safely and consistently by multiple threads.
 The use of volatile can have a significant impact on performance, as it
prevents the compiler from performing certain optimizations, such as
caching the value of a variable in a register. Therefore, it is important to
use volatile only when necessary.

1.3. Operators and Expressions


1.3.1. Operators

An operator is a symbol that tells a computer to perform certain


mathematical or logical manipulations. C operators can be classified in to a
number of categories.

 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and decrement operators
 Conditional operators
 Bitwise operators
 Special operators
Arithmetic Operator

C provides the entire basic arithmetic operator. There are two types

 Binary operators
 Unary operators
Binary operators

It performs numeric calculations between two values

Binary arithmetic Example


operator s

+ 2+2=4

- 5-3=2

/ 10/2=5

% 11%3=2

* 2*3=6

Unary operators

Unary operators are increment operator (++), decrement operator(--)


and

minus(-)

Operator Description

- Minus
++ Increment

-- Decrement

& Address operator

Sizeof Gives the size of operator

Relational Operators

Relational operators are symbols that are used to test the relationship
between two variables or between a variable and a constant. These
operators are used for checking conditions in control statements. The table
shows the various relational operators and their meaning.

Operator Name Example Description

== Equal operator a = = b; True if a equals b else False

True if a does not equal b else


!= Not Equal operator a ! = b;
False

< Less than operator a < b; True if a less than b else False

> Greater than operator a > b; True if a greater than b else False

Logical Operators

Operators, which are used to combine two or more relational


expressions, are called as logical operators. These operators are used to test
more than one condition at a time. The table shows the various logical
operators and their meaning

Operator Symbol Example

AND && exp1 &&


exp2

OR || exp1 || exp2

NOT ! ! exp1

Assignment Operators

Assignment operator is the most common operator almost used with


all programming languages. It is represented by “=” symbol in C which is
used to assign a value to a variable lying to the left side of the assignment
operator.

But if the value already exists in that variable then it will be overwritten by
the assignment operator(=)

Syntax

<variable> = <expression>;

Example:

X = y = z = 2;

X = (y + z);

Increment and decrement operators

C allows 2 very useful operators not generally found in other


languages. These are the increment and decrement operators ++ and -- The
operator ++ adds 1 to the operand, while – subtracts 1. Both are unary
operators and take the following form

Symbo
Operator Action Examples
l
Incremen Increments the operand by
++ ++x, x++
t one

Decreme Decrements the operand by


-- --x, x--
nt one

Conditional Operators

C supports conditional operator that is known as the ternary operator


“?:”

It is basically used as an short hand for simple if..else

Boolean expression ?operand 1 : operand 2;

The ?operator evaluates an expression which may also be an operand and


returns operand1 if the expression is true; otherwise returns operand2, if the
expression is false. Syntax

Expression ? op1: op2

Example:

int a=14, b=20;

int x=(a>b) ? a b;

In the above example the value of x is assigned the value of b since the
condition is false.

Bitwise Operators

Bitwise operators operate on individual bits of integer values. If


an operand is shorter than an int, it is promoted to int before doing the
operations.It helps to know how integers are represented in binary. The
following table lists bitwise operators.
Exampl
OperatorName Result Description
e

A& b And 3&5 1 1 if both bits are 1

A|b Or 3|5 7 1 if either bit is 1

A^b Xor 3^5 6 1 if both bits are different

~a Not ~3 -4 Inverts the bits

Shifts the bits of n left p positions. Zero


Left
n << p 3<<2 12 bits are

Shift shifted in to the lower order positions

Right Shifts the bits of n right p positions.If n is a


n >> p shift 5>>2 1 2”scomplement signed number,the sign
bit is shifted in to the high- order positions

- Shifts the bits of n right p positions. Zeros


n >>> p Right 15
4>>>28 are

Shift shifted in to the high-order positions

For example the decimal number 3 is represented as 11 in binary and


the decimal number 5 is represented as 101 in binary.

Special Operators

The comma operator

Comma (,) operator is used to link the related expressions together.


Comma used expressions are linked from left to right and the value of the
right most expression is the value of the combined expression. The comma
operator has the lowest precedence of all operators.

Example

sum= (x=5, y=3,x+y);

The result will be sum = 8. The comma operator is used to separate


variables during declaration.

Example:

inta,b,c;

The sizeof operator

The size of operator is not a library function but a keyword, which


returns the operand in bytes. The size of operator always, precedes its
operand. The information obtained from this operator can be very useful
when transferring a program to a different computer.This operator can be
used for dynamic memory allocation. The various expressions and results of
size of operator are

Expression Result

Sizeof(char) 1

Sizeof(int) 2

Sizeof(float) 4

Sizeof(double
8
)

1.3.2. Expressions
An expression is a combination of variables, constants, operators and
function call.

It can be

 Arithmetic expression –It is used to perform arithmetic


calculation. Ex: int z= x+y
 Logical expression – It is used to compare values and combine
more than one arithmetic expressions. Ex: a>b, a+b>b+c
 Relational expression – It is used to combine more than one
relational expressions. Ex: a>b && b>c
 Constant expressions - Expressions consisting entirely of
constant values. Ex: 121 + 17 – 110

1.3.3. Evaluation of expression

Expression evaluated using an assignment statement. When there are


multiple operators in an expression, they are evaluated according to their
precedence and associativity. The operator with higher precedence is
evaluated first and the operator with the least precedence is evaluated last.

Syntax:

variable = expression;

Example:

x=a*b-c;

The precedence and associativity of the operators in C

You might also like