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

Q1. Write A Short Note On IDENTIFIERS: Syntax

Identifiers are the names given to variables, functions, and labels in a program. They must be unique and cannot be the same as keywords. Identifiers are declared and then used to refer to variables or functions. There are some syntax rules for identifiers regarding allowed characters and case sensitivity. Identifiers have scope within a program and linkage that determines if the same name refers to the same entity in different parts of a program.

Uploaded by

Prajita Mehta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Q1. Write A Short Note On IDENTIFIERS: Syntax

Identifiers are the names given to variables, functions, and labels in a program. They must be unique and cannot be the same as keywords. Identifiers are declared and then used to refer to variables or functions. There are some syntax rules for identifiers regarding allowed characters and case sensitivity. Identifiers have scope within a program and linkage that determines if the same name refers to the same entity in different parts of a program.

Uploaded by

Prajita Mehta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Q1.

Write a short note on IDENTIFIERS


"Identifiers" or "symbols" are the names you supply for variables, types,
functions, and labels in your program. Identifier names must differ in spelling
and case from any keywords. You cannot use keywords (either C or
Microsoft) as identifiers; they are reserved for special use. You create an
identifier by specifying it in the declaration of a variable, type, or function. In
this example, result is an identifier for an integer variable,
and main and printf are identifier names for functions.

Copy
#include <stdio.h>

int main()
{
int result;

if ( result != 0 )
printf_s( "Bad file handle\n" );
}

Once declared, you can use the identifier in later program statements to
refer to the associated value.

A special kind of identifier, called a statement label, can be used


in goto statements. (Declarations are described inDeclarations and
Types Statement labels are described in The goto and Labeled Statements.)

Syntax
identifier:

nondigit

identifier nondigit

identifier digit

nondigit: one of

_abcdefghijklmnopqrstuvwxyz

ABCDEFGHIJKLMNOPQRSTUVWXYZ
digit: one of

0123456789

The first character of an identifier name must be a nondigit (that is, the first
character must be an underscore or an uppercase or lowercase letter). ANSI
allows six significant characters in an external identifier's name and 31 for
names of internal (within a function) identifiers. External identifiers (ones
declared at global scope or declared with storage class extern) may be
subject to additional naming restrictions because these identifiers have to be
processed by other software such as linkers.

Microsoft Specific

Although ANSI allows 6 significant characters in external identifier names


and 31 for names of internal (within a function) identifiers, the Microsoft C
compiler allows 247 characters in an internal or external identifier name. If
you aren't concerned with ANSI compatibility, you can modify this default to
a smaller or larger number using the /H (restrict length of external names)
option.

END Microsoft Specific

The C compiler considers uppercase and lowercase letters to be distinct


characters. This feature, called "case sensitivity," enables you to create
distinct identifiers that have the same spelling but different cases for one or
more of the letters. For example, each of the following identifiers is unique:

Copy
add
ADD
Add
aDD

Microsoft Specific

Do not select names for identifiers that begin with two underscores or with
an underscore followed by an uppercase letter. The ANSI C standard allows
identifier names that begin with these character combinations to be reserved
for compiler use. Identifiers with file-level scope should also not be named
with an underscore and a lowercase letter as the first two letters. Identifier
names that begin with these characters are also reserved. By convention,
Microsoft uses an underscore and an uppercase letter to begin macro names
and double underscores for Microsoft-specific keyword names. To avoid any
naming conflicts, always select identifier names that do not begin with one
or two underscores, or names that begin with an underscore followed by an
uppercase letter.

END Microsoft Specific

The following are examples of valid identifiers that conform to either ANSI or
Microsoft naming restrictions:

Copy
j
count
temp1
top_of_page
skip12
LastNum

Microsoft Specific

Although identifiers in source files are case sensitive by default, symbols in


object files are not. Microsoft C treats identifiers within a compilation unit as
case sensitive.

The Microsoft linker is case sensitive. You must specify all identifiers
consistently according to case.

The "source character set" is the set of legal characters that can appear in
source files. For Microsoft C, the source set is the standard ASCII character
set. The source character set and execution character set include the ASCII
characters used as escape sequences. See Character Constants for
information about the execution character set.

