Programming in C Unit-1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

PROGRAMMING IN ‘C’

Unit 1:
Introduction
History of ‘C’:
C is a powerful high-level language, which is developed by Dennis Ritchie in bell laboratories
in 1972. The UNIX operating system, which was also developed at bell laboratories, was coded almost
entirely in ‘C’.
In1983, American National Stranded Institute (ANSI) appointed a technical committee to
define a standard for ‘c’ .the committee approved a version of ‘c’ in 1989, which is now called as
“ANSI C”.
In 1990 the C was approved by the International Standard Organization (ISO)

Importance of ‘C’:
• It has a rich set of built in functions and operators can be used to write any complete program.
• It is recommended and best for writing both system software & application software
• The compilers available in market are written in ‘C’.
• Programs written in ‘C’ are efficient and fast.
• ‘C’ is highly portable, which means program written in one computer than can be run another
computer without any modification.
• ‘C’ programs can be able to extend itself.
• ‘C’ language is best for structure programming.

Basic Structure of ‘C’ programs:


Documentation section
Link section
Definition section
Global declaration section
Main function section
{
Declaration part
Executable part
}
Sub program section (user defined function)
Function-1
Function-2
Function-n

Documentation section:
The documentation section contains a set of command lines, which explains the name of the
program, the author name, date of written and other details
For eg: \*program for addition*\

Link section:
The link section offer instructions to the compiler to link functions from system library
For example: #include<stdio.h>

Definition section:
To define all symbolic constants in definition section
For eg: #define PI 3.14

Global declaration section:


Variables declared in the global declaration sections that are used more than one function. The
user defined function also declared in this section.
For eg: int x,y; (\*global variable *\)’

Main function section: They contain two parts


1. Declaration part
2. Executable part
Declaration part:
To declare all the variables using the executable part.

Executable part:
It contains atleast one statement or instruction. These two parts must be appear between the
opening and closing braces. The program executions start at the opening braces and end the closing
braces. The closing braces of the main function indicates the logical end of the program all the
statements in the declaration and executable parts end with a semicolon(;).

1. Subprogram functions:
It contains all the user defined functions that are called in the main functions. User defined function
are generally placed immediately after the main function. Although it may appear in any order.
Note: All section, expect main function section may be absent.

Character set:
The character set that can be used to form words, numbers and expressions offers the
following character and the different categories.

Letters: letters both uppercase (A-Z) and lowercase (a-z).


Digits: all decimal digits (0-9).
Special character :!#$^)+)*&...Etc.
White spaces: blank space, horizontal tab, newline.

‘C’ tokens
In a c program a smallest individual unit is called as a C tokens. C has 6 types of tokens.
1. Keyword-int,for
2. Identifiers-tot, avg.
3. Constants-555,256.
4. Strings-“jsc”.
5. Special symbol-{},[].
6. Operator-/,*,-, +.

Keyword (or) Reserved words:


Every c word classified as either a keyword or identifires.All reserved words have fixed
meaning and these meanings cannot be redefined, to mean anything else, all reserved words must be
written in lowercase. Some of keywords are:
Auto,double,else,struct,break,case,long,switch,typedef,enum,register,return,char,foalt,const,continue,
unsigned,void,for,signed,sizeof,default,goto,if,while,static,int.
Note: keyword cannot be used as a identifiers name.

Identifiers:
Identifiers means the names of the variable, functions and arrays this are user defined names
and consists of a sequence of letters&digits.both uppercase and lowercase letters are allowed,
although lowercase letters are commonly use. The underscore character is also permitted in
identifiers. It is used as a link between two words in long identifiers.

Rules for defining identifiers:


• First letter should be alphabet and then followed by digits.
• Digits or underscore are allowed
• Only first 32 characters are significant.
• Cannot use a keyword as a variable.
• No white space is allowed.

Constants:
It means a fixed value that does not changed during the execution of the program.
Type of constant:
1) Numeric constants:
a) Integer constants-eg: 987, 978, and 21.
b) Real constants-eg; 98.3.
2) Character constants:
a) Single character constant-eg:’n’.
b) String constant-eg”Bca”
c) Backslash character constant-“\n”
This special backslashes constants can be used for output functions or statement.eg:”\t” stand for
horizontal tab.
‘\a’-alert bell ‘\n’-newline ‘\t’-horizontal tab ’\b’-backspace.

Variable:
Variable is a data name that is used to store a data value. The value of a variable, which can be
changed during the execution of programs. All the variables must be declared before they can be
used.th value assigned to a variable is placed in the memory allocated to those variables.

Different types of variable:


1) Integer variables-are used to store whole numbers.
2) Float variables-are used to store fractional numbers.
3) Character variables-are used to store character.
4) Boolean variables-can have one of two values (true or false).

Data types in ‘C’


The data type in ‘c’ can be classified into 2 types.
1) Primary data types.
2) Secondary data types.

Primary data types:


1) Character(1 byte)
2) Integer(2 byte)
3) Float(4 byte)
4) Boolean(1 byte)
5) Void(0 byte)

Secondary data types (or) derived data types:


1) Array
2) Pointers
3) Structure
4) Union
5) Enum…etc.

Operator in ‘C’
The operator can be used to manipulate data and variables. There are 7 different types of operators.
They are
1) Arithmetic operator
2) Relational operator
3) Shorthand operator
4) Increment and decrement operator
5) Conditional operator
6) Bitwise operator
7) Special operator.

Arithmetic operators:
Most the common binary arithmetic operators are
+ add two numeric values.
- Subtract the second value from first.
/ divides the second value from the first
* Multiply two numeric values
% modules (divide the second value from the first and gives
reminder)

