[go: up one dir, main page]

0% found this document useful (0 votes)
50 views16 pages

C Theory Notes

The document discusses the fundamentals of C programming language including character set, delimiters, reserved words, and identifiers. It describes the different types of translators used in C like assembler, compiler, and interpreter and explains their working. It also provides an overview of the structure of C programs.

Uploaded by

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

C Theory Notes

The document discusses the fundamentals of C programming language including character set, delimiters, reserved words, and identifiers. It describes the different types of translators used in C like assembler, compiler, and interpreter and explains their working. It also provides an overview of the structure of C programs.

Uploaded by

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

1 INTRODUCTION IN C

PROGRAMMING LANGUAGE
LOW LEVEL LANGUAGE
ASSEMBLY LANGUAGE:
Assembly language is only understand by human beings not by the computers.
In assembly language data can be represented with the help of mnemonics such
as Mov, Add, Sub, End etc. Assembly language is easy to understand by the
human being as compare to machine language. Modifications and error fixing
can be done in assembly language. Easy to memorize the assembly language
because some alphabets and mnemonics are used. Execution is slow as
compared to machine language. Assembler is used as translator to convert
mnemonics into machine understandable form. Assembly language is the
machine dependent and it is not portable.

MACHINE LANGUAGE:
Machine language is only understand by the computers. In machine language
data only represented with the help of binary format(0s and 1s), hexadecimal
and octadecimal. Machine language is very difficult to understand by the human
beings. Modifications and error fixing cannot be done in machine language.
Machine language is very difficult to memorize so it is not possible to learn the
machine language. Execution is fast in machine language because all data is
already present in binary format. There is no need of translator.The machine
understandable form is the machine language. Machine language is hardware
dependent.

HIGH LEVEL LANGUAGE:


One can easily interpret and combine these languages as compared to the low-
level languages. They are very easy to understand. Such languages are
programmer-friendly. Debugging is not very difficult. They come with easy
maintenance and are thus simple and manageable. One can easily run them on
different platforms. They require a compiler/interpreter for translation into a
machine code. A user can port them from one location to another. Such
languages have a low efficiency of memory. So it consumes more memory than
the low-level languages.They are very widely used and popular in today’s
times.Java, C, C++, Python, etc., are a few examples of high-level languages.

1
What is C?
The C language was developed by Dennis Ritchie in 1970s at AT&T Bell
laboratories) Murray Hill, New Jersey. Initially it was designed for programming
in the operating system called UNIX. After the advent of C, the whole UNIX
operating system was rewritten using it. Now almost the entire UNIX operating
system and the tools supplied with it including the C compiler itself are written
in C. The C language is derived from the B language, which was written by Ken
Thompson at AT&T Bell laboratories. The B language was, adopted from a
language called BCPL (Basic Combined Programming Language), which was
developed by Martin Richards at Cambridge University. In 1982 a committee
was formed by ANSI(American National Standards Institute) to standardize the
C language. Finally in 1989, the standard for C language was introduced known
as ANSI C. Generally most of the modern compilers conform to this standard.

Why Learn C?
 It is one of the most popular programming languages in the world
 If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the
syntax is similar
 C is very fast, compared to other programming languages,
like Java and Python
 C is very versatile; it can be used in both applications and technologies

Characteristics of C
C is a middle level language. It has the simplicity of a high level language as
well as the power of a low level language. This aspect of C makes it suitable for
writing both application programs and system programs. Thus it is an excellent,
efficient and general-purpose language for most of the applications such as
mathematical, scientific, business and system software applications

Translators
The three types of translators used are-

