0% found this document useful (0 votes)
12 views39 pages

Basics 1

Uploaded by

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

Basics 1

Uploaded by

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

C

PROGRAMMING
LANGUAGE

(REVISION)
INTRODUCTION
 The C language was invented at Bell Labs by
Dennis Ritchie in 1972 to allow the writing of
the UNIX operating system.
CHARACTER SET
Whenever we write any C program then it consists of
different statements. Each C Program is set of
statements and each statement is set of different c
programming lexims. In C Programming each and
every character is considered as single lexim.

Character Set Consists Of -


Types Character Set
Lowercase Letters a-z
Uppercase Letters A to Z
Digits 0-9
Special Characters # ~ & ( ) % : ; + - etc…
Whitespaces space, new line, tab
KEYWORD
These are the reserved words that have a predefined
meaning and these meanings cannot be changed. All
keywords must be written in small letters.
Note:
 Keywords can be used only for their intended
purpose.
 Keywords can't be used as programmer defined
identifier.
 The keywords can't be used as names for variables.
CONTD…
There are 32 keywords in C language:
IDENTIFIER
Identifiers are the names that are given to various program
elements such as variables, symbolic constants and functions.

Rules for define identifiers :


a) First character must be alphabetic character or under
score
b) Must consists of only letters, digits or underscore.
c) Can not use a keyword.
d) Must not contain a whitespace.
VARIABLE
A variable provide us with named storage that we can write
to, retrieve and manipulate throughout the course of the
program. In other words, variables are memory locations
in the computer’s memory that holds data. Contents of a
variable may vary-hence the name variable.

Before using a variable, declare the variable to reveal the


name and data type (i.e. what type of value can be stored
in it).

When we declare a variable we inform the compiler of two


things, the name of the variable and the type of the
variable.
EXAMPLE

int integer1=45;

In the above example, we declare a variable of type


integer with the name integer1 and initial value 45.

45
integer1
RULES FOR WRITING
 They must begin with a letter.
 Length can be of 31 characters, but it should be not
more than 8 characters as first 8 characters are
treated as significant by some compilers.
 Uppercase and lowercase are significant. i.e. Total
and total are not same.
 It should not be a keyword.
 Whitespaces are not allowed.
TOKENS
The smallest individual units of a C- program are
called Tokens. C has six type of tokens.

 Keywords (eg. int, goto, break)


 Identifiers (amount, marks)
 Constants (3.14)
 Strings (“abc”)
 Special symbols ([],{})
 Operators (+ - * /)
LVALUE AND RVALUE
There are two kinds of expressions in C:

lvalue: An expression that is an lvalue may appear


on either the left-hand or right-hand side of an
assignment.

rvalue: An expression that is an rvalue may appear


on the right- but not left-hand side of an
assignment.
Example:

Variables are lvalues and so may appear on the left-


hand side of an assignment. Numeric literals are
rvalues and so may not be assigned and appear on
the right-hand side. Following is a valid statement:
int g = 20;

But following is not a valid statement and would


generate compile-time error:
10 = 20;
WHITESPACES
Whitespace is the term used in C to describe blanks,
tabs, newline characters and comments. Whitespace
separates one part of a statement from another and
enables the compiler to identify where one element
in a statement, such as int ends and the next
element begins.
Therefore, in the following statement:
int age;
There must be at least one whitespace character
(usually a space) between int and age for the
compiler to be able to distinguish them.
DECLARATION

A declaration associates a group of variables with


a specific data type. A declaration consists of
data type, followed by one or more variable
names, ending with a semicolon.

E.g.
int x, y;
COMMENTS
Comments are added to make a program more
readable to you. Everything that is inside /* and */
is considered a comment and will be ignored by the
compiler. The compiler treats them as though they
were white space or blank characters. You must not
include comments within other comments, so
something like this is not allowed: [*].
/* this is a /* comment */ inside a comment, which is
wrong! */
Comments are a way of inserting remarks and
reminders into a program without affecting its
content.
CONTD..
 Comments do not have a fixed place in a program.
 Programs can contain any number of comments
without losing speed. This is because comments are
skipped out of a source program by the compiler
when it converts the source program into machine
code.
Comments are marked out or delimited by the
following pairs of characters:
/* ...... comment ......*/ for multiple lines
// for single line
CONSTANT

Constants are the fixed values that do not change


during execution of a program.
Any attempt to change the value of constant will
result in an error message.
Syntax:
const <type> <name>=<val>
Eg.
const float pi=3.14;
TYPES
Constants can be of following types:

Constants

Numeric constant Character constant

Single
Integer Real character
String
constant constant constant constant

Integer and floating-point constants represent numbers. They


