Вы находитесь на странице: 1из 181

Fundamentals of c programming

K V Ranga Rao
Asst.professor, CSE

K V RANGA RAO V FSTR University 02/27/17


History of C
C language is a structure oriented programming
language, was developed at Bell Laboratories in 1972
by Dennis Ritchie
C features were derived from earlier language called
"B" (BCPL language)
C was invented for implementing UNIX operating
system

K V RANGA RAO V FSTR University 02/27/17


1960 ALGOL International Group

1967 BCPL Martin Richards

B Ken Thompson
1970

Traditional C
1972 Dennis Ritchie

K&R Kernighan & Ritchie


1978

ANSI C
1989 ANSI Committee

ANSI/ISO C ISO Committee


1990
K V RANGA RAO V FSTR University 02/27/17
Basic structure of a C program
Documentation section
Link Section
Definition Section
Global Declaration Section
main() function section
{ Declaration Part
Executable part
}
Sub Program Section
function1
...
Function n

K V RANGA RAO V FSTR University 02/27/17


Basic structure of a c program
Documentation section consists of a set of comment
lines which gives description about the author or
program
Link section provides instructions to the compiler to
link functions from the system library
Definition Section defines all symbolic constants.
The variables that are used in more than one function
are declared in the global declaration section.
Main() function is must in any c program .
sub program section contains all use defined functions
that are called in main() function.
K V RANGA RAO V FSTR University 02/27/17
/* addition of 2 integers* / <- Documentation section
#include<stdio.h> <- Link Section
#define a 5 <- Definition section
#define b 6
void main()
{
int c; // local variable declaration
c=a+b;
printf(addition of 2 integers is %d,c);
}

K V RANGA RAO V FSTR University 02/27/17


Character set
Letters ( AZ, a..z)
Digits (0.9)
Special characters
White spaces

K V RANGA RAO V FSTR University 02/27/17


Special characters
, Comma . Period
; Semicolon : Colon
? Question mark Apostrophe
Quotation mark ! Exclamation mark
| Vertical bar / Slash
\ Back slash ~ Tilde
_ Underscore $ Dollar sign
% Percent sign & Ampersand
^ Caret * Asterisk (or multiplication )
- Minus sign + Plus sign
< Less than sign (or opening angle > Greater than sign (or closing angle
bracket) bracket)
( Left parenthesis ) Right parenthesis
[ Left bracket ] Right bracket
{ Left brace } Right brace
# Number sign
K V RANGA RAO V FSTR University 02/27/17
White spaces
Blank space
Horizontal tab
Carriage return
New Line
Form feed

K V RANGA RAO V FSTR University 02/27/17


Token
Smallest individual unit in a c program.
6 types of tokens
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. operators

K V RANGA RAO V FSTR University 02/27/17


Constants

CONSTANTS

Numeric Constants Character constants

Single String
Integer Real
character constants

K V RANGA RAO V FSTR University 02/27/17


integer constants
Decimal integer constants
-> consists of one or more decimal digits 0 through 9 preceded
by an optional sign.
ex: 0 276 31467 -7123
Octal integer constants
-> consists of digit 0 followed by a sequence of one or more
digits 0 though 7
Hexa decimal integer constants
-> consists of one or more digits 0 through 9 or letters
through a to f or A to F preceded with ox or 0X.
Ex: 0x1F 0x9a2F 0XFFFF
K V RANGA RAO V FSTR University 02/27/17
Real constants
A real constant is a sequence of digits with a decimal point.
It can be expressed in either decimal notation or using
scientific form.
Ex: 17.458 , .083, +247.0 are valid real constants expressed
in decimal notation.
the real constant 215.65 may be written as 2.1565 e 2 in
exponential ( scientific form) form. Its general form is
mantissa e exponent
mantissa can be a real number expressed in decimal
notation or can be an integer. Exponent is an integer with
optional plus or minus sign
K V RANGA RAO V FSTR University 02/27/17
What will be the output of the program?
#include<stdio.h>
void main()
{
float d=2.25;
printf("%e,", d);
printf("%f,", d);
printf("%g,", d);
printf("%lf", d);
}
(a) 2.2, 2.50, 2.50, 2.5 (b).2.2e, 2.25f, 2.00, 2.25
2.250000e+000, 2.250000, 2.25, 2.250000
(d)Kerror
V RANGA RAO V FSTR University 02/27/17
character constants
A single character constant contains a single character
enclosed with in a pair of single quote marks.
Ex: A , 5 + etc
character constants have integer values known as ASCII
values.
ex: printf(%d, a); would print 97.
since each single character constant represents an integer
value, it is possible to perform arithmetic operations on single
character constants.
a string constant is a sequence of characters enclosed in
double quotes. Characters can be letters, digits, special
characters etc. Ex: hai, welcome
K V RANGA RAO V FSTR University 02/27/17
identifier
Every word in c is either a keyword or an identifier.
First character must be an alphabet or an underscore.
Must contain only letters, digits or underscore
Only first 31 characters are significant.
Must not contain a white space.
Ex:
Which of these is an invalid identifier?
a) wd-count b) wd_count
c) w4count d) wdcountabcd

K V RANGA RAO V FSTR University 02/27/17


operators
* Arithmetic operators ( +, -, *, / , % )
* Relational operators ( <, >, <=, >=, ==, !=)
* Logical operators ( &&, ||, !)
* Assignment operators(=, +=, -=, *=, /=.)
* Increment/Decrement operators (++, --)
* conditional operators (?:)
* Bitwise operators ( &, |, ^, <<, >>)
* special operators (comma , size of )

K V RANGA RAO V FSTR University 02/27/17


integer arithmetic
When both operands in a arithmetic expression are integers,
the expression is called arithmetic expression and the
operation is called integer arithmetic.
integer arithmetic always yields an integer value.
Ex: if a=14 and b=4 then a/b=3
During integer division, if sign of one operand is negative,
then the result is machine dependent.
ex: -6/7 may be zero or -1.
during modulo division, the sign of the result is always the
sign of the first operand.
Ex: -14%3 = -2 14% -3 =2
K V RANGA RAO V FSTR University 02/27/17
real and mixed mode arithmetic

An arithmetic operation involving only real operands is


called real arithmetic

Ex: x=6.0/7.0 = 0.857143


y=-2.0/3.0=-0.666667
% can not be used with real operands.
when one of the operands is an integer and another is a real
number, the expression is called mixed mode arithmetic and
the result always will be a real number.
Ex: 15/10.0=1.5
15/10=1
K V RANGA RAO V FSTR University 02/27/17
relational operators
An expression containing a relational operator is termed as
relational expression.
the value of a relational expression is either 0 or 1.
general form of a relational expression is
a.e-1 relational operator a.e-2 where ae-1 and ae-2 are
arithmetic expressions which may be constants, variables or
their combination.
Ex: 4 <= 10
10 < 7+5
* when arithmetic expressions are used on either side of a
relational operator , the a.e will be evaluated first and then
results are RAO
K V RANGA compared.
V FSTR University 02/27/17
Logical operators
Logical operators are
logical AND &&
Logical OR ||
Logical NOT !
an expression which combines two or more relational
expressions is termed as logical expression which yields
either 0 or 1.
ex: (a>=10) && (b<=5)

K V RANGA RAO V FSTR University 02/27/17


Assignment operators
Used to assign the result of an expression to a variable.
Its general form will be
v op = exp; which is equivalent to v = v op exp;
where v is a variable, exp is an expression and op is a
binary arithmetic operator.
x= x+1 is equivalent to x+=1 . += is called as short
hand assignment operator.
Other short hand assignment operators are
-=, *=, /=, %=etc.

K V RANGA RAO V FSTR University 02/27/17


Increment/decrement operators
Increment and decrement operators in c are ++ and --.
++ adds 1 to the operand where as subtracts 1 from the
operand.
Extensively used in for and while loops
when post fix ++ or is used with a variable in an
expression, the expression is evaluated first using the
original value and variable will be incremented/
decremented by 1.
ex: m=5
y=m++;
value of y will be 5 and value of m is incremented to 6.
K V RANGA RAO V FSTR University 02/27/17
When prefix ++ or -- is used with a variable in an
expression, variable will be incremented/ decremented by 1
and then the expression is evaluated using the new value of
the variable

ex: m=5;
y=++m;
in this case value of y will be 6. as m value is incemented
first and assigned to y.

K V RANGA RAO V FSTR University 02/27/17


bitwise operators
Following are the bitwise operators that are used for
manipulation of data at bit level.
& bitwise AND
| ... Bitwise OR
^ bitwise XOR
<< .. Shift left
>> .. Shift right

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following
main()
{
int x=100,y;
y= 10 + x++;
printf(%d, y);
}
Find the output of the following
main()
{
int x=5,y=10,z=10;
x=y==z; printf (%d , x);
}
K V RANGA RAO V FSTR University 02/27/17
Which of the following is correct .

(a) a>b? c=30;


(b) max= a>b ?a>c ? a: c: b>c ? b:c
(c) return (a>b)?(a:b);

* What will be the output of the following statement ?


printf( 3 + "goodbye");

a) goodbye b) odbye c) bye d) dbye

K V RANGA RAO V FSTR University 02/27/17


What will be the output if the following program is
executed.
void main()
{
int i=4,x;
x= ++i + ++i + ++i ;
printf (%d,x);
}

(a) 21 (b) 12 18 (d) none

K V RANGA RAO V FSTR University 02/27/17


What will be the output if the following program is executed.
void main()
{
int a=10;
printf (%d %d%d, a, a++, ++a));
}
(a) 12 11 11 (b) 12 10 10 11 11 12 (d) 10 10 12

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following.
main()
{
int i=14,j;
j=i>>2;
printf(%d ,j);
}
a. 3 b. 14 c. 2 d. none

K V RANGA RAO V FSTR University 02/27/17


#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
}
What will output when you compile and run the above code?

(a)Garbage value garbage value garbage value


(b)5 6 11
(c)11 6 5
(d)Compiler error
K V RANGA RAO V FSTR University 02/27/17
#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("VIGNANUNIVERSITY"));
getch();
}
What will output when you compile and run the above
code?

(a)16VIGNANUNIVERSITY
(b)VIGNANUNIVERSITY16
(c)Garbage VIGNANUNIVERSITY
(d)Compiler error
K V RANGA RAO V FSTR University 02/27/17
Evaluation of arithmetic expression
An arithmetic expression is a combination of variables,
constants and operators arranged as per syntax of the
language.
Expressions are evaluated using assignment statement
of the form variable= expression
The expression is evaluated first and the result replaces
the previous value of the variable on the left hand side.

K V RANGA RAO V FSTR University 02/27/17


Operator precedence and Associativity
An arithmetic expression may contain two or more
operators. The order of priority in which the operations
are performed in an expression is called precedence of
operators.
The operators of same precedence are evaluated from
either left to right or right to left depending on the
precedence level and this is the associativity of
operators

K V RANGA RAO V FSTR University 02/27/17


Operator precedence and associativity table
Operator associativity precedence
(), [] L to R 1

+,-(unary),++,
--, !, ~, *(pointer), R to L 2
&, sizeof, (type)

*,/, % L to R 3

+,- L to R 4

<<, >> L to R 5
<, <=, > >= L to R 6
++ , != L to R 7
K V RANGA RAO V FSTR University 02/27/17
Operator associativity precedence

& L to R 8
^ L to R 9
| L to R 10
&& L to R 11
|| L to R 12
?: R to L 13
=,*=,/=,%-,+=
-=,&=,^=,|=,<<=
>>= R to L 14
, L to R 15
K V RANGA RAO V FSTR University 02/27/17
The expression a= 30 * 1000 + 2768 evaluates to
(a) 32768 (b) -32768 113040 (d) 0
the expression x = 4 + 2 % -8 evaluates
(a) -6 (b) 6 4 (d) none
the expression a= 7/ 22 * (3.14 +2) * 3/5 evaluates to
(a) 8.28 (b) 6.28 3.14 (d) 0
which of the following arithmetic expressions are valid.
(a) 25/3%2 -14%3 (e) 7.5 %3
(b) +9/4 + 5 (d) 15.25 +-5.0 (f) (5/3)*3+5%3
What will be the output of the following arithmetic expression ?
5+3*2%10-8*6

a) -37 b) -42 c) -32 d) -28


K V RANGA RAO V FSTR University 02/27/17
key words
int float char struct
short double void typedef
long switch if enum
signed break else union
unsigned case for volatile
auto continue goto while
static const return
register default sizeof
extern do

K V RANGA RAO V FSTR University 02/27/17


Modifiers
The amount of memory space to be allocated for a
variable is derived by modifiers
Modifiers are prefixed with basic data types to modify
(either increase or decrease) the amount of storage
space allocated to a variable
For example, storage space for int data type is 4 byte
for 32 bit processor. We can increase the range by
using long int which is 8 byte. We can decrease the
range by using short int which is 2 byte.

K V RANGA RAO V FSTR University 02/27/17


Modifiers
There are 5 modifiers available in C language. They are,
short
long
signed
unsigned
long long

K V RANGA RAO V FSTR University 02/27/17


C Scopes
By Scope, we mean accessibility and visibility of the
variables at different points in the program
A variable or a constant in C has four types of Scope
a. Block scope
b. Function Scope
c. Program scope
d. File scope

K V RANGA RAO V FSTR University 02/27/17


Block scope
A block refers to any sets of statements enclosed in
braces ({ and })
A variable declared within a block has block scope
If a variable is declared with in a statement block then
as soon as control exits that block, variable will cease
to exist
Such a variable is called as local variable

K V RANGA RAO V FSTR University 02/27/17


Block Scope Example

K V RANGA RAO V FSTR University 02/27/17


#include <stdio.h>
void main()
{
int x=10;
int i=0;
printf (the value of x outside while loop is %d, x);
while (i<3)
{
int x=i ;
printf(value of x inside while loop is %d, x);
i++; }
printf(value of x outside while loop is %d,x);
}
K V RANGA RAO V FSTR University 02/27/17
output of the example
value of x outside while loop is 10
value of x inside while loop is 0
value of x inside while loop is 1
value of x inside while loop is 2
value of x outside while loop is 10
Note :
1. variables declared with same name as those in outer
block, masks the outer block variables while executing
the inner block
2. In nested blocks, variables declared outside the inner
blocks are accessible to nested blocks , provided these
variables are not re declared with in inner block
K V RANGA RAO V FSTR University 02/27/17
Function Scope
Function scope indicates that a variable is active and
visible from the beginning to the end of a function
In C , only go to label has function scope
Example : void main()
{

.
loop: /* go to label has function scope */
.
go to loop;

}
K V RANGA RAO V FSTR University 02/27/17
program Scope
A variable is said to have program scope when it is
declared outside a function
int x = 0; /* program scope */
float y = 0.0; /* program scope */
int main()
{
int i; /* block scope */
...
return 0;
}
Here the int variable x and the float variable y have
program scope.V FSTR University
K V RANGA RAO 02/27/17
Variables with program scope are also called global
variables, which are visible among different files.
These files are the entire source files that make up an
executable program.
Note that a global variable is declared with an
initializer outside a function.

