Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
C-Programming Language
Introduction Fundamental of C
It is a structured programming language, high speed and easy to understand so it is C - Character sets are letters, digits, and special characters and white spaces that form
also termed as middle level language or intermediate language between high level and low words, numbers and expressions.
level language. It is very powerful programming language because it is used to prepare The following character sets are used in C.
system software as well as application software. It has large numbers of vocabularies and Letters Digits
syntax to write program. It was developed by Dennis Ritchie in 1970, USA. Upper case A to Z 0 to 9
Why to use C? Lower case a to z
a. C is a powerful and flexible language so system software and application software Special characters
can be developed. , Coma . Period
b. C is a popular language preferred by professional as it has wide variety of c- ; Semicolon : Colon
compilers and accessories. ? Question mark ' Apostrophe
c. C is a portable language so can be compiled and run on another system too. " Quotation marks ! Exclamation mark
d. Those words which has special meaning to a language compiler (called keyword or | Vertical bar / Slash
reversed word), can be used to do any task while making program. \ Back slash ~ Tilde
e. C is a modular so subroutines can be reused in other applications or programs. _ Underscore $ Dollar sign
Features of C-Language % Percentage sign & Ampersand
It is a popular language because it has large number of features for programmer to ^ Caret * Asterisk
write medium types of program. Some features of it are given below: - Minus sign + Plus sign
a. It is highly portable language.
< Opening angle > Closing angle
b. It is procedural programming language because program is divided into number of
( Left parenthesis ) Right parenthesis
functions.
[ Left bracket ] Right bracket
c. It inherits or invisible functionality(memory management, CPU register
{ Left brace } Right brace
manipulation, etc.)
d. It is general purpose structured programming language. # Hash
e. It is internationally standardized programming language. White space
Advantages - Blank line - Horizontal Tab - Carriage Return - New line - Form feed
a. It is very easy for debugging, testing and maintaining.
b. It is portable so can be run in any computer. Comments:
c. It is fast for executing. Comments in code can be useful for various purpose such as for providing visual split
d. The statement in C-language are generally short but very powerful. between various parts of the code, gives information about the code that what is going to do,
e. It has only 32 keywords so that easy to remember. etc.
Disadvantages Comments can be written in two Forms:
C-language has not strong disadvantages but has some negligible disadvantages which //single line comments
are given below: /*multi-line comments*/
a. There is no runtime checking. Tokens:
b. As the program extends it is very difficult to fix the bug. Tokens are fundamental parts of C program from which other parts are formed. There are six
c. It may be compile time overhead due to the misplacing and excessive use of types of C tokens that are listed below:
pointers. a) Operators b) Keywords c) Identifiers d) Constants e) strings f) Special symbols
d. It does not use to develop complex type of software now a days. Keywords or reserved words
e. Object oriented programming concept is not included in C.
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
C keeps a small set of keywords for its own use. These keywords cannot be used as Unsigned short integer Unsigned short 2 0 to 65535
identifiers in the program. It has special meaning in the program to a language compiler that int
are pre defined in C programming language. Unsigned long integer Unsigned long int4 0 to 4294967295
Auto Double Int Struct Float Float 4 1.2E-38 to
Break Else Long Switch Double Double 8 2.2e-308 to
Case Enum Register Typedef Long double Long double 10 3.4E-4932 to
Char Extern Return Union Secondary Data type
Const Float Short Unsigned a. Arrays b. Pointers c. Structure (we will study letter)
Continue For Signed Void
Default Goto Sizeof Volatile Variable
Do If Static While A variable is a location of a computer's memory used to store data value using unique
name and can be changed during program execution time. Any variable declared in a program
Identifiers should confirm to the following:
Identifiers are the names given to program unit such as variable, structure, etc. They a. They must always begin with a letter, although some systems permit underscore as
are not defined in programming language but are used to define by the programmer. Some the first character.
basic rules to define identifiers are given below: b. The length of a variable must not be more than 8 characters.
a. First character must be an alphabet underscore then digits or alphabets. c. White space is not allowed.
b. It must consist of only letters, digits and underscore. d. A variable should not be a keyword.
c. Any standard C language keyword cannot be used as identifier name. e. It should not contain any special characters.
d. It should not be contain a space. Valid Variables:
e. It allows both upper case and lower case characters. Sum1, numb, Salary, emp_name, average1, etc.
Valid Identifier: B1, first_name, lastname, etc. Invalid variables:
Invalid Identifier: 1A, int, void, sum-is, first name, etc. 123, (area), 6th , %ans, first name
Data Type in C Types of variable:
Data type can be represented in variety of ways in any programming language, so that 1. Numeric Variable: The variable which stores numeric data only called numeric variable.
we can store data in system. It determines what types of data should be stored. There are two It may be whole number or fractional number.
types of data type in C which are: Ex. Int Salary float average
a. Primary data type and 2. String variable: The variable which stores character data only is called string variable. It
b. Secondary data type. may be character or string.
Ex. Char name, char w
Primary data type Constant
It is the fundamental data types used to build other data types. The fundamental data A constant is the fixed values that do not change during the execution of a program. C
types are given below: supports four types of constants. They are character, string, integer and floating point
constants.
Variable type Keyword Bytes Range Types of constant are :
Character Char 1 -128 to 127 a. Character constant: Character constant is a single character which is enclosed
Unsigned character Unsigned char 1 0 to 255 with singe quotation mark. Eg. 'a' , 'A', '4' , etc.
Integer Int 2 -32768 to 32767 b. String constant: String constant of any number of consecutive characters
Short integer Short int 2 -32768 to 32767 enclosed in double quotation marks whose maximum length is 255 characters. Eg.
Long integer Long int 4 -2147483648 to .647 "it's very easy language", "2*3/5(55-2)", "3400", etc.
Unsigned integer Unsigned int 2 0 to 65535
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
c. Integer constant: An integer constant refers to a sequence of digits that will be }
either positive or negative. There are three types of integers namely, decimal, octal c. Control statement
and hexadecimal. A control statements are used to create special program features such as logical tests,
i. Decimal integer constant: A decimal constant may have the combination of the loops, branches and it also contains other statements.
digits from 0 to 9 and ve or +ve sign. Eg. 123, -56, 0, etc. Example:
ii. Octal integer constant: An octal integer constant can consist of the For(i=1;i<=100;i++)
combination of the digits 0 to 7 with a leading 0. eg. 0, 01, 0675, etc. Operator
iii. Hexadecimal integer constant: A hexadecimal integer should begin with 0x An operator is a symbol which is used to do mathematical or logical operation on data and
and it contains the digits from 0 to 9 and an alphabet A to F. eg. 0x2, 0xgf, etc. variables. C has a rich set of operators which can be classified as follows:
d. Floating point constant: A floating point constant is a base 10 number that a. Arithmetic Operators
contains either a decimal point or an exponent or both. 0, 33, 1.345E+4, 4.22e78, Those symbols which are used in C Language to perform simple arithmetic operation
etc. such as addition, subtraction, multiplication, division, etc. is called arithmetic operators.
` It uses two operands so that is called binary operator also.
Escape sequence Operator Meaning Example
The non printing characters started with slash ( \ ) which uses special characters with output + addition a+b 2+3=5
functions. These are listed below: - subtraction a-b 5-1=4
Escape sequenceMeaning * multiplication a*b 2*3=6
\a Audible alert / division a/b 6/2=3
\b Backspace % modulus a%b 3/2 = 1
\n New line b. Relational operator
\t Horizontal tab It is required to compare the relationship between operands and bring out a decision
\v Vertical tab and program accordingly. It is also called comparison operator because it is used to
\' Single quote compare any two expressions. C supports the following relational operator:
\" Double quote Operator Meaning Examples
\? Question mark < less than a<b
Statement <= is less than or equal to a<=b
Statement causes the computer to carry out some action as per given instructions. It is > greater than a>b
terminated by semicolon ( ; ). The types of statement are given below: >= is greater than or equal to a>=b
a. Expression statement == is equal to a= =b
It consists of an expression followed by a semicolon only. != is not equal to a!=b
Example: c. Logical operator
B = 5; Logical operators are used to give logical value either true of false. C has following
C = d+e; logical operators:
Printf(Enter a number"); Operator Meaning Examples
b. Compound statement && Logical And (a>b)&&(a>c)
A compound statement consists of several individual statements enclosed with a pair of || Logical Or (a>b)||(a>c)
braces { }. It provides capability for embedding statements with in other statements. ! Logical NOT !(a==b)
Example: Logical AND (&&)
{ This operator is used to operate 2 condition or expression with relational operators if
A = l*b both expression are true then displays true otherwise false.
Area = 3.14*r*r Example: a>b && x ==10
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
Logical OR (||) m = 5;
the logical OR is used to combine 2 expression or the condition evaluates to true if any y = m++;
one of the 2 expression is true. here, first ' m ' is assign to y so y = 5 and m = m + 1 =5 +1=6
Example: a<m || a<n Relational Operator
Logical NOT ( ! ) The operator which compare two quantities and depending on their relation and take certain
The logical not operator takes single expression and evaluates to true if the expression decisions.
is false and evaluates to false if the expression is true. These comparisons can be done with the help of relational operators. Example: <, <=, >, >=,
Example: ! (x>=y) ==, != (not equals to).
Assignment Operator Special Operator
It is used to evaluate t an expression on the right of the expression and substitutes it to a. Comma operator: It is used to separate expressions when multiple expressions are
the value or variable on the left of the expression. It is used to assign value of an used in one statement. It always is evaluated left to right and value of the left expression is
expression into variable. descarded.
variable = expression Example: j = (p = 3, p+2); ( where p+2 = 3+2=5 and p is discarded.
Example: x = a+b b. Size of Operatior: It gives the byte size of data type or variable.
Shorthand operator example:
It is the combination of arithmetic and assignment so it first performs arithmetic and int sum;
then only assignment operation. m = sizeof(sum);
Operators Statement with Statement with n = sizeof(int);
shorthand operator assignment operator c. Pointer operator: * operator is also used for multiplication but when used with a
+= a+=1 a = a+1 pointer variable, It is used to get the content of the address pointed by a pointer variable
-= a-=1 a=a1 to a particular memory location.
*= a*1 a = a*1 Format Specifiers in C
/= a/=b a = a/b It tells what type of the data is being processed. The syntax of format specifier is as
%= a%=b a=a%b follows:
Unary Operator %c - specify a character %d - specify a decimal integer
The increment and decrement operators are one of the unary operations which are very %s - specify a string %f - specify a float
useful in C language. The are extensively used in For and While loops. These format specifier is used with printf ( ) and scanf( ).
Syntax: scanf("%d", &a ); asks the user to input an integer and stores it in a.
++ variable name (prefix) printf("the sum of %d and %d is %d",a ,b, sum);
variable name ++ (postfix)
- - variable name (prefix) Basic structure of C programs:
variable name - - (postfix) Documentation section
The increment operator + + adds the value 1 to the current value of operand and Header file inclusion section
decrement operator - - subtract 1 to the current value of operand. Global declaration section of user defined function
Example: main ( ) function section
suppose {
m = 5; .......................................
y = ++m; .......................................
in this case y = m + 1 = 5+1 = 6 }
m=m+1=5+1=6 Sub program section
again if we rewrite the above as function 1
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
function 2
...................................
...................................
function n