are often referred to as numeric-type constants.
ESCAPE SEQUENCES
SCANF()AND PRINTF()
scanf() is used to input data through keyboard. The
general format is:
scanf(“control string”, &variable1,&variable2….);
scanf(“%d %d”, &val1, &val2);
The control string contains the format of data being
retrieved. The ampersand symbol (i.e. &) before
each variable name is an operator that specifies the
variable name’s address.
printf() is used to display any message or requesting
the user to enter a value. The general format is:
printf(“control string”, variable1,variable2….);
printf(“%d %d”,val1, val2);
OPERATORS
An operator in general, is a symbol that operates on
a certain data type. C language is very rich in
operators. The commonly used operators include:
 Arithmetic operator
 Relational operator
 Logical operator
 Assignment operator
 Conditional operator
 Comma operator
 Unary operator
 Bitwise operator
 Arithmetic  Relational
 +  <
 -  <=
 *  >
 /  >=
 %  ==
 !=
Logical Assignment

AND && =
OR ||
NOT !
Conditional operator is an Comma
equivalent form of if –else
loop. It is called as ternary
operator because used on i = (j=10, j+20);
three operands. This
operator consist of two
symbols: the question mark
(?) and the colon (:).

Syntax:
(Condition) ? statement1 :
statement2
Unary  Bi
Minus ~ Bitwise Negation one's
Increment and complement (unary)
Decrement & Bitwise And
| Bitwise Or
Sizeof
^ Bitwise Exclusive Or
Cast >> Right Shift by right hand
side (RHS) (divide by power
of 2)
<< Left Shift by RHS
(multiply by power of 2)
 twise
DATA TYPES
TYPE CASTING
When values of different types are mixed in an
expression, they are converted to the same type
before performing the particular operation.
This process of converting values for one type
to another type is known as type conversion.

C language facilitates type conversion in following


two forms:
 Implicit type conversion
 Explicit type conversion
IMPLICIT TYPE CONVERSION

An implicit type conversion is automatically


performed by the compiler without
programmer’s intervention. For implicit type
conversion, the C language follows certain rules
that are introduced below:
At the time of evaluation of expression, when an
operation is performed on operands of different
ranks, then operand of lower rank is converted
to operand of higher rank. The operand of
larger size is given a higher rank than that of
smaller size.
E.g.
int i, j;
float x, y;
x / i + y * j;
while evaluating “x/i”, integer operand “i” will be
converted to float type as “x” is float type and
float has higher rank than int. Similar is the case
with “y*j”.

In the assignment statements, depending on the


difference of rank of operands, the C system tries
to either promote or demote the right operand so
as to make its rank match the left operand
(variable).
CONVERSION RANK

Real

Integ
er

11. long
double
Charact 10. double
8. unsigned 9. float
er long int
7. long int
6.signed int
5. int
4. unsigned
short
3. short
PROMOTION
Promotion is the process that takes place when
value of an operand of lower rank is assigned to
a variable of type that has a higher rank.
E.g.
int i;
float f;
f=425; //value of f will be 425.0
DEMOTION
Demotion is the process that takes place when a
value of an operand of higher rank is assigned
to a variable of type that has lower rank.
E.g.
int i;
float f;
f=425.76;
i=f; //value of i will be 425
EXPLICIT TYPE CONVERSION

An explicit type conversion is user defined that


forces an operand or expression to be of specific
type. The process of explicit type conversion is
also known as type casting.

Syntax is:
type(operand) or type(expression)

E.g. (float) x
Will convert variable x of type int to float type.
TYPEDEF
#include<stdio.h>
#include<conio.h>
Void main()
{
typedef int myvar;
myvar val1,val2;

Printf(“Enter 2 values”);
Scanf(“%d%d”,&val1,&val2);
getch();
}
ENUMERATION CASE 1
#include<stdio.h>
#include<conio.h>
Void main()
{
enum day{mon,tue,wed,thu,fri,sat,sun};

printf(“%d”,tue);
}

Output : 1
ENUMERATION CASE 2
#include<stdio.h>
#include<conio.h>
Void main()
{
Enum
day{mon=1,tue,wed,thu,fri,sat,sun};

printf(“%d”,tue);
}

Output : 2
ENUMERATION CASE 3
#include<stdio.h>
#include<conio.h>
Void main()
{
enum
day{mon,tue,wed=7,thu,fri,sat,sun};

printf(“%d”,thu);
}

Output : 8
CONDITIONAL/TERNARY OPERATOR

Conditional operator is an equivalent form of if –else loop.


It is called as ternary operator because used on three
operands. This operator consist of two symbols: the
question mark (?) and the colon (:).

Syntax:
(Condition) ? statement1 : statement2

This expression evaluates statement 1 or statement2


depends on the
condition. If the condition is true statement1 gets
executed other wise
statement2 gets executed.
Ex:
main( )
{
int x;
clrscr( );
printf( “ enter first no”);
scanf(“%d”,&x);
printf( “ enter second no”);
scanf(“%d”,&y);

(x>y) ? printf(“first no is greater”) : printf(“second no is


greater”);
getch();
The conditional expression can be used as
shorthand for some if-else statements. It is a ternary
operator.

You might also like