K V RANGA RAO V FSTR University 02/27/17


#include <stdio.h>
int x = 1234; /* program scope */
double y = 1.234567; /* program scope */
void function_1()
{
printf("From function_1:\n x=%d, y=%f\n", x, y);
}
main()
{ int x = 4321; /* block scope 1*/
int y=7.654312; /* block scope 1*/
function_1();
printf("Within the main block:\n x=%d, y=%f\n", x, y); /*
a nested block */
K V RANGA RAO V FSTR University 02/27/17
{
double y = 7.654321; /* block scope 2 */
function_1();
printf("Within the nested block:\n x=%d, y=%f\n", x, y); }
return 0;
}
output:
From function_1:
x=1234, y=1.234567
Within the main block: x=4321, y=1.234567
From function_1: x=1234, y=1.234567
Within the nested block: x=4321, y=7.654321
K V RANGA RAO V FSTR University 02/27/17
File Scope
When a global variable is accessible until the end of
the file, variable is said to have file scope
To allow a variable to have file scope, declare that
variable with static key word before specifying its data
type such as
static int x =10;
A global static variable can be sued any where from
the file in which it was declared but it is not accessible
by any other files
A variable with file scope is visible from its
declaration point to the end of the file

K V RANGA RAO V FSTR University 02/27/17