END Microsoft Specific

An identifier has "scope," which is the region of the program in which it is


known, and "linkage," which determines whether the same name in another
scope refers to the same identifier. These topics are explained in Lifetime,
Scope, Visibility, and Linkage.

Q2. Explain in detail about Operators


In this tutorial you will learn about Operators, Arithmetic operators, Relational Operators,
Logical Operators, Assignment Operators, Increments and Decrement Operators,
Conditional Operators, Bitwise Operators and Special Operators.
Operators Introduction
An operator is a symbol which helps the user to command the computer to do a certain
mathematical or logical manipulations. Operators are used in C language program to
operate on data and variables. C has a rich set of operators which can be classified as

1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators

1. Arithmetic Operators
All the basic arithmetic operations can be carried out in C. All the operators have almost the
same meaning as in other languages. Both unary and binary operations are available in
C language. Unary operations operate on a singe operand, therefore the number 5 when
operated by unary – will have the value –5.

Arithmetic Operators
Operator Meaning

+ Addition or Unary Plus

– Subtraction or Unary Minus

* Multiplication

/ Division

% Modulus Operator

Examples of arithmetic operators are

x + y
x - y
-x + y
a * b + c
-a * b

etc.,

here a, b, c, x, y are known as operands. The modulus operator is a special operator in


C languagewhich evaluates the remainder of the operands after division.
Example

.
#include //include header file stdio.h
void main() //tell the compiler the start of the program
{
int numb1, num2, sum, sub, mul, div, mod; //declaration of variables
scanf (“%d %d”, &num1, &num2); //inputs the operands

sum = num1+num2; //addition of numbers and storing in sum.


printf(“\n Thu sum is = %d”, sum); //display the output

sub = num1-num2; //subtraction of numbers and storing in sub.


printf(“\n Thu difference is = %d”, sub); //display the output

mul = num1*num2; //multiplication of numbers and storing in mul.


printf(“\n Thu product is = %d”, mul); //display the output

div = num1/num2; //division of numbers and storing in div.


printf(“\n Thu division is = %d”, div); //display the output

mod = num1%num2; //modulus of numbers and storing in mod.


printf(“\n Thu modulus is = %d”, mod); //display the output
}
.

Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers than such an
operation is called as integer arithmetic. It always gives an integer as the result. Let x =
27 and y = 5 be 2 integer numbers. Then the integer operation leads to the following
results.

x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5

In integer division the fractional part is truncated.

Floating point arithmetic


When an arithmetic operation is preformed on two real numbers or fraction numbers such
an operation is called floating point arithmetic. The floating point results can be truncated
according to the properties requirement. The remainder operator is not applicable for
floating point arithmetic operands.

Let x = 14.0 and y = 4.0 then

x + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50
Mixed mode arithmetic
When one of the operand is real and other is an integer and if the arithmetic operation is
carried out on these 2 operands then it is called as mixed mode arithmetic. If any one
operand is of real type then the result will always be real thus 15/10.0 = 1.5

2. Relational Operators
Often it is required to compare the relationship between operands and bring out a decision
and program accordingly. This is when the relational operator come into picture. C supports
the following relationaloperators.

Operator Meaning

< is less than

<= is less than or equal to

> is greater than

>= is greater than or equal to

== is equal to

!= is not equal to

It is required to compare the marks of 2 students, salary of 2 persons, we can compare


them usingrelational operators.

A simple relational expression contains only one relational operator and takes the following
form.

exp1 relational operator exp2

Where exp1 and exp2 are expressions, which may be simple constants, variables or
combination of them. Given below is a list of examples of relational expressions and
evaluated values.

6.5 <= 25 TRUE


-65 > 0 FALSE
10 < 7 + 5 TRUE

Relational expressions are used in decision making statements of C language such as if,
while and forstatements to decide the course of action of a running program.

3. Logical Operators
C has the following logical operators, they compare or evaluate logical
and relational expressions.
Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT

Logical AND (&&)


This operator is used to evaluate 2 conditions or expressions with relational operators
simultaneously. If both the expressions to the left and to the right of the logical operator is
true then the whole compound expression is true.

