Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
24Activity
0 of .
Results for:
No results containing your search query
P. 1
C & C++ 4 Quick Reference

C & C++ 4 Quick Reference

Ratings: (0)|Views: 290|Likes:
Published by saagarchary

More info:

Published by: saagarchary on Sep 19, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/09/2010

pdf

text

original

 
Lecture NotesProgramming Concepts in C & C++
Kamlesh Tiwari
(ktiwari@iitk.ac.in)
1 History
C was originally designed for and implemented on theUNIX operating system on the DEC PDP-11, by DennisRitchie. The C language was based on two languages:BCPL, written by Martin Richards, and B, written byKen Thompson. American National Standards Institute(ANSI) adopted C in 1988.
2 Features
C is a general-purpose high-level programming languagewith features of economy of expression, modern flow con-trol & data structures, and a rich set of operators. it isnot specialized to any particular area of application, theabsence of restrictions and its generality make it moreconvenient and effective for many tasks than any otherpowerful languages. It is not tied to any particular hard-ware or system, and therefore it is easy to write programsthat will run without change on any machine that sup-ports C. It is often called a middle-level computer lan-guage because it combines the best elements of high-levellanguages with the control and flexibility of assembly lan-guage.
3 First Program
Let’s start learning by writing the typical
Hello
program.
3.1 Basics
Every C program starts execution form a user de-fined function called
main()
. this function must bepresent in every C program. Program can have onlyone
main 
function.
void main(){}
A function named
printf()
is used to send outputat VDU screen. what we want to print should beplaced in between the brackets of 
printf()
.
If we want to use any function we must
include
theappropriate header file that containes the definitionsof that function.
The definitions of function
printf()
are found inheader file named
stdio.h 
, which is an abbriviationfor
STanDard Input Output Header 
file.
To include
stdio.h 
in our program we have to writestatement as
#include
<
stdio.h 
>
include
statements are written in the begining of thesource code, generally before
main()
function.
#include<stdio.h>void main(){}
What is this
void 
written before the
main()
? wellit signifies the type of return value from the
main()
function, for now take it as it is, we will come to itlater.
Executable statements are plaved in between thebody of 
main()
function, and every executable state-ment terminates with a semicolon (;) at the end.
3.2 Do it
Come let’s write a program that prints
Hello
. First of all to define precisely output, write
Hello
as
”Hello”
,then use the function
printf()
to print the output, bypassing
”Hello”
to is, as
printf(”Hello”)
. Our executablestatement is ready
printf(”Hello”);
Place this statement in the body of 
main()
#include<stdio.h>void main(){printf("Hello");}
Save this program. what name you want to give thisprogram ?
hello.c
ok no problem do it.1
 
3.3 Compile and Run
Next step is to check the output of the program.
3.3.1 Compile
How ? well, press F10, use arrow keys to move to compiletab, press
ENTER
, choose the option
COMPILE 
againpress
ENTER
.During the compilation the compiler will generate fileswith name
hello.bak, hello.map, hello.obj, hello.exe
. Weare inerested in
hello.exe
, this is the executable file forDOS environment that will give us output.
3.3.2 Run
How ? well, press F10, use arrow keys to move to runtab, press
ENTER
, choose the option
RUN 
again press
ENTER
.The program will get exectued. It prints the desiredoutput to output screen and then, Integrated Devel-opment Environment (IDE) of Turbo C start once again.To see the output screen press
ALT + F5 
Congratulation, U done it.
4 Definitions
4.1 Identifier
the names of variables, functions, labels, and variousother user-defined objects are called identifiers. Theseidentifiers can vary from one to several characters. Thefirst character must be a letter or an underscore, andsubsequent characters must be either letters, digits, orunderscores.In C, identifiers may be of any length. However, not allcharacters will necessarily be significant. If the identifieris not used in an external link process, then at least thefirst 31 characters will be significant. In C++, there isno limit to the length of an identifier, and at least thefirst 1,024 characters are significant. In an identifier, up-per and lowercase are treated as distinct. Hence, count,Count, and COUNT are three separate identifiers. Anidentifier cannot be the same as a keyword, and shouldnot have the same name as functions that are in the li-brary.
Correct : abcd, Var_2, _env_, envVarIncorrect : 1abc, var-2, ab c, this, var...2
4.2 Keyword
C has 32 keywords that, combined with the formal Csyntax, form the C programming language. Of these, 27were defined by the original version of C, and five morewere added by the ANSI C committee: enum, const,signed, void, and volatile.
auto double int structbreak else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed voiddefault goto sizeof volatiledo if static while
4.3 Basic Data Type
There are five atomic data types in C, listed below withtheir size in byte.
Type Keyword Size Rangevalueless void 0 nilcharacter char 1 -128 to 127integer int 2 -32768 to 32767floating-point float 4 6 digit precisiondouble floating-point double 8 10 digit precision
4.4 Modifier of Basic Data Type
Except for type void, the basic data types may have var-ious modifiers preceding them. You use a modifier toalter the meaning of the base type to fit various situa-tions more precisely. The list of modifiers is as follows :signed, unsigned, long, short
4.5 Variables
A variable is a named location in memory that is usedto hold a value that may be modified by the program.All variables must be declared before they can be used.The general form of a declaration is.type variable list;Here, type must be a valid data type plus anymodifiers, and variable list may consist of one or moreidentifier names separated by commas. Here are somedeclarations:
int i,j,l;short int si;unsigned int ui;double balance, profit, loss;
Variables will be declared in three basic places: insidefunctions, in the definition of function parameters, andoutside of all functions. These are local variables, formalparameters, and global variables.2
 