File Scope
int x = 0; /* program scope */
static int y = 0; /* file scope */
static float z = 0.0; /* file scope */
int main()
{
int i; /* block scope */
...
return 0;
}
Here the int variable y and the float variable z both have
file scope.
K V RANGA RAO V FSTR University 02/27/17
Data types
ANSI c supports three types of data types.
primary data types
User defined data types
Derived data types

K V RANGA RAO V FSTR University 02/27/17


Primary Data types
integer character

Signed unsigned char


int unsigned int signed char
Short int unsigned short int unsigned char
Long int unsigned long int
float

float
double
long double

K V RANGA RAO V FSTR University float void 02/27/17


Data Type Bytes Range
char/ signed char 1 -128 to 127
un signed char 1 0 to 255
int/ signed int 2 -32768 to 32767
un signed int 2 0 to 65535
short int/signed short int 2 -32768 to 32767
un signed short int 2 0 to 65535
long int/ signed long int 4 -2147483648 to 2147483647
un signed long int 4 0 to 4294967295
float 4 3.4 e -38 to 3.4 e +38
double 8 1.7 e -308 to 1.7 e +308
long double 10 3.4 e -4932 to 1.1 e +4932

K V RANGA RAO V FSTR University 02/27/17


Data Types & Format specifiers
Data Type Format Specifier

char/ signed char %c

un signed char %c

int/ short int %d, %i

un signed int /unsigned short int %u

long int/signed long int %ld

un signed long int %lu

float %e, %f, %g

double %lf
K V RANGA RAO V FSTR University 02/27/17
User Defined data types
typedef

the purpose of type def is to give a new name to the


existing data type. Its general form is
typedef data type identifier
EX: typedef int x;
if the above statement is executed , we can replace
the data type int in the statement int y; as follows
x y;

K V RANGA RAO V FSTR University 02/27/17


enumerated data type
General form is
enum identifier { value1,.value n};
enum identifier v1,v2,vn;
what will be the output of the following
void main()
{
enum color { red, green=-20, blue, yellow};
enum color x;
x=yellow;
printf( %d,x);
}
(a) -22 (b) -18 1 (d) compiler error
K V RANGA RAO V FSTR University 02/27/17
Derived data types
Arrays
Pointers
Structures
unions

K V RANGA RAO V FSTR University 02/27/17


Type Qualifiers
The keywords which are used to modify the properties
of a variable are called type qualifiers.
C defines type qualifiers that control how variables
may be accessed or modified. C defines two of these
qualifiers:
1) const
2) volatile

K V RANGA RAO V FSTR University 02/27/17