Example

a > b && x = = 10

The expression to the left is a > b and that on the right is x == 10 the whole expression is
true only if both expressions are true i.e., if a is greater than b and x is equal to 10.

Logical OR (||)
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one
of the 2 expressions is true.

Example

a < m || a < n

The expression evaluates to true if any one of them is true or if both of them are true. It
evaluates to true if a is less than either m or n and when a is less than both m and n.

Logical NOT (!)


The logical not operator takes single expression and evaluates to true if the expression is
false and evaluates to false if the expression is true. In other words it just reverses the
value of the expression.

For example

! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater
than or equal to y

4. Assignment Operators
The Assignment Operator evaluates an expression on the right of the expression and
substitutes it to the value or variable on the left of the expression.

Example
x = a + b

Here the value of a + b is evaluated and substituted to the variable x.

In addition, C has a set of shorthand assignment operators of the form.

var oper = exp;

Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The
operatoroper = is known as shorthand assignment operator

Example

x + = 1 is same as x = x + 1

The commonly used shorthand assignment operators are as follows

Shorthand assignment operators

Statement with Statement with


simple shorthand operator
assignment operator

a=a+1 a += 1

a=a–1 a -= 1

a = a * (n+1) a *= (n+1)

a = a / (n+1) a /= (n+1)

a=a%b a %= b
Q3. Write a program to accept principle, amount, rate of interest
& compound interest from users and calculate simple interest &
compound interest.

A) To calculate simple interest-


#include <stdio.h>
#include<conio.h>
Void main ()
{
float p,n,r,Si
scanf (“%f%f%f”, &p,&n&r);
Si=p*n*r/100;
Printf (“the simple interest is %f”, Si);
Getch();
}
B)To calculate compound interest
#include <stdio.h>
#include<conio.h>
#include<math.h>
Void main ()
{
float p,n,r,Si,Ci;
scanf (“%f%f%f”, &p,&n&r);
Si=p*n*r/100;
Ci=p*pow(1-r,n);
printf (“the simple interest is %f”, Si);
printf(“the compound interest is %f”,Ci);
Getch();
}
Q4.write a program to accept base & height of a triangle &
calculate area of triangle.
#include <stdio.h>
#include<conio.h>
Void main ()
{
float b,h,a
scanf(“%f%f” &b&h);
a=1/2*b*h;
printf(“the area of triangle is %f”,a);
getch();
}

Q.5 write a short note on HISTORY of C


programming language
C was developed at Bell Laboratories in 1972 by Dennis Ritchie. Many of its
principles and ideas were taken from the earlier language B and B's earlier ancestors
BCPL and CPL. CPL ( Combined Programming Language ) was developed with the
purpose of creating a language that was capable of both high level, machine
independent programming and would still allow the programmer to control the
behavior of individual bits of information. The one major drawback of CPL was that it
was too large for use in many applications. In 1967, BCPL ( Basic CPL ) was created
as a scaled down version of CPL while still retaining its basic features. In 1970, Ken
Thompson, while working at Bell Labs, took this process further by developing the B
language. B was a scaled down version of BCPL written specifically for use in
systems programming. Finally in 1972, a co-worker of Ken Thompson, Dennis
Ritchie, returned some of the generality found in BCPL to the B language in the
process of developing the language we now know as C.
C's power and flexibility soon became apparent. Because of this, the Unix operating
system which was originally written in assembly language, was almost immediately
re-written in C ( only the assembly language code needed to "bootstrap" the C code
was kept ). During the rest of the 1970's, C spread throughout many colleges and
universities because of it's close ties to Unix and the availability of C compilers. Soon,
many different organizations began using their own versions of C causing
compatibility problems. In response to this in 1983, the American National Standards
Institute ( ANSI ) formed a committee to establish a standard definition of C which
became known as ANSI Standard C. Today C is in widespread use with a rich
standard library of functions.

Q6. List and explain with an example


