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

C PROGRAMMING

BASIC TYPES AND OPERATORS:


1. Where is a C program born?
The basic control of a computer rests with its operating system. This is a layer of
software which drives the hardware and provides users with a comfortable
environment in which to work. An operating system has two main components
which are of interest to users: a user interface (often a command language) and
a filing system. The operating system is the route to all input and output, whether it
is to a screen or to files on a disk. A programming language has to get at this input
and output easily so that programs can send out and receive messages from the
user and it has to be in contact with the operating system in order to do this, and
programming language does this.
2. Wha is C?
is what it is called as !ompi"e# "ang$age. !t is a !omp$er programming
"ang$age that can be used to create lists of instructions for a computer to follow.
"nce a program is written, it must run through a C !ompi"er to turn the program
into an e%e!$ab"e that the computer can run.
&. Wha is a !ompi"er?
ompilers translate source code from a high level programming language to a
lower level language.
'. Wha is a so$r!e !o#e?
#ource code is a te$t written in a computer programming language. #ource code is
primarily used as input to the process that produces an e$ecutable program.
(. Wha is a "ibrar)?
A "ibrar) is simply a package of code that someone else has written to make your
life easier. !t has a set of functions that may be used by a program and declare
special data types. has a large number of standard libraries like stdio, including
string, time and math libraries.
*. Wha is a +ariab"e?
"ne of the fundamental things we need to be able to do in our applications is
temporarily store data. %e do these using variables, which are containers that can
hold various types of data and be manipulated in various ways.
%e use variables to store all sorts of data, but we must first tell the compiler what we are
going to store in it. &ere are several of the most important variables that you should know
about for now:
in ' for storing integers (numbers with no decimal point)
!har ' for storing a character
,"oa ' for storing numbers with decimal points
#o$b"e ' same as a float but double the accuracy
-. .o/ o name0 #e!"are an# iniia"i1e a +ariab"e?
The variable name can contain letters, digits and the underscore but the first letter
has to be a letter or the underscore.
To declare a variable we specify its name and kind of data type it can store. The
variable declaration always ends with a semicolon.
A variable can be initiali(ed when it is declared or it can be initiali(ed at any point
in a program before using it.
2. Wha are he T)pes o, +ariab"es?
1. 3o!a" +ariab"e: )ariables declared within the function.
*. 4"oba" 5ariab"e: variables known throughout the program. They are
declared outside all function and conventionally +ust after include
statements.
,. Parameer 5ariab"e: They are used for passing values between functions.
Their main +ob is to automatically initiali(e depending upon the data
processed
2. Wha are !onsans?
onstants are values that never change. The constant may be character constant,
string constant, integer constant and floating point constant.
A char constant is written with single -uotes (.) like .A. or .(.. The char constant .A.
is really +ust a synonym for the ordinary integer value /0 which is the A#!! value
for uppercase .A.. There are special case char constants, such as .1t. for tab, for
characters which is not convenient to type on a keyboard.
67. Wha is an I#eni,ier?
An identifier is a name. !t can be the name of a variable, function, a structure or
union, a member of a struct, union, etc.
66. Wha are he r$"es o be ,o""o/e# /hi"e !reaing an i#eni,ier?
There are some rules that must be followed when creating an identifier. An
identifier is a se-uence of one or more characters. !t must start with a non2digit
character3 it can use upper and lower case a2( characters or universal character
codes or an underscore character (4). After the first character, digits 526 may also
be used.
62. Wha is a )pe 8$a"i,ier?
They are used to -ualify types, modifying the properties of variables in certain
ways. There are three types
The -ualifier .const. is most often used in modern programs, and probably best
understood. The addition of a .const. -ualifier indicates that the (relevant part of
the) program may not modify the variable.
The -ualifier .volatile. indicates to the compiler, that a variable may be modified
outside the scope of the program.
The -ualifier 78estrict9 is one of the newest features added to . !t is applicable
only to pointers. A restrict pointer is the only mean by which the data pointed to
that ob+ect can be accessed.
6&. Wha is a sorage !"ass spe!i,ier?
#torage class specifier when applied to a variable tells the compiler how to store
that variable. There are four storage class specifiers in .
1. :$tern
*. #tatic
,. 8egister
;. Typedef
E%ern: There are three general use of e$tern. !nitially it was used to before
functions to indicate the function has global scope, but now, all the functions are
automatically put in global scope and this use is now virtually no e$istent.
#econd use of e$tern is to declare that a global variable used in the file is from an
e$ternal file.
The third use is the commonly used one and it enables the programmer to use a
variable without defining by marking it as e$tern
Sai!: !t9s a high utility word often used in programming.
%hen applied to global variables, its scope is limited to file only i.e. other files
included in the program has no clue of its e$istence.
%hen applied to local variables, the variable is stored in global variable memory
space and not on stack.
Regiser: This is a compiler dependant keyword. <sing the register keyword, the
program runs faster since a register variable is stored in =< register instead of
8A>.
T)pe#e,: !t is a controversial storage class specifier since it doesn?t allocate
storage, neither defines or modifies the type of storage when applied to a variable.
#till it is defined in standard . This has two advantages.
!t makes the code source portable since we use own name for code generated by
compiler for a particular environment
!t creates self documenting code, making the code easier to understand.
62. Wha is a #aa )pe?
@ata types are used to store various types of data that is processed by program. C
supports various data types such as character, integer and floating-point
types

6&. Wha is Chara!er #aa )pe?
stores character type internally as an integer. :ach character has A bits so we can
have *0/ different characters values (52*00). Bow, how did this *0/ characters
comeC
:ach character has A bits, and there are only * bytes i.e. 5 and 1
1 Dyte E A bits or *FA
*FA E *0/
and that is split that from 21*A G 1*H
S)na%: char Ivariable nameJ3 like
char ch E 7a93
6'. Wha is Ineger Daa T)pe?
!nteger data types are used to store numbers and characters.
S)na%: int Ivariable nameJ3 like
int num13
short int num*3
long int num,3
6(. Wha is 9"oaing:poin Daa T)pe?
The floating2point data types are used to represent floating2point numbers.
Kloating2point data types come in three si(es: float (single precision), double
(double precision), and long double (e$tended precision).
Syntax: float Ivariable nameJ3 like
float num13
double num*3
long double num,3
6*. Wha is 5oi# #aa )pe?
The void type has no values therefore we cannot declare it as variable as we did in
case of integer and float. !t does not return any value.
6-. Wha is he ,$n!ion o, an operaor in C?
C operators can do arithmetic, compare data, modify variables, combine
relationship logically... etc.
"perators taking +ust one operand are called unary operators.
"perators re-uiring two operands are called binary operators.
Arihmei! operaors:
supports almost common arithmetic operators such as L,2,M, N and modulus
operator O. The modulus operator (O) returns the remainder of integer division
calculation. The modulus operator cannot be applied to a double or float.
Assignmen operaor:
!t is the single e-uals sign (E). !t copies the value from its right hand side to the
variable on its left hand side.
Bi/ise operaors:
The bitwise operators perform bitwise2AB@ (;), bitwise2e$clusive2"8 (<), and
bitwise2inclusive2"8 (=) operations. The bitwise operators are preferred in some
conte$ts because bitwise operations are faster than (L) and (2) operations and
significantly faster than (M) and (N) operations.
The operands of bitwise operators must have integral types, but their types can be
different. The commonly used bitwise operators are,
G Ditwise Begation (unary) ' flip 5 to 1 and 1 to 5 throughout
P Ditwise And
Q Ditwise "r
F Ditwise :$clusive "r
JJ 8ight #hift by right hand side (8&#) (divide by power of *)
II Reft #hift by 8&# (multiply by power of *
3ogi!a" operaors an# Re"aiona" operaors:
logical operators are used to connect e$pressions and N or variables to
form compound conditions. The logical operators are


Operaor >eaning
PP logical AB@
QQ logical "8
S logical B"T
8elational operators are used to calculate the relationship between variables,
whereas the logical operators calculate the logic in these relationships. #ome of the
relational operators are,
EE :-ual
SE Bot :-ual
J Treater Than
I Ress Than
JE Treater or :-ual
IE Ress or :-ual
In!remen an# #e!remen operaors:
provides two operators for incrementing and decrementing the value of
variables.
The increment operator LL add 1 to its operand.
The decrement operator 22 subtracts 1 from its operand
The C increment and decrement operators can be used either as prefix
operator or postfix operators.
Compi"e ime operaor: only provides one compile time operator known as
si(eof. #i(eof returns the si(e of the ob+ect passed to it as parameters in
parenthesis.
T)pe !on+ersion: when variables and constants of different types are mi$ed in a
single e$pression, they are all converted into same type.
Ternar) operaor: The conditional operator or the ternary operator (?:) returns
one of two values depending on the value of a Doolean e$pression. The conditional
operator is of the form
condition C first4e$pression: second4e$pression3

6?. Wha is he #i,,eren!e be/een 3ogi!a" operaors an# bi/ise
operaors?
Rogical operators such as !, &&, and || always yield a Boolean value 22
either true or false.
Ditwise operators yield a string of bits stored in a built2in variable of
type char, unsigned char, int and so on.
62. .o/ are !ommens $se,$" o programmers?
omments help programmer e$plain the comple$ logic of the code to make it
easier to maintain and enhance. provides two types of comments are block
comments and inline comments.
Dlock comments begin with NM and end with MN
!nline comments begin with NN and end with your comment.

27. Wha are he )pes o, saemens in C?
1. Con#iiona" Saemen: !t9s also called a selection statement. This
statement depends on the condition. This decides the flow of statements
based on evaluation of the results of condition. if 2 else and switch
statements always come in conditional statements.
I, e"se: i, statement will have some condition (true or false), that is given by
the program .!f the i, condition will not be true than it will read
the e"se statement and e$ecute the e"se statement and go to the ne$t line of the
program for e$ecution.
SYNTA@:
if (condition) statement3
else statement3
"8
if (condition) U
e$presion13
V
else U
e$presion*3
V
S/i!h Saemen: !t has a built in multiple 2 branch structure and work similar to
if else ladder generally. The input value to the switch statement construct is a int or
char variable. Bote that no float or any other data is allowed. This input variable is
compared against a group of integer constant
#WBTAX:
switch (input) U
case constant1 :
statement13
break3
case constant* :
statement*3
break3
.
.
case default :
statement nL13
break3
2. Ieraion Saemens : These are used to run particular block statements
repeatedly or in other words form a loop. ,or0 /hi"e and #o:/hi"e statements
come under this category.
9or: !t provides unmatched fle$ibility and power to the program.
This is an entry controlled looping statement.
!n this loop structure, more than one variable can be initiali(ed. "ne of the most
important features of this loop is that the three actions can be taken at a time like
variable initiali(ation, condition checking and incrementNdecrement.
S)na%:
9or (initialisation3 test2condition3 increNdecre)
U
statements3
V
Whi"e: This is also an entry controlled looping statement. !t is used to repeat a
block of statements until condition becomes true.
S)na%:
%hile (condition)
U
statements3
incrementNdecrement3
V
Do:/hi"e "oop: This is an e$it controlled looping statement. #ometimes, there is
need to e$ecute a block of statements first then to check condition. At that time
such type of a loop is used. !n this, block of statements are e$ecuted first and then
condition is checked.
S)na%:
#o
U
statements3
(incrementNdecrement)3
V/hi"e(condition)3
&. A$mp Saemens: These are used to make the flow of your statements from
one point to another. Dreak, continue, goto and return come under this
statements.
goo Saemen: Toto is used for +umping from one point to another point in your
function. Yump points or way points for goto are marked by label statements. Rabel
statement can be anywhere in the function above or below the goto statement.
S)na%:
goto label13
.
label1 :
.
label* :
goto label*3