const
Constants are also like normal variables. But, only
difference is, their values cant be modified by the
program once they are defined.
They refer to fixed values. They are also called as
literals
const data_type variable_name; (or) const data_type
*variable_name.
When any variable is not qualified by const variable
then its default qualifier is not const

K V RANGA RAO V FSTR University 02/27/17


Example
What will be output of following program?
#include<stdio.h>
int main()
{
const int a=5;
a++;
printf(%d, a);
}
Output:
Compiler error, we cannot modify const variable

K V RANGA RAO V FSTR University 02/27/17


Volatile
The modifier volatile tells the compiler that a
variable's value may be changed in ways not explicitly
specified by the program.
Syntax:
volatile data_type variable_name; (or) volatile data_type
*variable_name;

When any variable has qualified by volatile keyword


in declaration statement then value of variable can be
changed by any external device or hardware interrupt

K V RANGA RAO V FSTR University 02/27/17


What is meaning of the declaration:
const volatile int a=6;
Answer:
Value of variable cannot be changed by program (due
to const) but its value can be changed by external
device or hardware interrupt (due to volatile).

K V RANGA RAO V FSTR University 02/27/17


Storage Classes in C:
A storage class defines the scope (visibility) and life time of
variables and/or functions within a C Program.

Scope: The Scope of a variable is the part of a program in


which the variable may be visible or available.

Visibility: The program ability to access a variable from the


memory.

Life time: The Lifetime of a variable is the duration of time


in which a variable exists in the memory during execution.
K V RANGA RAO V FSTR University 02/27/17
Storage Classes in C
C supports four storage class specifiers
Auto
register
static
extern

K V RANGA RAO V FSTR University 02/27/17


Auto storage class
Automatic variables are declared inside a function in which they
are to be utilized.
Ex: auto int i, j ;
Storage : Memory.
Default initial value: An unpredictable value, which is often called
a garbage value.
Scope: Local to the block in which the variable is defined.
Life: Till the control remains within the block in which the variable
is defined.

K V RANGA RAO V FSTR University 02/27/17


Register Storage Class

A register is a small amount of storage available on the


CPU whose contents can be accessed more quickly
than storage available elsewhere.

Ex: register int i ;

K V RANGA RAO V FSTR University 02/27/17


Register Storage Class
Storage: CPU registers.

Default initial value: Garbage value

Scope: Local to the block in which the variable is


defined.

Life: Till the control remains within the block in which


the variable is defined.

K V RANGA RAO V FSTR University 02/27/17


Static Storage Class
the value of static variables persists until the end of the
program.
Ex: static int i=1;

Static variables may be either an internal type or an


external type depending on the place of declaration.
1) Internal static variables (Local)
2) External static variables (Global)

K V RANGA RAO V FSTR University 02/27/17


Static Storage Class
Storage: Memory
Default initial value: Zero
Scope: Local to the block in which the variable is
defined.
Life: Value of the variable persists between different
function calls.

K V RANGA RAO V FSTR University 02/27/17


External Storage Class
extern is the default storage class of all global
variables as well as functions. The life and scope of the
extern (global) variable is throughout the program.
Storage: Memory
Default initial value: Zero
Scope (visibility): Global.
Life: As long as the programs execution doesnt
come to an end.

K V RANGA RAO V FSTR University 02/27/17


scope (visibility) and life of a storage
classes:

Storage How it has Scope Life time


class declared
auto Locally Block Block
Globally - -
register Locally Block Block
Globally - -
static Locally Block Program
Globally File Program
extern Locally Block Program
Globally Program Program

K V RANGA RAO V FSTR University 02/27/17


Control statements
If statement
Switch statement
Conditional operator statement
goto statement

K V RANGA RAO V FSTR University 02/27/17


if statement
Simple if
if.else
nested if..else
else if ladder

K V RANGA RAO V FSTR University 02/27/17


simple if
General form is
if (test expression)
{
statement block;
}
statement x;
When the test expression is true, statement block will be
executed and control reaches statement x. if the test
expression is false, statement block will be skipped
and control jumps to statement x.
K V RANGA RAO V FSTR University 02/27/17
if.else
General form is
if (test expression)
{
statement x-block;
}
else
{
statement y-block;
}
If test condition is true, statement x block will be executed
otherwise statement y block will be executed. In either case,
either x-block or y-block statements will be executed.

K V RANGA RAO V FSTR University 02/27/17


What will be the output for the following.
void main()
{
int a=15,b=10,c=5;
if(a>b>c)
printf(true);
else
printf ( false);
}

K V RANGA RAO V FSTR University 02/27/17


What will be output if you will execute following c code?

