Upload
sukanta-pal
View
215
Download
0
Embed Size (px)
Citation preview
7/31/2019 C_Programming1
1/27
C Programming
Design By:Solutioncab.com
7/31/2019 C_Programming1
2/27
What is a language?
A Language is the Spoken and signedforms of communication among
Humankind. A formal language is a set of words, i.e.
finite strings of letters, symbols, or tokens.
The set from which these letters are takenis called the alphabet.
Eg. English, Hindi etc.
7/31/2019 C_Programming1
3/27
What is a language?
Communicating with a Human Being involvesspeaking the language that human being
understands.
Communicating with a computer involvesspeaking the language the computer andhuman being both understands,
7/31/2019 C_Programming1
4/27
What is C ?
C is a programming language developed atAT & Ts Bell Laboratories of USA in 1972. Itwas designed and written by a man namedDennis Ritchie. In the late seventies C beganto replace the more familiar languages of thattime like PL/I, ALGOL, etc.
C is often called a middle-level computerlanguage, because it has the features of bothhigh-level languages, such as BASIC,PASCAL etc., and low-level languages suchas assembly language.
7/31/2019 C_Programming1
5/27
How to learn Language? Steps Of Learning English Languages.
Alphabets Words Sentences Paragraphs
Steps Of Learning C.
AlphabetsDigits
Special Symbols
ConstantsVariablesKey words
Instructions Programs
7/31/2019 C_Programming1
6/27
The C Character Set
A character denotes any alphabet, digit orspecial symbol used to represent information.
Like:Alphabets: A,B,C.X,Y,Z
a,b,c,.....x, y, z
Digits: 0,1,2,3,4,5,6,7,8,9.
Special Symbols: ~!@#$%^&*()_+|-=\[{}];:?/
7/31/2019 C_Programming1
7/27
Constants, Variables & Keywords
Constants: A constant is an entity that does notchange. eg. 2, 3, 4, 900,8.675.
Variables: A variable is an entity that may
change. eg. X, f, Ram, Var_iable,u z
x = 40
x u zx = 20 20 40
Variable Constant
x
40 20 40
4020
Whose value is changing ?
7/31/2019 C_Programming1
8/27
Types of Constants
C constants can be divided into two majorcategories:
Primary Constants Integer Constant.
Real or Float Constant. Character Constant.
Secondary Constants Array.
Pointers
Structure.
Union
Enum etc.
(we will study later.)
7/31/2019 C_Programming1
9/27
Integer Constant.
Rules of Integer Constants An integer constant must have at least one digit.
It must not have a decimal point.
It can be either positive or negative. If no sign precedes an integer constant it is
assumed to be positive.
No commas or blanks are allowed within aninteger constant.
The allowable range for integer constants is -32768 to 32767.
7/31/2019 C_Programming1
10/27
Integer Constant.
Examples of Valid Integer Constant.
Example of Invalid Integer Constants.
Valid : 0, 1, 743, -67, 5478, 9999, etc
12,245
36.4
12 34
0900
illegal Character (,)
illegal Character(.)
illegal Character (Blank Space)
The first digit cannot be zero
7/31/2019 C_Programming1
11/27
Real or Float Constant.
Rules for Constructing Real Constants
A real constant must have at least onedigit.
It must have a decimal point.
It could be either positive or negative.
Default sign is positive. No commas or blanks are allowed within a
real constant.
7/31/2019 C_Programming1
12/27
Real or Float Constant
Example of valid Real or Float Constant+325.34 , 426.0 , -32.76, -48.5792
Example of in valid Real or Float Constant1
1,000.0
2E+10.2
3 .20
Must Have (.)
illegal Character (,)
Power cannot be in decimal (10.2)
Blank space Not allow
7/31/2019 C_Programming1
13/27
Real constants expressed in
exponential form: The mantissa part and the exponential part should be
separated by a letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive. The exponent must have at least one digit, which must
be a positive or negative integer. Default sign is positive.
Range of real constants expressed in exponential form is
-3.4e38 to 3.4e38. Ex.: +3.2e-5
4.1e8
-0.2e+3
-3.2e-5
-53.2 e
Mantissa SeparatorExponent
7/31/2019 C_Programming1
14/27
Character Constants
Rules for Constructing Character Constants
A character constant is a single alphabet, asingle digit or a single special symbol enclosed
within single inverted commas. Both the inverted commas should point to the
left. For example, A is a valid characterconstant whereas A is not.
The maximum length of a character constantcan be 1 character.
7/31/2019 C_Programming1
15/27
7/31/2019 C_Programming1
16/27
Variable Constant Variable names are names given to locations inmemory. These locations can contain integer, real or
character constants.
The types of variables that it can support depend on
the types of constants that it can handle. This isbecause a particular type of variable can hold only thesame type of constant.
For example, An integer variable can hold only an integer constant.
A real variable can hold only a real constant.
A character variable can hold only a character constant.
7/31/2019 C_Programming1
17/27
Variable Constant
Rules of Variable declaration. Variables must be declared before use
immediately after {.
Valid characters are letters, digits and _ . First character cannot be a digit.
31 characters recognized for local variables.(more can be used, but are ignored).
Upper and lowercase letters are distinct.
7/31/2019 C_Programming1
18/27
Variable Declaration
int b;
double x;
unsigned int a;
char c; C is a typed language that means a variable has a
Type
Name
C standard types int, double, char, float, short, long, long double
unsigned char, unsigned short, unsigned int, unsigned long
int b;
Variable type Variable Name
7/31/2019 C_Programming1
19/27
Variable Constant
Example of Variable constants.
int a; /* General declaration */
int a = 10; /* Valued Declaration */
float b = 12.6; /* Valued Declaration */
float b; /* General declaration */
char c; /* General declaration */
char c = y; /* Valued Declaration */
7/31/2019 C_Programming1
20/27
Keywords.
In Standard C, the keywords have special significance to thecompiler and therefore can not be used as identifiers.
auto double int struct
break else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed void
default goto sizeof volatiledo if static while
7/31/2019 C_Programming1
21/27
The Format of C
Statements are terminated with semicolons(;) Indentation is ignored by the compilerC is case sensitive - all keywords and
Standard Library functions are lowercaseStrings are placed in double quotes ()New lines are handled via\nPrograms are capable of flagging success or
error, those forgetting to do so have one orother chosen randomly!
7/31/2019 C_Programming1
22/27
printf and scanf
printfwrites
integer values to screen when %d is used float values to screen when %f is used
character values to screen when %c is used
scanf reads integer values from the keyboard when %d is used. float values from the keyboard when %f is used.
character values from the keyboard when %c is used.
& VERY important with scanf & not necessary with printf because current
value of parameter is used
7/31/2019 C_Programming1
23/27
The Basic Structure of a C Program
{
}
Global variables Declaration
/* Program documentation */
#Preprocessor directives
Global constants Declaration
main ()
Instructions & Statements
7/31/2019 C_Programming1
24/27
First C program.
#include // input-output libraryint main() // function main
{
printf(Hello World\n); // send string to standard output
}
Output: Hello world
7/31/2019 C_Programming1
25/27
First C program.
#include includes the standard I/O library which allows you to read from the
keyboard (standard in) and write to the screen (standard out)
int main()
declares the main function. Every C-program must have a functionnamed main somewhere in the code{ ... }
The symbols { and } mark the beginning and end of a block of code.
printf(Hello World\n)
Sends output to the screen. The portion in quotes ... is the formatstrings and describes how the data is formatted.
7/31/2019 C_Programming1
26/27
Second program.#include
void main(){
int a;float x;char c;
printf(Enter integer:);scanf(%d,&a);
printf(\nEnter float:);scanf(%f,&x);
printf(\nEnter character:);
scanf(%c,&c);printf(\nValue of a is %d, of x is %f, of c is %c\n,a,x,c);
}
standard I/O filemain function
declare a variable of type integer.declare a variable of type float.declare a variable of type character.
display on screenEnter integer.store the integer value in a.display on screenEnter Float.store the Floatvalue in x.
display on screenEnter character.
store the charactervalue in c.gives output
Code block starts/ Main start
Code block end or Main end
7/31/2019 C_Programming1
27/27
End of lecture 1