Documente Academic
Documente Profesional
Documente Cultură
com
C/C++
Tutorial
By alzanaty
Lesson 1
Program Structure
Program Structure
Consist of:
Preprocessor Commands
Functions
Variables
Comments
Lesson 2
Basic syntax
Basic syntax
Comments: $$ or $* any+text *$ )
C is case sensiti-e )
A variable:
is not3ing but a name gi-en to a storage area t3at our
programs can manipulate) Eac3 -ariable in C 3as a
specific type# 13ic3 determines t3e si0e and layout of t3e
-ariable<s memory' t3e range of -alues t3at can be stored
1it3in t3at memory' and t3e set of operations t3at can be
applied to t3e -ariable)
(aria)les
Declaration:
Type variable_name = value;
(aria)les
Floating-point literals:
. floating6point literal 3as an integer part# a decimal point# a
fractional part# and an exponent part)
23ile representing using decimal form# you must include t3e
decimal point# t3e exponent# or bot3 and 13ile representing
using exponential form# you must include t3e integer part# t3e
fractional part# or bot3) T3e signed exponent is introduced by e
or E)
Constant
@ou can use const prefix to declare constants 1it3 a specific type as
follo1s:
const type -ariable G -alue'
e+ining Constants ,xample
e+ining Constants ,xample
Lesson -
Storage Classes
Storage Classes
int mount'
auto int mont3'
(
T3e example abo-e defines t1o -ariables 1it3 t3e same storage
class# auto can only be used 1it3in functions# i)e)# local -ariables)
T$e register Storage Class
23en you 3a-e multiple files and you define a global -ariable or function#
13ic3 1ill be used in ot3er files also# t3en extern 1ill be used in anot3er
file to gi-e reference of defined -ariable or function) Kust for
understanding# extern is used to declare a global -ariable or function in
anot3er file)
T3e extern modifier is most commonly used 13en t3ere are t1o or more
files s3aring t3e same global -ariables or functions as explained belo1)
T$e extern Storage Class
Lesson .
Lperators
/perators
types of operators:
56 .rit3metic Lperators)
76 Belational Lperators)
86 >ogical Lperators)
96 Bit1ise Lperators)
M6 .ssignment Lperators)
N6 Iisc Lperators)
0rit$metic /perators
1elational /perators
Logical /perators
Bitwise /perators
0ssignment /perators
2isc /perators sizeo+ 3 ternary
2isc /perators sizeo+ 3 ternary
/perators Prece&ence in C
Lesson .
4ecision Iaking
ecision 2a4ing
4efinition:
Exp5 O Exp7 : Exp8'
Syntax:
T3e syntax of a 13ile loop in C programming language is:
13ile%condition&
statement%s&'
(
statement%s&'
(
&o...w$ile loop
:nlike for and 13ile loops# 13ic3 test t3e loop condition at
t3e top of t3e loop# t3e do)))13ile loop in C programming
language c3ecks its condition at t3e bottom of t3e loop)
Syntax:
do
statement%s&'
(13ile% condition &'
neste& +or loops
Syntax:
13ile%condition&
13ile%condition&
statement%s&'
(
statement%s&'
(
neste& &o...w$ile loop
do
statement%s&'
do
statement%s&'
(13ile% condition &'
(13ile% condition &'
Loop Control Statements6
Syntax:
T3e syntax for a break statement in C is as follo1s:
break'
continue statement
Syntax:
continue'
goto statement
Syntax:
goto label'
))
)
label: statement'
,xample go to
T$e !n+inite Loop
Syntax:
for% ' ' &
,f you omit t3e si0e of t3e array# an array ;ust big enoug3 to
3old t3e initiali0ation is created) T3erefore# if you 1rite:
double balanceQR G 5???)?# 7)?# 8)9# S)?# M?)?('
2ay65
Formal parameters as a pointer as follo1s)
-oid myFunction%int *param&
)
)
)
(
Passing 0rrays as "unction 0rguments
2ay67
Formal parameters as a si0ed array as follo1s:
-oid myFunction%int paramQ5?R&
)
)
)
(
Passing 0rrays as "unction 0rguments
2ay68
Formal parameters as an unsi0ed array as follo1s:
-oid myFunction%int paramQR&
)
)
)
(
,xample
,xample Con....
1eturn array +rom +unction
(
1eturn array +rom +unction
T3e actual data type of t3e -alue of all pointers# 13et3er integer# float#
c3aracter# or ot3er1ise# is t3e same# a long 3exadecimal number t3at
represents a memory address) T3e only difference bet1een pointers of
different data types is t3e data type of t3e -ariable or constant t3at t3e
pointer points to)
<ow to use Pointers5
=o1# after t3e abo-e operation# t3e ptr 1ill point to t3e
location 5??9 because eac3 time ptr is incremented# it 1ill
point to t3e next integer location 13ic3 is 9 bytes next to
t3e current location) T3is operation 1ill mo-e t3e pointer to
next memory location 1it3out impacting actual -alue at t3e
memory location) ,f ptr points to a c3aracter 13ose
address is 5???# t3en abo-e operation 1ill point to t3e
location 5??5 because next c3aracter 1ill be a-ailable at
5??5)
!ncrementing a Pointer
C arrays allo1 you to define type of -ariables t3at can 3old se-eral
data items of t3e same kind but structure is anot3er user defined
data type a-ailable in C programming# 13ic3 allo1s you to combine
data items of different kinds)
Title
.ut3or
Sub;ect
Book ,4
e+ining a Structure
member definition'
member definition'
)))
member definition'
struct Books
c3ar titleQM?R'
c3ar aut3orQM?R'
c3ar sub;ectQ5??R'
int book+id'
( book'
0ccessing Structure 2em)ers
@ou can define a union 1it3 many members# but only one
member can contain a -alue at any gi-en time)
To define a union# you must use t3e union statement in -ery similar
1as as you did 13ile defining structure)
T3e union statement defines a ne1 data type# 1it3 more t3an one
member for your program)
union Qunion tagR
member definition'
member definition'
))
member definition'
( Qone or more union -ariablesR'
e+ining a =nion
.t t3e end of t3e union<s definition# before t3e final semicolon# you can
specify one or more union -ariables but it is optional)
union 4ata
int i'
float f'
c3ar strQ7?R'
( data'
e+ining a =nion
T3is means t3at a single -ariable ie) same memory location can
be used to store multiple types of data)
@ou can use any built6in or user defined data types inside a
union based on your reDuirement)
,f you are using suc3 -ariables inside a structure t3en you can
define t3e 1idt3 of a -ariable 13ic3 tells t3e C compiler t3at you
are going to use only t3ose number of bytes)
struct
T3e -ariables defined 1it3 a predefined 1idt3 are called bit fields) .
bit field can 3old more t3an a single bit for example if you need a
-ariable to store a -alue from ? to S only t3en you can define a bit
field 1it3 a 1idt3 of 8 bits as follo1s:
struct
23en 1e are saying ,nput t3at means to feed some data into
program) T3is can be gi-en in t3e form of file or from
command line)
T3e file points are t3e means to access t3e file for reading and 1riting
purpose) T3is section 1ill explain you 3o1 to read -alues from t3e screen
and 3o1 to print t3e result on t3e screen)
T$e getc$ar?@ 3 putc$ar?@ +unctions
T3e int puts%const c3ar *s& function 1rites t3e string s and
a trailing ne1line to stdout)
,xample
T$e scan+?@ an& print+?@ +unctions
( * , ...) The int scanf const char format function reads input from
the standard input stream stdin and scans that input according
. to format provided
( * , ...) The int printf const char format function writes output to
the standard output stream stdout and produces output
. according to a format provided
, The format can be a simple constant string but you can specify
% , % , % , % , ., , , s d c f etc to print or read strings integer character or
. foat respectively There are many other formatting options
. available which can be used based on requirements For a
. complete detail you can refer to a man page for these function
, This function actually fushes any data still pending in the bu)er
, , to the le closes the le and releases any memory used for the
. le
, There are following two functions which can be used for binary
: input and output
_ ( * , _ _ _ , si$e t fread void ptr si$e t si$e of elements
_ _ _ , * _ ); si$e t number of elements F&'( a le
_ ( * , _ _ _ , si$e t fwrite const void ptr si$e t si$e of elements
_ _ _ , * _ ); si$e t number of elements F&'( a le
Finclude Ustandard+library+file+nameV
.ny standard library 3eader file name can appear bet1een
t3e angled brackets)
Finclude Amyfile)3A
13ereas t3e double Duotes form 1ill searc3 t3e current
source directory first# and t3en searc3 t3e default 3eader
file directory if t3e file 1as not in t3e current directory)
Static "unctions
T3is tells t3e CPP to define IESS.JE only if IESS.JE isn<t already
defined)
Fifdef 4EB:J
H* @our debugging statements 3ere *H
Fendif
T3is tells t3e CPP to do t3e process t3e statements enclosed if 4EB:J is
defined) T3is is useful if you pass t3e 644EB:J flag to gcc compiler at t3e
time of compilation) T3is 1ill define 4EB:J# so you can turn debugging on
and off on t3e fly during compilation)
Pre&e+ine& 2acros
Stringi0e %F&
T3e stringi0e or number6sign operator %<F<&# 13en used 1it3in a
macro definition# con-erts a macro parameter into a string constant)
T3is operator may be used only in a macro t3at 3as a specified
argument or parameter list)
Finclude Ustdio)3V
Fdefine message+for%a# b& $
printf%Fa A and A Fb A: 2e lo-e youZ$nA&
int main%-oid&
message+for%Carole# 4ebra&'
return ?'
(
Preprocessor /perators
T3ere are t1o types of 3eader files: t3e files t3at t3e
programmer 1rites and t3e files t3at come 1it3 your
compiler)
Bot3 user and system 3eader files are included using t3e
preprocessing directi-e Finclude) ,t 3as follo1ing t1o
forms:
Finclude UfileV
Finclude AfileA
T3is form is used for 3eader files of your o1n program) ,t
searc3es for a file named file in t3e directory containing
t3e current file) @ou can prepend directories to t3is list 1it3
t3e 6, option 13ile compiling your source code)
/nce#/nly <ea&ers
exit%65&'
exit%?&'
Lesson 22
Becursion
Concept
recursion%&'
(
Concept
func%5# 7# 8&'
func%5# 7# 8# 9&'
(
Concept
calloc%7??# si0eof%c3ar&&'
So you 3a-e complete control and you can pass any si0e
-alue 13ile allocating memory unlike arrays 13ere once you
defined t3e si0e can not be c3anged)
1esizing an& 1eleasing 2emory