#include<stdio.h>
int main(){
float x=12.25, y=13.65;
if(x=y)
printf("x and y are equal");
else
printf("x and y are not equal");

(A) x and y are equal (B) x and y are not equal


(C) It will print nothing (D) Compilation error

K V RANGA RAO V FSTR University 02/27/17


Identify the incorrect one

1.if(c=1)
2.if(c!=3)
3.if(a<b)then
4.if(c==1)

(a) 1 only (b) 1&3 (c) 3 only (d) All of the above

K V RANGA RAO V FSTR University 02/27/17


Nested ifelse
General form is
if( test c ondition1)
{
if (test condition2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
statement x;

K V RANGA RAO V FSTR University 02/27/17


else if ladder
if (condition1)
statement-1;
else if (condition2)
statement-2;
else if (condition3)
statement-3;
.
else
default statement;

statement- x;
K V RANGA RAO V FSTR University 02/27/17
Switch statement

Switch(expression)
{
case value1: statement-1;
break;

case value n : statement n;
break;
default: default statement;
break;
}
K V RANGA RAO V FSTR University 02/27/17
What will be the output of the following program ?
#include <stdio.h>
void main()
{ int a = 2;
switch(a)
{
case 1:
printf("goodbye"); break;
case 2:
continue;
case 3:
printf("bye");
}
}
a) error b) goodbye c) bye d) bye goodbye
K V RANGA RAO V FSTR University 02/27/17
Point out the error, if any, in the program.
main()
{
int i=1;
switch(i)
{
printf(hello);
case 1: printf ( Vignan );
break;
case 2: printf(university);
break;
}
}
K V RANGA RAO V FSTR University 02/27/17
Point out the error, if any in the program
main()
{
int i=1;
switch(i)
{

case 1: printf ( Vignan );


break;
case 1*2+0: printf(university);
break;
}
K V RANGA RAO V FSTR University 02/27/17
Point out the error, if any in the program
main()
{
int i=1;
switch(i)
{
}
printf( Vignan university);
}

K V RANGA RAO V FSTR University 02/27/17


Consider the following program segment

int n,sum=1;
switch(n)
{
case 2:sum=sum+2;
case 3:sum*=2;
break;
default: sum=0;
}
If n=2, what is the value of sum

(a) 0 (b) 6 (c) 3 (d) None of these

K V RANGA RAO V FSTR University 02/27/17


conditional operator
General form is

conditional exp? exp1: exp2;

conditional expression will be evaluated first and if the result


is non zero, exp1 will be evaluated otherwise exp2 will be
evaluated and returned as value of the conditional expression.

K V RANGA RAO V FSTR University 02/27/17


goto statement
goto statement is used to jump control from one point to
another point in the same program.
goto requires a label to identify the place where control is to be
moved.
general form is
goto label; label: statement;
.

label: statement; goto label;

K V RANGA RAO V FSTR University 02/27/17


Point out the error if any in the following program
main()
{
int i=1;
while(i<=5)
{
printf(%d,i);
if(i>2)
goto here;
}
}
fun()
{K V RANGA
here:RAOprintf(Hi); }
V FSTR University 02/27/17
Iterative statements
While
dowhile
for

K V RANGA RAO V FSTR University 02/27/17


while
Syntax for while loop is

while (condition)
{
Body of the loop
}
* While is an entry controlled loop. The test condition
is evaluated first and if the condition is true, statements
in the body of the loop will be executed. Then again
the condition is evaluated and if it is true again body of
the loop will be executed. This process will be
K V RANGA RAO V FSTR University 02/27/17
continued until the condition becomes false
What will be the output of the following.
void main()
{
int i=1;
while(i<=10);
{
printf(%d,i);
i=i+1;
}
getch();
}

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following.
void main()
{
int i=1;
while(i<=32767)
{
printf(%d,i);
i=i+1;
}
getch();
}

K V RANGA RAO V FSTR University 02/27/17


do . while
General form is
do
{
body of the loop
} while (condition);

Do while is an exit controlled loop. First the statements


in the body of the loop will be executed and then
condition will be evaluated. If it is true, again body of
loop will be executed and this process will be
continued until the condition became false
K V RANGA RAO V FSTR University 02/27/17
How many times a do while loop is guaranteed to loop?
(a) 0 (b) 1 infinite (d) variable
what will be the output of the following
void main()
{ int m=0;
do
{
if(m>10)
continue;
m=m+10;
} while(m<50);
printf( %d ,m);
}
K V RANGA RAO V FSTR University 02/27/17
for loop
Syntax of for loop is

for( initialization; test condition; updation)


{
body of the loop
}
* for loop as an entry controlled loop. If the test
condition is false for the first time it self, body of the
loop will not be executed even once.

K V RANGA RAO V FSTR University 02/27/17


Arrays
An array is a fixed size sequential collection of
elements of same data type.

Arrays cal be classified in to

1-D arrays
2-D arrays
Multi dimensional arrays

K V RANGA RAO V FSTR University 02/27/17


1-D arrays
General form for declaring a 1-D array is

type variblename [size];


ex: int x[10]; declares x as an array to contain maximum of
10 integer elements
initialization of 1-d array is
type array name[size]={ list of values};
Ex: int x[3]={0,0,0};
if number of initializers is less than declared size, then the
remaining elements are initialized to zero.
ex: int n[5]={1,2};
The first 2 elements are initialized to 1 and 2 and the
remaining elements are
K V RANGA RAO
initialized to zero.
V FSTR University 02/27/17
What will be the output of the following
void main()
{
int i;
int x[3]={5};
for(i=0;i<=2;i++)
printf(%d,x[i]);
}

(a) 5 garbage garbage (b) 5 0 0 5 NULL NULL


(d) compiler error

K V RANGA RAO V FSTR University 02/27/17


What will happen if in a C program you assign a
value to an array element whose subscript exceeds the
size of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets
overwritten
D. The array size would appropriately grow

K V RANGA RAO V FSTR University 02/27/17


What does the following declaration mean?
int (*ptr)[10];

A. ptr is array of pointers to 10 integers


B. ptr is a pointer to an array of 10 integers
C. ptr is an array of 10 integers
D. ptr is an pointer to array

K V RANGA RAO V FSTR University 02/27/17


In C, if you pass an array as an argument to a function,
what actually gets passed?

A. Value of elements in array


B. First element of the array
C. Base address of the array
D. Address of the last element of array

K V RANGA RAO V FSTR University 02/27/17


Which of the following statements are correct about an
array?
1. The array int num[26]; can store 26 elements.
2. The expression num[1] designates the very first
element in the array.
3. It is necessary to initialize the array at the time of
declaration.
4. The declaration num[SIZE] is allowed if SIZE is a
macro.
A. 1 B. 1,4 C. 2,3 D. 2,4

K V RANGA RAO V FSTR University 02/27/17


.Consider the following program
main()
{
int a[5]={1,3,6,7,0};
int *b;
b=&a[2];
}
The value of b[-1] is

(a) 1 (b) 3 (c) -6 (d) none

K V RANGA RAO V FSTR University 02/27/17


2-D arrays
Two dimensional arrays are declared as
type array name [row size][column size];
Ex: int x[2][3]={0,0,0,1,1,1};
Initializes the elements of first row to zero and second row to
one.
when an array is initialized with values, size of the first
dimension need not be specified.
Ex: int x[][3]={{0,0,0},{1,1,1,}};
If the values are missing in an initializer, they are
automatically set to zero.
ex: int x[2][3]={ {1,1},{2}}; will initialize the first two
elements of first row to one, first element of 2nd row to 2 and
all other elements to zero.
K V RANGA RAO V FSTR University 02/27/17
Which of the following statements are correct about the
program below?
void main()
{ int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{ scanf("%d", arr[i]); printf("%d", arr[i]); } return 0; }
A. The code is erroneous since the subscript for array used in
for loop is in the range 1 to size. B. The code is erroneous
since the values of array are getting scanned through the
loop. C. The code is erroneous since the statement declaring
array is invalid. D. The code is correct and runs successfully.
K V RANGA RAO V FSTR University 02/27/17
#include<stdio.h>
#include<conio.h>
main()
{
int a[3],i;
for(i=0;i<3;i++)
a[i]=i++;
for(i=0;i<3;i++)
printf("\t %d", a[i]);
}
What will be the output?

K V RANGA RAO V FSTR University 02/27/17


What will be the output if we execute the following code
void main()
{
char c=125;
c=c+10;
printf(%d, c)
}
(a) 135
(b) compiler error
-121
(d) -8

K V RANGA RAO V FSTR University 02/27/17


What will be the output if we execute the following code
#include<string.h>
void main()
{
int i=0;
for(;i<=2;)
{
printf(%d, i++);
}
(a) 0 1 2
(b)0 1 2 3
123
(d) compiler errorV FSTR University
K V RANGA RAO 02/27/17
Declaring and initializing string
variables
General form for declaring a string variable is
char string name[ size ];

* size must be equal to the maximum no of characters


in the string plus 1 as compiler automatically appends \
0 at the end of the string
* characters arrays can be initialized similar to the
numeric arrays.
ex: char name [5]={c , z,\0};
char name[5]=cz;
K V RANGA RAO V FSTR University 02/27/17
What will be output of following code?
void main()
{
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
printf("%s",a);
getch();
}

K V RANGA RAO V FSTR University 02/27/17


reading strings from terminal
Scanf can be used with %s format specifier to read a
string of characters.
The problem with scanf is it terminates the input on
the first white space it finds.
Ex: if we give input as NEW DELHI in to the array x,
only the string NEW will be read in to x.
We can specify field width using %ws in scanf for
reading a specified number of characters.
Ex: char name[10];
scanf( %5s,name);
if we give input as krishna , only the string krish
will be stored in name.

K V RANGA RAO V FSTR University 02/27/17


Writing strings to the screen
Printf function with %s format can be used to print
strings to the screen.
Ex: char x[15]= united kingdom;
printf( %15s\n, x);
output: united kingdom
printf( %5s\n , x);
Output: united kingdom
printf( 15.6s\n, x);
output: united
printf(% -15.6s\n , x);
Output: united
K V RANGA RAO V FSTR University 02/27/17
String handling functions
Strcat()
Strcmp()
Strlen()
Strcpy()

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following
void main()
{
char st1[]=Kolkata;
char st2[]=pune;
strcpy(s1,s2);
printf(string1 is %s, s1);
}
* What will be the output of the following.

printf(%d, strcmp(acef , abcd ) );

K V RANGA RAO V FSTR University 02/27/17


Identify the correct statement
(a) a=b=3=4;
(b) a=b=c=d=0;
float a= int b = 3.5;
(d) int a; float b; a=b=3.5;

* which of the following is an invalid expression


(a) +0XAB5
(b) -0525
15-
(d) +a

K V RANGA RAO V FSTR University 02/27/17


main( )
{
unsigned int i=10;
while(i>=0)
{
printf(%u, i);
i--;
}
}
how many times the loop will get executed.
(a) 10 (b) 9 11 (d) infinite

K V RANGA RAO V FSTR University 02/27/17


what will be the output of the following.
void main()
{
char ch;
int i ;
scanf(%c, &i);
scanf( %d, &ch);
printf( %c %d, ch, i);
}
(a)error: suspicious char to int conversion in scanf
(b) error: we may not get input for the second scanf statement
(c) no error
(d) none of the
K V RANGA RAO above V FSTR University 02/27/17
the default return type in the function definition is
(a) void (b) int float (d) char
what will be the output of the following program if value 25
is given to scanf.
void main()
{
int i ;
Printf(%d, scanf(%d, &i));
}
(a)25 (b) 2 1 (d) 5
what will be the output of following.
main()
{ intKx=10, y=20; printf(%d,x,y);}
V RANGA RAO V FSTR University 02/27/17
function
A function is a self contained block of statements that
are used to perform a specific task.
Function types are
Library functions
User defined functions.
Library functions are the functions which are pre
defined.
User defined functions are the functions whose code
must be developed by the user

K V RANGA RAO V FSTR University 02/27/17


Which of the following is correct way to define the function
fun() in the below program?

#include<stdio.h>
void main()
{
int a[3][4];
fun(a);
}
A. void fun(int p[][4]) { } B. void fun(int *p[4]) { }
C. void fun(int *p[][4]) { } D. void fun(int *p[3][4]) { }

K V RANGA RAO V FSTR University 02/27/17


Elements of user defined function
Function definition
Function call
Function declaration

K V RANGA RAO V FSTR University 02/27/17


Function definition
General form of a function definition is

function type function name( parameter list)


{
local variable declaration;
executable statement-1;
.
return statement;
}

K V RANGA RAO V FSTR University 02/27/17


Function call
A function can be called by using the function name
followed by a list of actual parameters , if any, enclosed in
parentheses.

Ex:
main()
{
int x;
x=add(10,5); /* function call */
printf( %d ,x);
}

K V RANGA RAO V FSTR University 02/27/17


Function declaration
Also known s function prototype
Consists of
* function (return )type
* function name
* parameter list
* terminating semicolon
General form is
function type function name( parameter list) ;
Ex: int add(int x, int y);

K V RANGA RAO V FSTR University 02/27/17


What is true about the following c functions.

(a) need not return any value


(b) should always return an integer
should always return more than one value
(d) should always return any value

K V RANGA RAO V FSTR University 02/27/17


Categories of functions
with arguments, return a value
With arguments, no return of value
With out arguments, return a value
With out arguments, no return of value
Returns multiple values

K V RANGA RAO V FSTR University 02/27/17


with arguments, return a value
int add(int,int);
void main()
{
int a=5,b=6,c;
clrscr();
c=add(a,b);
printf(addition is %d,c);
}
int add(int x, int y)
{
return (x+y );
} K V RANGA RAO V FSTR University 02/27/17
With arguments, no return value
void add(int,int);
void main()
{
int a=5,b=6,c;
clrscr();
add(a,b);
printf(addition is %d,c);
}
void add(int x, int y)
{
printf(%d, x+y );
} K V RANGA RAO V FSTR University 02/27/17
With out arguments, returns a value
int add();
void main()
{
int c;
clrscr();
c=add();
printf(addition is %d,c);
}
int add()
{
int x=5,y=6;
printf(%d, x+y );
K V RANGA RAO V FSTR University 02/27/17
}
Point out the error in the program
f(int a, int b)
{
int a;
a = 20;
return a;
}
(a). Missing parenthesis in return statement
(b) The function should be defined as int f(int a, int b)
Redeclaration of a
(d) None of above

K V RANGA RAO V FSTR University 02/27/17


what will be the output of the following.
int f(int);
int main()
{
int b;
b = f(20); printf("%d\n", b); }
int f(int a)
{
a > 20? return(10): return(20);
}
A. Error: Prototype declaration B. No error C. Error: return
statement cannot be used with conditional operators D. None
of above
K V RANGA RAO V FSTR University 02/27/17
Point out the error in the program
void f();
void main()
{
int a=10;
a = f();
printf("%d\n", a);
}
void f() { printf("Hi"); }
A. Error: Not allowed assignment
B. Error: Doesn't print anything
C. No error
D. None of above
K V RANGA RAO V FSTR University 02/27/17
With out arguments, without return value
void add();
void main()
{
int c;
clrscr();
add();
}
void add()
{
int x=5,y=6;
printf(%d, x+y );
} K V RANGA RAO V FSTR University 02/27/17
Function that returns multiple values
int xyz(int *, int *);
void main()
{
int a, b, c, d;
clrscr();
a=5; b=10;
xyz(a, b, &c, &d);
printf( sum is %d difference is %d, c, d);
}
int xyz(int p, int q, int *sum, int *diff)
{
sum = p + q;
diff=p-q; }

K V RANGA RAO V FSTR University 02/27/17


int fun(int);
void main()
{
int i=3;
i=fun(i);
i=fun(i);
printf("i is %d",i);
}
fun(int i)
{
if(i%2)
return 0;
else
returnK V1;RANGA
} what
RAO will be theUniversity
V FSTR output? 02/27/17
storage classes
a variables storage class describes
Where the variable will be stored
The initial value of the variable
Scope of the variable
How long would the variable exists
there are 4 storage classes in c
Automatic
External
Static
Register
K V RANGA RAO V FSTR University 02/27/17
automatic

Storage : memory

Default initial value: garbage value

Scope: local to the block in which variable is defined.

Life: till the control remains with in the block in which


the variable is declared.

K V RANGA RAO V FSTR University 02/27/17


void increment();
Void main()
{
increment();
increment();
increment();
}
void increment()
{
auto int i=1;
printf(%d, i);
i=i+1;
} what will be the output?

K V RANGA RAO V FSTR University 02/27/17


register

Storage : cpu registers

Default initial value: garbage value

Scope: local to the block in which variable is defined.

Life: till the control remains with in the block in which


the variable is declared.

K V RANGA RAO V FSTR University 02/27/17


static
Storage : memory

Default initial value: zero

Scope: local to the block in which variable is defined.

Life: value of the variable persists between different


function calls.

K V RANGA RAO V FSTR University 02/27/17


void increment();
Void main()
{
increment();
increment();
increment();
}
void increment()
{
static int i=1;
printf(%d, i);
i=i+1;
} Kwhat will
V RANGA RAObe the output
V FSTR University 02/27/17
extern
Storage : memory

Default initial value: zero

Scope: global

Life: as long as program execution does not come to an


end.

K V RANGA RAO V FSTR University 02/27/17


int i;
void main()
{
printf(%d,i);
increment();
decrement();
}
void increment()
{ i=i+1;
printf(%d,i);
}
void decrement()
{ i=i-1; printf(%d,i); } find the output

K V RANGA RAO V FSTR University 02/27/17


What will be the output for the following.
int x=10;
void display();
void main()
{
int x=20;
printf(%d,x);
display();
}
void display()
{
printf(%d,x);
}K V RANGA RAO V FSTR University 02/27/17
int x=21;
void main()
{
extern int y;
printf(%d %d,x,y);
}
int y=31;

What will be the output?

K V RANGA RAO V FSTR University 02/27/17


Which of the following is true about the automatic variables
with in a function.

(a)its type must be declared before using the variable


(b)They are local
(c) they are global.

Identify the incorrect statement


(a)Automatic variables are automatically initialized to zero.
(b) static variables are automatically initialized to zero.
(c) the address of a register variable is not accessible
(d) static variables can not be initialized with any statement

K V RANGA RAO V FSTR University 02/27/17


main()
{
int i=3,x;
while(i>0)
{
x=func(i);
i--;
}
}
int func(int n)
{
static sum=0;
sum=sum + n;
return(sum); }
The final value of x is
(a) 6 (b) 8 (c) 1 (d) 3
K V RANGA RAO V FSTR University 02/27/17
#include <stdio.h>
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( "%d %d\n", x, y );
}
void main()
{
func();
func();
}
what will be the output?

K V RANGA RAO V FSTR University 02/27/17


structures
A structure is a derived data type.
Its general form is
struct tag name
{
data type member-1;
data type membe-2;
.
data type member n;
}

K V RANGA RAO V FSTR University 02/27/17


Accessing structure members
Structure members can be accessed and assigned with
some values by linking with structure variable .
Ex: struct book bank
{
char title[20];
int pages;
float price;
} b1;
we can assign values to structure members as follows.
b1.pages=200;
b1.price=150.50;
strcpy(b1. title,V FSTR
K V RANGA RAO
C);
University 02/27/17
Using pointers also, structure members can be accessed.
Consider the following example.

struct abc
{
int a;
float b;
char c;
} e, *p;
(*p).a=5;
p->c=a;

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following program.
main()
{ struct emp
{
char name[20];
float salary;
};
struct emp e1={dravid };
printf(name is %s and salary is %f, e1.name,e1.salary);
}

K V RANGA RAO V FSTR University 02/27/17


Copying and comparing structure
variables
Two variables of same structure can be copied the
same way as ordinary variables.
If p1 and p2 are two structure variables of same
structure, then the following are valid.
p1=p2, p2=p1
The statements such as p1==p2, p1!=p2 are not
permitted.
Structure variables can be compared by comparing
members individually.

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following program.
main()
{ struct emp
{
char name[20];
int age;
};
struct emp e1={dravid , 23};
struct emp e2;
e2=e1;
if(e1==e2)
printf(equal);
}
K V RANGA RAO V FSTR University 02/27/17
Arrays of structures and arrays with
in structures
In array of structures, each element of the array
represents a structure variable.
Ex: struct class student[100]; defines an array student
that consists of 100 elements.
In arrays with in structures, arrays can be used as
structure members.
Ex: struct marks
{
int no;
float subject[3];
} student[2];
K V RANGA RAO V FSTR University 02/27/17
Structures with in structures
structures with in structures means nesting of structures.
Consider the following example
Struct salary
{
char name[20];
char dept;
struct
{
int DA;
int HRA;
} allowance;
}employee;
K V RANGA RAO V FSTR University 02/27/17
Size of structures
The unary operator sizeof() can be used to find the size
of structures
The expression sizeof(struct x) ; will give no of bytes
required to hold all the members of structure x.
If y is a structure variable of struct x then the
expression sizeof(y) would give the no of bytes
required to hold all the members of structure x

K V RANGA RAO V FSTR University 02/27/17


What will be the output of the following program.
main()
{ struct emp
{
char name[20];
int age;
};
printf( size of the structure is %d, sizeof( struct emp));
}

K V RANGA RAO V FSTR University 02/27/17


Bit fields
General form is
struct tag name
{
data type name1: bit length;
data type name2: bit length;
.
data type name n: bit length;
}
The data type is either an int or signed int or unsigned int

K V RANGA RAO V FSTR University 02/27/17


Scanf can not be used to read values in to a bit field. First
read the value in to a temporary variable and then assign its
value to the bit field.
Ex : struct personal
{
unsigned sex:1;
unsigned age: 7;
unsigned Children:3;
}emp;
scanf(%d%d, & AGE, & CHILDREN);
emp.age=AGE;
emp. Children= CHILDREN;
* Pointers can
K V RANGA RAO not be V
used to access the bit fields.
FSTR University 02/27/17
unions
Similar to structures.
general form is
union tagname
{
data type member1;


};

K V RANGA RAO V FSTR University 02/27/17


unions differ with structures in many aspects.
in structures , every member has individual memory location
where as in case of unions, the memory of the member of
largest data type will be shared by other members.
the size of the structure is the number of bytes required to
store all the elements of the structure. The size of the union is
the size of the member of largest data type.

K V RANGA RAO V FSTR University 02/27/17


in structures, all members can be initialized at a time where as in
unions, we can initialize the first member of the union and other
elements will be initialized in sequential statements.
union x
{
int a;
float b;
char c;
}p={10};
printf(a value is %d, p.a);
p.b=2.3;
printf(value of b is %f ,p.b);

K V RANGA RAO V FSTR University 02/27/17


union {
int no;
char ch;
} u;
u.ch = '2';
u.no = 0;
printf ("%d", u.ch);
What is the output?
a) 2 b) 0 c) null character d) none