4.6 Access Modifiers
There are two modifiers that control how variables maybe accessed or modified. These qualifiers are const andvolatile. They must precede the type modifiers and thetype names that they qualify. These modifiers are alsoreferred to as cv-qualifiers.
4.6.1 const
Variables of type const may not be changed by yourprogram. (A const variable can be given an initialvalue, however.) The compiler is free to place variablesof this type into read-only memory (ROM). For example,const int a=10;creates an integer variable called a, with an initialvalue of 10 that your program may not modify. Notethat a constant must be initialised with value, otherwisethe compiler will generate an error.const int a; // is an error
4.6.2 volatile
The modifier volatile tells the compiler that a variable’svalue may be changed in ways not explicitly specified bythe program. For example, a global variable’s addressmay be passed to the operating system’s clock routineand used to hold the real time of the system.
4.7 Storage Class Specifier
There are four storage class specifiers supported by C:extern, static, register, auto
4.7.1 extern
Note that distinction between the declaration and thedefinition of a variable is that the declaration declaresthe name and type of a variable, where as a definitioncauses storage to be allocated for the variable.Because C/C++ allows separate modules of a large pro-gram to be separately compiled and linked together,there must be some way of telling all the files about theglobal variables required by the program.In most cases, variable declarations are also definitions.However, by preceding a variable name with the externspecifier, you can declare a variable without defining it.
4.7.2 static
Static variables are permanent variables within their ownfunction or file. Unlike global variables, they are notknown outside their function or file, but they maintaintheir values between calls.When you apply the staticmodifier to a local variable, the compiler creates perma-nent storage for it, much as it creates storage for a globalvariable. The key difference between a static local vari-able and a global variable is that the static local variableremains known only to the block in which it is declared.In simple terms, a static local variable is a local variablethat retains its value between function calls.Applying the specifier static to a global variable instructsthe compiler to create a global variable that is knownonly to the file in which you declared it.
4.7.3 register
The register specifier requested that the compiler keepthe value of a variable in a register of the CPU ratherthan in memory, where normal variables are stored. Thismeant that operations on a register variable could occurmuch faster than on a normal variable because the reg-ister variable was actually held in the CPU and did notrequire a memory access to determine or modify its value.
4.7.4 auto
By default all variables are auto ( if are not static orregister ), they are created on function call and destroyedon termination of function. Normally we omit the worldauto to declare automatic variable.
5 Escape Sequences
Enclosing character constants in single quotes works formost printing characters. A few, however, such as thecarriage return, are impossible to enter into a string fromthe keyboard. For this reason, C/C++ include the spe-cial backslash character constants shown so that you mayeasily enter these special characters. These are also re-ferred to as Backslash Characters.
Code Meaning\b Backspace\f Form feed\n New line\r Carriage return\t Horizontal tab\" Double quote\Single quote\0 Null\\ Backslash\v Vertical tab\a Alert (Bell)\? Question mark\N Octal constant (N is octal)\xN Hexadecimal constant (N is hexadecimal)
3

Activity (24)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Anand Vadhadia liked this
akbisoi1 liked this
cpatil319311 liked this
srinathottur liked this
Gopualakrishnan liked this
ezhaivendan liked this
ssonby liked this
Pavani Achyutha liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->