Assembler
Compiler
Interpreter
2
Assembler is used for converting the code of low-level language (assembly
language, into machine language.

Compilers and interpreters are used to convert the code of high-level


language into machine language. high level program is known as source
program and the corresponding machine language program is known as object
program. Although both compilers and interpreters perform the same task,
there is a difference in their working.
A compiler searches all the errors of program and lists them. If the program is
error free then it converts the code of program into machine code and then the
program can be executed by separate commands An interpreter checks the
errors of program statement by statement. After checking one statement, it
converts that statement into machine code and then executes that statement.
This process continues until the last statement of program or an erroneous
statement occurs.

PROCESS OF PROGRAM DEVELOPMENT


User requirements

Problem analysis

Determine Input and Output

Designing algorithm

Program coding

Testing and debugging the program

Program Documentation
3
Structure of C Programs
Comments
Preprocessor directives
Global variables
int main(void)
{
local variables
statements
…………….
…………….
return 0;
}
func1()
{
local variables
statements
……………
……………
}
func2()
{
local variables
statements
…………….
…………….
}

4
2 FUNDAMENTALS OF C

CHARACTER SET
THE CHARACTER THAT ARE USED IN C PROGRAMS ARE GIVEN BELOW

UPPER CASE AND LOWERCASE LETTERS


A,B,C,D,E,F……………………………Z
A,b,c,d,e,f…………………………….z
DIGITS
0,1,2,3,4,5,6,7,8,9

GRAPHIC CHARACTER

CHARACTER NAME CHARACTER NAME


+ Plus sign [ Left bracket
* asterisk ] Right bracket
\ Backward slash { Left braces
/ Forward slash } Right braces
< Lesser than , comma
> Greater than ‘ Single quotes
( Left parenthesis “ Double quotes
) Right parenthesis # Hash
. period % Percent sign
: colon ^ Caret sign
; semicolon = Equal to sign
? Question mark - Minus sign
& ampersand _ Underscore
| Vertical bar ! Exclamation sign

5
Whitespace Characters
Space character, newline character, horizontal tab, vertical tab,
and form feed.

Other characters
Alert, backspace, carriage return and null character

ESCAPE SEQUENCES/EXECUTION CHARACTER


Escape Meaning purpose
sequence
\b
backspace Moves the cursor to the
previous position of the
current line

\a alert Produces an audible or visible


alert

\r Carriage return Moves the cursor to


beginning of the current line.

\n New line Moves the cursor to the


beginning of the next line

\f Form feed Moves the cursor to the initial


position of the next logical
page.

\() Null character Used for termination of


character string

\v Vertical bar Moves the cursor to next


vertical tab position

\t Horizontal bar Moves the cursor to the next


horizontal tab position.

\\ backslash Presents a character with


backslash (\)

6
DELIMITERS
: Colon Used for label
; Semicolon End of statement
( ) Parentheses Used in expression
[ ] Square Brackets Used for arrays
{ } Curly Braces Used for block of statements
# Hash Preprocessor directive
, Comma Variable delimeter

RESERVED WORDS/KEYWORDS

auto break case char


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

IDENTIFIERS
All the words that we will use in our C programs will be either keywords or
identifiers. Keywords are predefined and can't be changed by the user, while
identifiers are user defined words and are used to give names to entities like
variables, arrays, functions, structures etc.

Rules for naming identifiers are given below-

(1) The name should consist of only uppercase and lower case letters, digits
and underscore sign().

(2) First character should be an alphabet or underscore. (3) The name


should not be a keyword.

(4) Since C is case sensitive, the uppercase and lowercase letters are
considered different. For example code, Code and CODE are three different
identifiers.

7
(5) An identifier name may be arbitrarily long. Some implementations of C
recognize only the first eight characters, though most implementations
recognize 31 characters. ANSI standard compilers recognize 31characters.

The identifiers are generally given meaningful names. Some examples of


valid identifier names-

Value a net_pay recl _data MARKS

Value Some examples of invalid identifier names are-

5bc First character should be an alphabet or underscore


int int is a keyword
rec# #is a special character
avg no blank space is not permitted

DATA TYPES
A data type defines a domain of allowed values and the operations that can
be performed on those values. Storage representation of different data types
is different in memory.
There are four fundamental datatypes in C. which are int, char, float and
double.

char is used to store any single character


int is used to store integer value
float is used for storing single precision floating point number
double is used for storing double precision floating point number

We can use type qualifiers with these basic types to get some more types,
There are two types of type qualifiers-
1. Size qualifiers - short, long
2. Sign qualifiers – signed, unsigned

The qualifiers signed and unsigned can be applied to char and integer types.
When the qualifier unsigned is used the number is always positive, and when
signed is used number may be positive or negative.

The qualifiers short and long can be applied to int type to get types short int
and long int. qualifier long can be applied to double to get type long double
which stores extended precision floating point number. The size and range of
different data types on a 16-bit machine is given in the following table. The
size and range may vary on machines with different word sizes

8
Basic data Data types with type Size Range
type qualifiers (bytes)
char Signed char 1 -128 to 127
Unsigned char 1 0 to 255
int Int or signed int 2 -32768 to 32767
Unsigned int 2 0 to 65535
Short int 1 -128 to 127
Short int or signed int 1 0 to 255
Long int or signed int 4 -2147483648 to 2147483647
Unsigned long int 4 0 to 4294967295
float Float 4 3.4E-38 to 3.4E+38
double Double 8 1.7E-308 to 1.7E+308
Long double 10 3.4E-4932 to 1.1E+4932

CONSTANTS
constants

Numeric Character String


Constants constants constants

Integer Real
constant constants

Decimal Octal Hexadecimal

Numeric Constants
Numeric constants consist of numeric digits, they may or may not have
decimal point for defining numeric constants-These are the rules

1. Numeric constant should have at least one digit.

2. No comma or space is allowed within the numeric constant.

3. Numeric constants can either be positive or negative but default sign is


always positive.

9
Integer Constants
There are three types of integer constants based on different number
systems(decimal, octal, hexadecimal). The permissible characters that can
be used in these constants are-

Decimal constants 0,1,2,3,4,5,6,7,8,9(base 10)


Octal constants 0,1,2,3,4,5,6,7 (base 10)
Hexadecimal constants 0,1,2,3,4,5,6,7,8,9.A.B.C.D.E.F,ab,c,d,e,f
(base 16)

Real floating point constants


Floating point constants are numeric constants that contain decimal point.
Some valid floating point constants are-

0.5 5.3 4000.0 0.0073 5597.89

Character Constants
A character constant is a single character that is enclosed within single
quotes. Some valid character constants

‘k’ ‘h’ ‘r’ ‘j’ ‘A’ ‘G’ ‘U’

String Constants
A string constant is enclosed within double quotes (""). At the end of string,
the null character "\0" is automatically placed by the compiler. Some
examples of string constants are-

"Kumar" “kavin” “Hello”

Variables
Variable is a name that can be used to store values, it can take different
values but one at a time. These values can be changed during execution of
the program. A data type is associated with each variable and it decides
what values the variable can take. The rules for naming variables are same
as that for naming identifiers.

10
Declaration of Variables
It is must to declare a variable before it is used in the program. Declaration
of a variable specifies its name and datatype. The type and range of values
that a variable can store depends upon its datatype. The syntax of
declaration of a variable is- declare more than one variable in a single
declaration. For example- int x,y,z,total;

Here x, y, z and total are all variables of type int.

Initialization of Variables
When a variable is declared it contains undefined value commonly known as
garbage value. If we want we can assign some initial value to the variable
during the declaration itself, this is called initialization of the variable.

For example-
int a-5;
float x=6.9;
y=10.5;
char ch='y':
double num=0.15197e-7;
int l, m, n, total=0;

In the last declaration, only the variable total is initialized.

11
3
33
Input and output
(There are three main functions of any program: it takes data as input,
processes this data and gives output. The Input operation involves
movement of data from an input device (generally keyboard) to computer
memory, while in output operation the data moves from computer memory
to the output device (generally screen). C lan- guage does not provide any
facility for input-output operations. The input output is performed through a
set of library functions that are supplied with every C compiler. These
functions are formally not a part of the language but they are considered
standard for all input-output operations in C. The set of library functions that
per-forms input-output operations-is known as standard I/O library.

There are several header files that provide necessary information in support
of the various library functions.These header files are entered in the
program using the #include directive at the beginning of the program.For
example if a program uses any function from the standard I/O library, then
it should include the header file

stdio.h as-
#include<stdio.h>

Similarly there are other header files like math.h, string.h, stdlib.h that
should be included when certain library functions are used. In this chapter
we will discuss about the input functions scanf() and getchar() and the
output functions printf() and putchar(). There are several other input-output
functions that will be discussed in other chapters.A simple method of
providing value to variables is by assignment statement.

Conversion Specifications

The functions scanf() and printf() make use of conversion specifications to


specify the type and size of data. Each conversion specification must begin
with a percent sign (%). Some conversion specifications are given below-

12
%c print a single character
%d print a integer
%f print a floating point number(by default 6 digits printed after
the point)
%s print a string
%o print an unsigned octal integer
%x print an unsigned hexadecimal integer using a,b,c,d,e,f
%x print an unsigned hexadecimal integer using A,B,C,D,E,F
%u print an unsigned decimal integer

13
4 OPERATORS AND
EXPRESSION
This is a symbol use to perform some operation on variables, operands or
with the constant. Some operator required 2 operand to perform operation
or Some required single operation.
Several operators are there those are,
 arithmetic operator
 Assignment Operator
 Increment Operator
 Decrement Operator
 Logical Operator
 Conditional Operator
 Comma Operator
 Size Of Operator
 Bitwise Operator

1. Arithmatic Operator
This operator used for numeric calculation. These are of either Unary
arithmetic operator, Binary arithmetic operator.
And these operators are addition, subtraction, multiplication, division.
Binary arithmetic operator on other hand required two operand and its
operators are +(addition), -(subtraction), *(multiplication), /(division),
%(modulus). But modulus cannot applied with floating point operand as
well as there are no exponent operator in c. Unary (+) and Unary(-) is
different from addition and subtraction. When both the operand are
integer then it is called integer arithmetic and the result is always integer.

2. Assignment Operator
A value can be stored in a variable with the use of assignment operator. The
assignment operator(=) is used in assignment statement and assignment
expression. Operand on the left hand side should be variable and the
operand on the right hand side should be variable or constant or any
expression. When variable on the left hand side is occur on the right hand
side then we can avoid by writing the compound statement. For example, int
x= y; int Sum=x+y+z;
14
3. Increment & Decrement Operator
The Unary operator ++, --, is used as increment and decrement which acts
upon single operand. Increment operator increases the value of variable by
one .Similarly decrement operator decrease the value of the variable by one.
And these operator can only used with the variable, but can't use with
expression and constant as ++6 or ++(x+y+z) It again categories into
prefix post fix . In the prefix the value of the variable is incremented 1st,
then the new value is used, where as in postfix the operator is written after
the operand(such as m++,m--).

4. Relational Operator
It is use to compared value of two expressions depending on their relation.
Expression that contain relational operator is called relational expression.
Here the value is assign according to true or false value.

A. (A>=B) || (B>20)
B. (B>A) && (E>B)
C. 0(B!=7)

5. Conditional Operator
It sometimes called as ternary operator. Since it required three expressions
as operand and it is represented as (? , :)

SYNTAX
exp1 ? exp2 :exp3 Here exp1 is first evaluated. It is true then value return
will be exp2 . If false then exp3.

6. Comma Operator
Comma operator is use to permit different expression to be appear in a
situation where only one expression would be used. All the expression are
separator by comma and are evaluated from left to right.

15
7. Sizeof Operator
Size of operator is a Unary operator, which gives size of operand in terms of
byte that occupied in the memory. An operand may be variable, constant or
data type qualifier. Generally it is used make portable program(program that
can be run on different machine) . It determines the length of entities,
arrays and structures when their size are not known to the programmer. It is
also use to allocate size of memory dynamically during execution of the
program.

8. Bitwise Operator
Bitwise operator permit programmer to access and manipulate of data at bit
level. Various bitwise operator enlisted are
one's complement (~)
bitwise AND (&)
bitwise OR (|)
bitwise XOR (^)
left shift (<>)
These operator can operate on integer and character value but not on float
and double. In bitwise operator the function showbits( ) function is used to
display the binary representation of any integer or character value. In one's
complement all 0 changes to 1 and all 1 changes to 0. In the bitwise OR its
value would obtaining by 0 to 2 bits. As the bitwise OR operator is used to
set on a particular bit in a number. Bitwise AND the logical AND. It operate
on 2operands and operands are compared on bit by bit basic. And hence
both the operands are of same type.

16

You might also like