K V RANGA RAO V FSTR University 02/27/17


What will be the size of the following union

union abc
{
int x;
float y;
char z;
};

K V RANGA RAO V FSTR University 02/27/17


pointers
Derived data type in c
Pointer is a variable that stores address of another
variable.
Can be used to return multiple values from a function
Reduces length and complexity of programs
Supports dynamic memory management.

K V RANGA RAO V FSTR University 02/27/17


Declaring and initializing pointer variable
General form for pointer declaration is
data type * pt_name;
ex: int *p; declares that p is a pointer variable that points to
an integer data type
* we can use the assignment operator to initialize a pointer
variable as follows
int q;
int *p;
p= &q;

* a pointer variable can be initialized with NULL or 0


int *p=NULL;
int *p=0;
K V RANGA RAO V FSTR University 02/27/17
Pointer Arithmetic
If p1 and p2 are pointers, the following arithmetic
operations can be performed on pointers.
P1+2
p2-4
P1-p2
P1++
Pointers can be compared using relational operators as
follows
P1 == p2
P1!= p2
P1>p2
K V RANGA RAO V FSTR University 02/27/17
Find the output of the following program

main()
{
int x=5, *p;
p=&x
printf("%d",++*p);
}
(a) 5 (b) 6 (c) 0 (d) none of these