about Data Types
In this tutorial you will learn about C language data types, Primary data type, Integer Type,
Floating Point Types, Void Type, Character Type, Size and Range of Data Types on 16 bit
machine, derived data type, Declaration of Variables, User defined type declaration,
Declaration of Storage Class, auto, static, extern, register, Defining Symbolic Constants,
Declaring Variable as Constant and Volatile Variable
Primary data type
All C Compilers accept the following fundamental data types

1. Integer int

2. Character char

3. Floating Point float

4. Double precision floating point double

5. Void void

The size and range of each data type is given in the table below
DATA TYPE RANGE OF VALUES

char -128 to 127

Int -32768 to +32767

float 3.4 e-38 to 3.4 e+38

double 1.7 e-308 to 1.7 e+308

Integer Type :
Integers are whole numbers with a machine dependent range of values. A good
programming language as to support the programmer by giving a control on a range of
numbers and storage space. C has 3 classes of integer storage namely short int, int and
long int. All of these data types have signed and unsigned forms. A short int requires half
the space than normal integer values. Unsigned numbers are always positive and consume
all the bits for the magnitude of the number. The long and unsigned integers are used to
declare a longer range of values.

Floating Point Types :


Floating point number represents a real number with 6 digits precision. Floating point
numbers are denoted by the keyword float. When the accuracy of the floating point number
is insufficient, we can use the double to define the number. The double is same as float but
with longer precision. To extend the precision further we can use long double which
consumes 80 bits of memory space.

Void Type :
Using void data type, we can specify the type of a function. It is a good practice to avoid
functions that does not return any values to the calling function.

Character Type :
A single character can be defined as a defined as a character type of data. Characters are
usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly
applied to char. While unsigned characters have values between 0 and 255, signed
characters have values from –128 to 127.
Size and Range of Data Types on 16 bit machine.

TYPE SIZE (Bits) Range

Char or Signed Char 8 -128 to 127

Unsigned Char 8 0 to 255

Int or Signed int 16 -32768 to 32767

Unsigned int 16 0 to 65535

Short int or Signed short int 8 -128 to 127

Unsigned short int 8 0 to 255

Long int or signed long int 32 -2147483648 to 2147483647

Unsigned long int 32 0 to 4294967295

Float 32 3.4 e-38 to 3.4 e+38

Double 64 1.7e-308 to 1.7e+308

Long Double 80 3.4 e-4932 to 3.4 e+4932


Declaration of Variables
Every variable used in the program should be declared to the compiler. The declaration does
two things.

1. Tells the compiler the variables name.


2. Specifies what type of data the variable will hold.

The general format of any declaration

datatype v1, v2, v3, ……….. vn;

Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration
statement must end with a semicolon.

Example:

Int sum;
Int number, salary;
Double average, mean;

Datatype Keyword Equivalent

Character char

Unsigned Character unsigned char

Signed Character signed char

Signed Integer signed int (or) int

Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long

UnSigned Integer unsigned int (or) unsigned

UnSigned Short Integer unsigned short int (or) unsigned short

UnSigned Long Integer unsigned long int (or) unsigned long

Floating Point float

Double Precision Floating Point double

Extended Double Precision Floating Point long double

User defined type declaration


In C language a user can define an identifier that represents an existing data type. The user
defined datatype identifier can later be used to declare variables. The general syntax is

typedef type identifier;

here type represents existing data type and ‘identifier’ refers to the ‘row’ name given to
the data type.

Example:

typedef int salary;


typedef float average;

Here salary symbolizes int and average symbolizes float. They can be later used to declare
variables as follows:

Units dept1, dept2;


Average section1, section2;

Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and
section2 are indirectly float data type.
The second type of user defined datatype is enumerated data type which is defined as
follows.

Do while loop
Do while loop statement allows you to execute code block in loop body at least one. Here is do while loop syntax:

view source

print?

1.do {

2. // statements

3.} while (expression);

Here is an example of using do while loop statement:

view source

print?

01.#include <stdio.h>

02.void main(){

03. int x = 5;

04. int i = 0;

05. // using do while loop statement

06. do{

07. i++;

08. printf("%d\n",i);

09. }while(i < x);

10.

11.}

The program above print exactly 5 times as indicated in do while loop body

1
2
3
4
5

You might also like