Relational operators:
> Greater than
< lesser than
>= Greater than or equal to
<= Lesser than or equal to
== Equal to
!= Not equal to

Logical operators:
&& Logical and
|| Logical or
! Logical not
Note: the above operators evaluate two expressions and evaluations stop as a true or false of the result
is known.

Assignment operators:
The assignment operators can be used to assign the result of the expression to a variable. In c
assignment operator is’=’
Syntax:
[Variable operator=expression]
Eg: n=m+2.

Shorthand assignment operator:


This operator are simple to real and use.
I+=5; I=i+5
I*=5 I=i*5
I/=5 I=i/5
i-=5 I=i-5
Note: This operator is usually very efficient while writing a program.

Increment &decrement operators:


The unary operator one operand variables or constant. C provides two unary operator are +
+,--for incrementing and decrementing variables. The increment operator add 1 to it operand. While
the decrement operator subtract 1.
For eg:++k,k++ its equivalent to k=k+1.
--n , n—its equivalent to n=n-1.

Conditional operator:
A ternary operator is used to take a decision. It is an alternate to instructor. syntax is
Expression? expression1; expression2;
Eg :(x > y)?
Printf(“x is greater than y”)
Printf(“y is greater than x”)

Bitwise operator:
Bitwise operator can be used for manipulation of data at bit level. These operators are used for
testing the bits or shifting the right or left bitwise operator cannot be applied to float or double.
& Bitwise and
<< Shift left
>> Shift right
! Bitwise or
^ Bitwise exclusive or

Special operators:
The special operators are comma and size of the operator.
Comma operator:
The comma operator can be used to link the related expression together.
Eg: x(n=5,m=2,n+m);
For(k=1,n=3;k<n;k++,n++)

Size of operator:
Assize of operator can be used to returns the no of bytes the operand occupy. The operand
may be a variable, a constant or a data type
Eg1: int total, x;
X=sizeof(total);
2. sizeof (float);
Note: a size of operator generally used to determine size of arrays, and structures. When the size is not
known to the programmer. In addition to that, it is also used to allocate dynamic memory space to the
variables during the execution of the program.

Handling input and output format:


The get char function ();
This function is used to read a single character. The syntax for get char() is:
Variable_name=getchar();
Where, the variable name is valid ‘C’ name that has been declared as chartype. When this line is
encounted,the cursor will wait on the screen until a key is pressd and then assigned this character as a
value to getchar function.
Eg: #include<stdio.h>
Main()
{
Char ch;
Ch=getchar();
Printf(“the value of ch is %c”, ch”);
}
Will assign the character ‘k’ to the variable name ch when we press the key ‘k’ on the keyboard.

Put char function():


The function can be used for writing a single character one at the time to the terminial, the
syntax is:
Put char(variable name)
The statement displays character contained in the variable name at the terminal.
For eg: #include<stdio.h>
Main()
{
Char ch;
Ch=getchar();
Putchar(ch);
}
Formatted input:
It refers to an input data that has been arranged in a particular format. Reading integer
number the field specification for reading the integer number is %wd where the “%” indicates the
conversion specification’s is an integer number that specify the field width of the number to be read
and ‘d’ known as data types character, indicates that the number to be read that is an integer mode.
Eg: scanf(“%2d%4d”,&x,&y);

The input data is 13 4988


The value 13 will assigned to x and 4988 to y suppose the input data is 4988 13.the variable x
will be assigned 49 because of %2d and y will be assigned 88 unread part of 4988,the value 13 is
unread will be assigned to the first variable in next scanf column.
An input may be supplied by specifying “*” in the place of the field width.
Eg: scanf(“%d%*d%d”.&x,&y);

The input data is 156 876 765


The value 156 assigned to x, 876 is scipped because of asterisic(*) and 765 to y .
Note:’l’ for long integer and ‘h’ for short integer.

Reading the real numbers:


The fixed width of the real numbers is not to be specified and therefore scanf reads real no
using simple specification %f for both notations, namely decimal and exponential notations.
Eg: scanf(“%f%f%f”,&x,&y,&z);
Note: %lf for the double data type number may be skipped %*f.

Reading character and string:


A specification character and string is %s(or) %c
Reading the mixed data type:
We can read more than any data type of data by using one single scanf statement
Eg: int count;
Char sex;
Float avg;
Char name [10];
Scanf(“%d%c%f%S”,&count,&sex,&avg,&name);
Formatted output:
Output of the integer numbers the format specification for printing an integer number is %wd,
where the ‘w’ specifies the minimum field width for the output. However if a number is greater than
specify field width, it will be printed in fall. Otherwise over writing the minimum specification. the ‘d’
specifies that value to be printed an integer.
Eg: x=5678
Printf(“%d”,x)
5 6 7 8
Printf(“%5d”,x)
5 6 7 8
Printf(“%2d”,x)
5 6 7 8

Output of the real number:


The formate specification for the real numbers is “%wpf” where ‘w’ indicate the minimum
number of positions that all to be used display of the value and the integer ‘p’ indicates the number of
digits to be displayed after the decimal point.
For eg:y=75.8769 can be displayed under different formate specification.
Eg:1. Printf(“%7.4f”,y);
7 5 . 8 7 6 9

Printf(“%7.2f”,y);
7 5 . 8 0
Printf(“%-7.2f”,y)
7 5 . 8 0
Printing of single character:
A single statements can be displayed in a single formate “%wc”. Here default value ‘w’ is 1.
Char ch;
Ch= m;
Printf(“%c”,ch);

Printing the string


The formate specification for printing string is “%wps”,where ‘w’ specifies for field width for display
and ‘p’ indicates that only first ‘p’ character of the string are to be displayed.
For eg:
Char city[10];
City=”Bangalore”
Printf(“%10.3s”,city);

You might also like