K V RANGA RAO V FSTR University 02/27/17


scale factor
When we increment a pointer, its value is increased by
length of the data type that the pointer points to. This
length is called as scale factor
Ex: if p is an integer pointer with initial value 28, after
the operation p= p+1 ,the value of p1 will be 30 and
not 29.

K V RANGA RAO V FSTR University 02/27/17


Pointers and arrays
When an array is declared, compiler allocates a base
address and sufficient storage in contiguous memory
locations.
ex: int x[5]={1,2,3,4,5};
int *p;
p=x;
this is equivalent to p= & x[0]

K V RANGA RAO V FSTR University 02/27/17


Files
A file is a place on the disk where a group of related
data is stored.
Basic operations on files are
* creating a file
* opening a file
* reading data from a file
* writing data to a file
* closing a file

K V RANGA RAO V FSTR University 02/27/17


Input/output operations on files
Putc() and getc() - to write and read a character to and
from a file
Putw() and getw()- - to write and read an integer to
and from a file
fopen () to create a new file or to open an existing
file
fclose() to close the file that has been opened for use
fscanf() and fprintf() to read set of data values from
a file and to write a set of data values to a file

K V RANGA RAO V FSTR University 02/27/17


Random access to files
ftell() gives the current position in the file
n= ftell(fp); n would give the current position in
bytes.
rewind() set the position to beginning of the file
rewind(fp); rewind takes the file pointer to the
beginning of the file.
fseek() set the position to the desired point in the file
fseek(file pointer, .offset , position);
ex: fseek(fp , -m, 2); means go back ward by m
bytes from the end of file.