BreaB Saemen: Dreak statement when encountered within a loop immediately
terminates the loop by passing condition check and e$ecution transfer to the first
statement after the loop. !n case of switch it terminates the flow of control from
one case to another.


Re$rn : 8eturn statement has a special property that it can return a value with it to
the calling function if the function is declared non 2 void.
Conin$e Saemen: !t is analogues to break statement. <nlike break which
breaks the loop, continue statement forces the ne$t e$ecution of loop bypassing
any code in between

.
26. Wha is a ,$n!ion in C?
Kunction is a block of source code which does one or some tasks with specified
purpose.
22. Wha are he )pes o, ,$n!ions?
There are *(two) types of functions as:
6. B$i" in 9$n!ions
2. Cser De,ine# 9$n!ions
6. B$i" in 9$n!ions:
These functions are also called as .library functions.. These functions are provided
by system. These functions are stored in library files. e.g.
scanf()
printf()
strcpy
strlwr
strcmp
strlen
strcat
2. Cser De,ine# 9$n!ions:
The functions which are created by user for program are known as .<ser defined
functions.
2&. .o/ is a ,$n!ion !a"" #one?
9$n!ion Ca"" b) Passing 5a"$e:
%hen a function is called by passing value of variables then that function is known
as .function call by passing values..
9$n!ion Ca"" b) Re$rning 5a"$e:
%hen a function returns value of variables, which may be a reference to the ob+ect,
then that function is known as .function call by returning values..
9$n!ion Ca"" b) Passing an# Re$rning 5a"$e:
%hen a function passes and returns value of variables then that function is known
as .function call by passing and returning values..
2'. Wha is he sr$!$re o, a ,$n!ion?
9$n!ion hea#er
The general form of function header is:
1 return4type function4name(parameter4list)
9$n!ion bo#)
Kunction body is the place you write your own source code.
Re$rn saemen
8eturn statement returns a value to where it was called. A copy of return value
being return is made automatically
CO>P3E@ DATA TYPES:
1. What is an array and how do you declare it?
An array in language is a collection of similar data2type, means an array can
hold value of a particular data type for which it has been declared.
%e can declare an array by specifying its data type, name and the fi$ed number of
elements the array holds between s-uare brackets immediately following the array
name.
The following illustrates how to declare an array:
data4type array4nameZsi(e[3
2..o/ #o )o$ iniia"i1e an# #e!"are an arra)?
Iniia"i1ing Arra)s
!t is like a variable, an array can be initiali(ed. To initiali(e an array, you provide
initiali(ing values which are enclosed within curly braces in the declaration and
placed following an e-uals sign after the array name.
&ere is an e$ample of initiali(ing an array of integers.
int list[5] = {2,1,3,7,8};
A!!essing Arra) E"emen
Wou can access array elements via inde$es like
array_name[index].
!nde$es of array starts from 5 not 1 so the highest elements of an array
isarray_name[size-1].
&.Wha are he )pes o, arra)?
1. "ne N #ingle @imensional Array
2. Two @imensional Array
,. >ulti dimensional array.
Sing"e D One Dimensiona" Arra):
The array which is used to represent and store data in a linear form is called as
.single or one dimensional array..
S)na%:
Type name [size]
T/o Dimensiona" Arra)s:
The array which is used to represent and store data in a tabular form is called as
.two dimensional array.. #uch type of array specially used to represent data in a
matri$ form.
The following synta$ is used to represent two dimensional array.
S)na%:
Type name [size1] [size2]
#i(e1 is the number of 1@ arrays and si(e* is the si(e of that 1@ array.
>$"i #imensiona" Arra):
allows more than *@ array. The general synta$ for declaring this array is
Type name Zsi(e1[ Zsi(e*[\.Zsi(eX[3
&. Can an arra) be passe# as a parameer?
!n entire array cannot be passed as a parameter. "nly the base address of the
pointer is passed as an argument.
'.Wha is a poiner0 /ha are is ,ea$res?
pointer is a memory address. To access memory address of variable we have to
use pointer.
M =ointer variable should have prefi$ .M..
M ombination of data types is not allowed.
M =ointers are more effective and useful in handling arrays.
M !t supports dynamic memory management.
M !t reduces comple$ity and length of a program.
M !t helps to improve e$ecution speed that results in reducing program e$ecution
time.
(. Wha is a sring?
A string is a collection of characters. Strings are always enlosed in double quotes as
]string4constant].
#trings are used in string handling operations such as,
ounting the length of a string.
omparing two strings.
opying one string to another.
onverting lower case string to upper case.
onverting upper case string to lower case.
Yoining two strings.
8eversing string.
*. .o/ #o )o$ #e!"are a sring?
The string can be declared as follows:
S)na%:
!har string4nmZsi(e[3
E%amp"e:
!har nameZ05[3
-. E%p"ain he Sring Sr$!$re
%hen compiler assigns string to character array then it automatically supplies n$""
!hara!er EFG7FH at the end of string. Thus, si(e of string E original length of string
L 1. An e$ample would be,
!har nameZH[3
name E ]T:&B"]
?. .o/ #o )o$ Rea# Srings?
To read a string, we can use scanf() function with format specifier Os.
!har nameZ05[3
scanf(]Os],name)3
The above format allows accepting only strings, which does not have any blank
space, tab, new line, forming feed, carriage return.
2. .o/ #o )o$ Wrie Srings?
To write a string, we can use printf() function with format specifier Os.
!har nameZ05[3
scanf(]Os],name)3
printf(]Os],name)3
67. De,ine a n$"" erminae# sring
#tring is terminated by a special character which is called as null terminator or null
parameter (N5). !n A#!! table, the null terminator has value 5.
66. Wha is a sorage !"ass?
#torage class defines the the scope and lifetime of a variable.
9$n!ions o, sorage !"ass:
To determine the location of a variable where it is stored C
#et initial value of a variable or if not specified then setting it to default value.
@efining scope of a variable.
To determine the life of a variable.
62. Wha are he )pes o, sorage !"ass?
A$omai! Sorage C"ass :
^eyword : auto
#torage Rocation : >ain memory
!nitial )alue : Tarbage )alue
Rife : ontrol remains in a block where it is defined.
#cope : Rocal to the block in which variable is declared.
S)na% :
a$o Zdata4type[ Zvariable4name[3
E%amp"e :
a$o in a3
Regiser Sorage C"ass :
^eyword : register
#torage Rocation : =< 8egister
!nitial )alue : Tarbage
Rife : Rocal to the block in which variable is declared.
#cope : Rocal to the block.
S)na% :
regiser Zdata4type[ Zvariable4name[3
E%amp"e :
regiser in a3
Sai! Sorage C"ass :
^eyword : static
#torage Rocation : >ain memory
!nitial )alue : _ero and can be initiali(e once only.
Rife : depends on function calls and the whole application or program.
#cope : Rocal to the block.
S)na%:
sai! Zdata4type[ Zvariable4name[3
E%amp"e:
sai! in a3
E%erna" Sorage C"ass:
^eyword : e$tern
#torage Rocation : >ain memory
!nitial )alue : _ero
Rife : <ntil the program ends.
#cope : Tlobal to the program.
S)na% :
e%ern Zdata4type[ Zvariable4name[3
E%amp"e:
e%ern in a3
6&. Wha is a sr$!$re?
Sr$!$re is user defined data type which is used to store heterogeneous data
under uni-ue name. ^eyword .struct. is used to declare structure.
The variables which are declared inside the structure are called as .members of
structure..
S)na%:
sr$! structure4nm
U
Idata2typeJ element 13
Idata2typeJ element *3
2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
Idata2typeJ element n3
Vstruct4var3
E%amp"e :
sr$! emp4info
U
!har emp4idZ15[3
!har nmZ155[3
,"oa sal3
Vemp3
6'..o/ #o )o$ a!!ess a sr$!$re member?
#tructure members can be accessed using member operator ... . !t is also called as
.#o operaor. or .perio# operaor..
S)na%:
structure4var.member3
6(. Wha is a Cnion?
<nion is user defined data type used to stored data under uni-ue variable name at
single memory location.
S)na%:
$nion union4name
U
Idata2typeJ element 13
Idata2typeJ element *3
Idata2typeJ element ,3
Vunion4variable3
E%amp"e:
$nion techno
U
int comp4id3
char nm3
float sal3
Vtch3
6*. Wha is he #i,,eren!e be/een a $nion an# a sr$!$re?
The ma+or #i,,eren!e be/een sr$!$re an# $nion is Fsorage.F !n structures,
each member has its own storage location, whereas all the members of union use
the same location. <nion contains many members of different types, it can handle
only one member at a time.
6-. Wha is #)nami! memor) a""o!aion in C?
provides y a powerful and fle$ible way to manage memory allocation at runtime.
!t is called dynamic memory allocation. @ynamic means you can specify the si(e
of data at runtime. %ith dynamic memory allocation, you can allocate and free
memory as needed.
6?. .o/ o ge o Bno/ he si1e o, he #aa?
can get the si(e of data by usingsizeof() function. The sizeof() function returns
a size_t an unsigned constant integer.
62. .o/ o a""o!ae memor) in C?
%e use malloc() function to allocate memory. &ere is the function interface:
void ! "allc#si$e%t si$e&;
27. Wha is he $se o, !a""o!E H an# rea""o!E H?
The calloc() function not only allocates memory like malloc() function but also
allocates memory for a group of ob+ects which are specified by the first argument
num.
The realloc() function takes in the pointer to the original area of memory to e$tend
and how much the total si(e of memory should be.
26. .o/ #o )o$ ,ree memor)?
%hen you use malloc() function to allocate memory, you implicitly get the
memory from a dynamic memory pool which is known as hea. The hea is
limited so you have to deallocate or free the memory, you re-uested when you
don.t use it in any more. provides function free() to allow you to free memory.
Delow is the function prototype:
void free(void Mptr)3

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