K V RANGA RAO V FSTR University 02/27/17


Dynamic memory allocation
Process of allocating memory at runtime
Malloc allocates requested size of bytes and returns
a pointer to the first byte of allocated space.
ptr= (cast type *)malloc(byte size);
Calloc allocates memory for an array of elements,
initializes them to zero and returns a pointer to the
memory.
ptr= (cast type *)calloc(n, byte size);
Free- frees previously allocated memory
free(ptr);
Realloc modifies size of previously allocated
memory.
ptr= (cast type *)realloc(ptr, new size);
K V RANGA RAO V FSTR University 02/27/17
pick out the add one out

a. malloc()
b. calloc()
c. free()
d. realloc()

K V RANGA RAO V FSTR University 02/27/17


. Point out the error/warning in the program?
#include<stdio.h>
int main()
{
unsigned char ch;
FILE *fp;
fp=fopen("trial", "r");
while((ch = getc(fp))!=EOF)
printf("%c", ch);
fclose(fp);
}
A. Error: in unsigned char declaration B. Error: while
statement C. No error D. prints all characters in file "trial"
K V RANGA RAO V FSTR University 02/27/17
What will be the output of the following
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen(trail, r);
fseek(fp,20,SEEK_SET);
fclose(fp);
}
(a)Un recognized word SEEK_SET
(b) fseek() long off set value
(c) no error
(d) none of the
K V RANGA RAOabove V FSTR University 02/27/17

Вам также может понравиться