Programming With C
Fundamentals of ‘C’ including
data types, operators and I/O
statements
Gagan Deep
Founder & Director
Rozy Computech Services
Kurukshetra-136119
M- 9416011599
Email – rozygag@yahoo.com
www.rozyph.com
Contents of Discussion
• Language & Programming Language
• About C
• Character Set, Identifiers & Statements
• Data Types
• Operators
• Input Output Functions
• Programming Examples
Language
• Language is a way of Communication Between
Two like Hindi, English, Punjabi, Marathi,
Tamil etc.
• If we want to communicate other then our own
languages then we can communicate in either of
two ways
- Either Learn
- Use Translator
Programming Language
• Way of Communication between Human and
Computer like Machine language, Assembly
Language, High Level Language.
• Machine Language and Assembly Languages are
low level languages because these don’t work like
our own languages.
• High level languages(HLL) works like our own
languages like Hindi, English and others that’s
why these are known as HLL.
About C
• C is a Programming Language
• C Language is a High Level Language (because it
works like natural language). Here we uses
English like statements.
• C Language also have a properties of low level
languages
• That’s why some of us says – This is Middle
Level Language.
About C
• C is a general purpose programming language.
• C was originally designed for and implemented
on the UNIX operating system on the DEC PDP-
11, by Dennis Ritchie.
• C is a case sensitive language A≠ a
Character Set / Alphabets
• First Thing we learned in any language is
Alphabets – Similarly in Programming Languages
is Character Set.
• C character Set includes alphabets like a-z, A-Z,
Digits like 0-9, special symbols like !, @, #, $, %,
…….-, + etc., and some other characters which
are not available on keyboard <=, >=, !=, == etc.
• Print Characters- which are known as Escape
sequences like ‘n’, ‘t’, ‘b’ etc…
Words
• Second thing we always learn in any language are
words. Similarly we learn here in C. Here we say
words as Identifiers and Reserve words.
• Identifier Gagan, Rajesh, Saminder, Meenu, etc.
are our names which identifies us, and some other
names are like Table, chair, Fan, Tube which we
always we are using for some objects.
• First Type of Identifiers are known as Variable in
any Language or we can say in C and second type of
names are known as Constants.
• Some other words are known as Standard words
like min, max etc.
Rules for Naming Identifiers
• First Character is Letter not digit
• Second character it may be letter or digit
• Special characters (other then letters or digits)
are not allowed Except underscore(_)
• Length of Identifiers 8, 31 etc.
Type of Identifiers – Data Type
• Like we have our Gender Males / Females. Similarly
in each language we have some types of identifier
known as Data types.
• What data type does is categorize data.
• These basic categorizations or data types are
integer(int),
• Real(float),
• Character (char)
• char – 1 byte - -128 to 127
• Character can declare as
char c;
• int – 2 bytes - Range (-32768 to 32767)
• Integer can declare as
int a;
• float – 4 bytes - 3.4X10-38 to 3.4X1038
• Real can declare as
float f;
Why int’s range is -32768 to 32767 ?
Integer Representation
• Most of the computers use 2 bytes to store an
integer.
• The left most bit, out of sixteen bits, is used to
indicate the sign of the integer and is called Sign
bit.
• The other 15 bits are used to store the given
integer, a 0 in the sign bit position indicates a
positive integer and 1 in this position means the
integer stored is negative.
Integer Representation
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0/1
Sign
Bit
15 bits for Number representation
Maximum Number
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
+215-1 = 32767
Minimum Number
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-215 = -32768
Unsigned Numbers are numbers without sign i.e. First bit is used for number itself.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Range is 0 to 216 -1 i.e. 0 to 65535
If we want bigger number then the
ranges defined.
• char – strings
String is collection of character
• int – short, long, unsigned, unsigned long
Short is of 1 byte.
Long is of 4 bytes(double precession(size))
• float - double
Double is of 8 Bytes
Constants
• int – Decimal, Octal, Hexadecimal
• Decimal int – 77 equals 77
• Octal int – 077 equals 63
• Hexadecimal int – 0x77 equals 119
• Unsigned int – 45678U
• Unsigned long – 243567849UL
• Float –
• Char - ‘a’
• String – “Rozy” ,
Sentences / Statements
• Third thing we learn in any language are sentences,
here in programming these are known as statements.
• Statements are the instructions given to the
computer to perform any kind of action.
• Statements form the smallest executable unit within
a C++ program.
• As sentences terminated by some full stop(., I),
similarly, Statements are terminated with a
semicolon (;).
Types of Statements
• Simple Statements
• Compound Statements
• Control Statements
Simple Statement – Single statement. The
simplest statement is the empty, or null statement.
e.g.
; // it is a null statement.
Examples of simple statement are as
x=y; x=x+y; scanf(“%d”, &ajay);
printf(“%d”, ajay);
• Compound Statement – paragraph (Block) A
compound statement in C++ is a sequence of
statements enclosed by a pair of braces ({}). e.g.,
{ statement 1;
statement 2; },
A compound statement is treated as a single unit or
statement.
• Control Statements – which controls flow of
statements like decision/ branching and loops
Operators
• Operators are tokens that trigger some computation
when applied to variable and other objects in an
expression. Classifications of Operators are
Depending upon the number of operands we have :
• Unary operators : These operators require just 1
operand. e.g, - (minus sign), ++, --, sizeof etc.
• Binary operators: These operators take 2
operands. e.g. +, -, *, / , <, >, !=, <=
• Ternary operators: These operators take 3 operands,
e.g. Conditional operator (? : ) e.g. (A >B?5:6)
Operators by Operations
• Arithmetic Operators : Integer and Real
Integer : +,-, *, /, %
Real : +, -, *, /
• Relational Operators : <, >, <=, >=,
• Equality Operators : !=, ==
• Logical Operators : && (AND), ||(OR), !(NOT)
• Assignment Arithmetic Operators : +=, *=, /= etc.
e.g. a+=5 is equivalent to a=a+5. These are also
known as Compound Assignment or
Shorthand's
Operators by Operations
• Increment Operator
• Decrement Operators : Similarly pre and post
decrement operators e.g. - -a; and a- -;
Pre increment : ++a
++a means a = a +1
Pre means before execution of
statement e.g. If a=5;
Post increment : a++
a++ means a = a +1
Post means after execution of
statement e.g. If a=5;
printf(“%d”, a); //returns 5
printf(“%d”, ++a); //returns 6
printf(“%d”, a); //returns 6
printf(“%d”, a); //returns 5
printf(“%d”, a++); //returns 5
printf(“%d”, a); //returns 6
Hierarchy / Precedence of
Operators with their Associativity
Operators Category Operators Associativity
Unary Operators -, ++, - - , !, sizeof , (type) Right → Left (R→L)
Arithmetic * , / , %, Left → Right (L → R)
+ , -
Relational < , <=, >, >= L → R
Equality == , != L → R
Logical && L → R
|| L → R
Assignment =, +=, -=, *=, /=, %= R → L
Data Input and Output
• In C you can input/output using input and output
library function.
• Functions – User Defined and Library Function
• These I/O Library functions are getchar(), putchar(),
scanf(), printf(), gets() and puts().
• These six functions permits the transfer of information
between the computer and the standard I/O
devices(e.g. Keyboard, VDU etc.)
• An I/O fxs. Can be accessed from anywhere within the
program simply by writing the function name.
I/O Statements
• First we discuss about Single Character and
Strings I/O Functions
Single Character I/O
Functions are getchar()
and putchar ()
String I/O Functions are
gets() and puts()
char c;
Input statement like this
c = getchar();
Output statement like this
putchar( c );
char name[20];
Input statement like this
gets(name);
Output statement like this
puts(name);
scanf()
• With the help of scanf() we can enter any type of
data and mixed data. In general terms scanf()
function is written as
scanf( control string, arg1, arg2, arg3…, argn);
• Control String consists of individual groups of
characters, with one character group for each
input data item.
• Each character group must begin with a
percent(%) sign and followed by conversion
character which indicates the type of
corresponding data item.
Commonly used conversion characters for data
input are
• c for single character,
• d is for decimal integer,
• e for floating point value,
• f for floating point value,
• l is for long etc….
• The arguments are written as variables, arrays,
whose types match the corresponding character
group in the control string.
• Each variable must be preceded by an ampersand
(&).
• Array name should not begin with &.
Examples
char name[20];
int roll; float marks;
scanf(“ %s %d %f”, name, &roll, &marks);
• Formatted scanf() function
int a,b,c;
scanf(“%3d, %3d %3d”, &a, &b, &c);
In the above statement all a,b and c can take
maximum of 3 digits.
printf()
• It is similar to input function scanf(), except that
its purpose is to display data rather than to enter
it into the computer.
• Also there is no ampersand (&) symbol before
args.
printf(“ %s n %d n %f”, name, roll, marks);
Formatted printf() function
• example
int a,b,c;
printf(“%3d, %3d %3d”, a, b, c);
• In the above statement all a,b and c can display
minimum of 3 digits or spaces instead of digits.
Structure of C Program
# include < header file> // # is pre-processor
directive
#define x 5; //symbolic constant
int a, b; //global variable declaration
int fxn(); // function declaration
main()
{ int i,j,k; // local variable declaration
Input statements;
Process;
Output Statements; }
Program 1
# include < stdio.h>
main( ) // by default main function is int type
{ int a=5,b=6,c;
c=a+b;
printf(“The sum is = %d”, c);
}
Program 2
# include < stdio.h>
main( )
{
int a,b,c;
scanf(“%d, %d”, &a, &b);
c=a+b;
printf(“The sum is = %d”, c);
}
Program 3
# include < stdio.h>
void main()
/* void is data type which says function should not
return any value*/
{ int a,b;
scanf(“%d, %d”, &a, &b);
printf(“The sum is = %d”, a+b);
}
Program 4
# include < stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf(“Please enter the value of a & b”);
scanf(“%3d, %3d”, &a, &b);
printf(“The sum is = %4d”, a+b); }
C – A Programming Language- I

C – A Programming Language- I

  • 1.
    Programming With C Fundamentalsof ‘C’ including data types, operators and I/O statements Gagan Deep Founder & Director Rozy Computech Services Kurukshetra-136119 M- 9416011599 Email – [email protected] www.rozyph.com
  • 2.
    Contents of Discussion •Language & Programming Language • About C • Character Set, Identifiers & Statements • Data Types • Operators • Input Output Functions • Programming Examples
  • 3.
    Language • Language isa way of Communication Between Two like Hindi, English, Punjabi, Marathi, Tamil etc. • If we want to communicate other then our own languages then we can communicate in either of two ways - Either Learn - Use Translator
  • 4.
    Programming Language • Wayof Communication between Human and Computer like Machine language, Assembly Language, High Level Language. • Machine Language and Assembly Languages are low level languages because these don’t work like our own languages. • High level languages(HLL) works like our own languages like Hindi, English and others that’s why these are known as HLL.
  • 5.
    About C • Cis a Programming Language • C Language is a High Level Language (because it works like natural language). Here we uses English like statements. • C Language also have a properties of low level languages • That’s why some of us says – This is Middle Level Language.
  • 6.
    About C • Cis a general purpose programming language. • C was originally designed for and implemented on the UNIX operating system on the DEC PDP- 11, by Dennis Ritchie. • C is a case sensitive language A≠ a
  • 7.
    Character Set /Alphabets • First Thing we learned in any language is Alphabets – Similarly in Programming Languages is Character Set. • C character Set includes alphabets like a-z, A-Z, Digits like 0-9, special symbols like !, @, #, $, %, …….-, + etc., and some other characters which are not available on keyboard <=, >=, !=, == etc. • Print Characters- which are known as Escape sequences like ‘n’, ‘t’, ‘b’ etc…
  • 8.
    Words • Second thingwe always learn in any language are words. Similarly we learn here in C. Here we say words as Identifiers and Reserve words. • Identifier Gagan, Rajesh, Saminder, Meenu, etc. are our names which identifies us, and some other names are like Table, chair, Fan, Tube which we always we are using for some objects. • First Type of Identifiers are known as Variable in any Language or we can say in C and second type of names are known as Constants. • Some other words are known as Standard words like min, max etc.
  • 9.
    Rules for NamingIdentifiers • First Character is Letter not digit • Second character it may be letter or digit • Special characters (other then letters or digits) are not allowed Except underscore(_) • Length of Identifiers 8, 31 etc.
  • 10.
    Type of Identifiers– Data Type • Like we have our Gender Males / Females. Similarly in each language we have some types of identifier known as Data types. • What data type does is categorize data. • These basic categorizations or data types are integer(int), • Real(float), • Character (char)
  • 11.
    • char –1 byte - -128 to 127 • Character can declare as char c; • int – 2 bytes - Range (-32768 to 32767) • Integer can declare as int a; • float – 4 bytes - 3.4X10-38 to 3.4X1038 • Real can declare as float f;
  • 12.
    Why int’s rangeis -32768 to 32767 ? Integer Representation • Most of the computers use 2 bytes to store an integer. • The left most bit, out of sixteen bits, is used to indicate the sign of the integer and is called Sign bit. • The other 15 bits are used to store the given integer, a 0 in the sign bit position indicates a positive integer and 1 in this position means the integer stored is negative.
  • 13.
    Integer Representation 15 1413 12 11 10 9 8 7 6 5 4 3 2 1 0 0/1 Sign Bit 15 bits for Number representation Maximum Number 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +215-1 = 32767 Minimum Number 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -215 = -32768 Unsigned Numbers are numbers without sign i.e. First bit is used for number itself. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Range is 0 to 216 -1 i.e. 0 to 65535
  • 14.
    If we wantbigger number then the ranges defined. • char – strings String is collection of character • int – short, long, unsigned, unsigned long Short is of 1 byte. Long is of 4 bytes(double precession(size)) • float - double Double is of 8 Bytes
  • 15.
    Constants • int –Decimal, Octal, Hexadecimal • Decimal int – 77 equals 77 • Octal int – 077 equals 63 • Hexadecimal int – 0x77 equals 119 • Unsigned int – 45678U • Unsigned long – 243567849UL • Float – • Char - ‘a’ • String – “Rozy” ,
  • 16.
    Sentences / Statements •Third thing we learn in any language are sentences, here in programming these are known as statements. • Statements are the instructions given to the computer to perform any kind of action. • Statements form the smallest executable unit within a C++ program. • As sentences terminated by some full stop(., I), similarly, Statements are terminated with a semicolon (;).
  • 17.
    Types of Statements •Simple Statements • Compound Statements • Control Statements Simple Statement – Single statement. The simplest statement is the empty, or null statement. e.g. ; // it is a null statement. Examples of simple statement are as x=y; x=x+y; scanf(“%d”, &ajay); printf(“%d”, ajay);
  • 18.
    • Compound Statement– paragraph (Block) A compound statement in C++ is a sequence of statements enclosed by a pair of braces ({}). e.g., { statement 1; statement 2; }, A compound statement is treated as a single unit or statement. • Control Statements – which controls flow of statements like decision/ branching and loops
  • 19.
    Operators • Operators aretokens that trigger some computation when applied to variable and other objects in an expression. Classifications of Operators are Depending upon the number of operands we have : • Unary operators : These operators require just 1 operand. e.g, - (minus sign), ++, --, sizeof etc. • Binary operators: These operators take 2 operands. e.g. +, -, *, / , <, >, !=, <= • Ternary operators: These operators take 3 operands, e.g. Conditional operator (? : ) e.g. (A >B?5:6)
  • 20.
    Operators by Operations •Arithmetic Operators : Integer and Real Integer : +,-, *, /, % Real : +, -, *, / • Relational Operators : <, >, <=, >=, • Equality Operators : !=, == • Logical Operators : && (AND), ||(OR), !(NOT) • Assignment Arithmetic Operators : +=, *=, /= etc. e.g. a+=5 is equivalent to a=a+5. These are also known as Compound Assignment or Shorthand's
  • 21.
    Operators by Operations •Increment Operator • Decrement Operators : Similarly pre and post decrement operators e.g. - -a; and a- -; Pre increment : ++a ++a means a = a +1 Pre means before execution of statement e.g. If a=5; Post increment : a++ a++ means a = a +1 Post means after execution of statement e.g. If a=5; printf(“%d”, a); //returns 5 printf(“%d”, ++a); //returns 6 printf(“%d”, a); //returns 6 printf(“%d”, a); //returns 5 printf(“%d”, a++); //returns 5 printf(“%d”, a); //returns 6
  • 22.
    Hierarchy / Precedenceof Operators with their Associativity Operators Category Operators Associativity Unary Operators -, ++, - - , !, sizeof , (type) Right → Left (R→L) Arithmetic * , / , %, Left → Right (L → R) + , - Relational < , <=, >, >= L → R Equality == , != L → R Logical && L → R || L → R Assignment =, +=, -=, *=, /=, %= R → L
  • 23.
    Data Input andOutput • In C you can input/output using input and output library function. • Functions – User Defined and Library Function • These I/O Library functions are getchar(), putchar(), scanf(), printf(), gets() and puts(). • These six functions permits the transfer of information between the computer and the standard I/O devices(e.g. Keyboard, VDU etc.) • An I/O fxs. Can be accessed from anywhere within the program simply by writing the function name.
  • 24.
    I/O Statements • Firstwe discuss about Single Character and Strings I/O Functions Single Character I/O Functions are getchar() and putchar () String I/O Functions are gets() and puts() char c; Input statement like this c = getchar(); Output statement like this putchar( c ); char name[20]; Input statement like this gets(name); Output statement like this puts(name);
  • 25.
    scanf() • With thehelp of scanf() we can enter any type of data and mixed data. In general terms scanf() function is written as scanf( control string, arg1, arg2, arg3…, argn); • Control String consists of individual groups of characters, with one character group for each input data item. • Each character group must begin with a percent(%) sign and followed by conversion character which indicates the type of corresponding data item.
  • 26.
    Commonly used conversioncharacters for data input are • c for single character, • d is for decimal integer, • e for floating point value, • f for floating point value, • l is for long etc…. • The arguments are written as variables, arrays, whose types match the corresponding character group in the control string. • Each variable must be preceded by an ampersand (&). • Array name should not begin with &.
  • 27.
    Examples char name[20]; int roll;float marks; scanf(“ %s %d %f”, name, &roll, &marks); • Formatted scanf() function int a,b,c; scanf(“%3d, %3d %3d”, &a, &b, &c); In the above statement all a,b and c can take maximum of 3 digits.
  • 28.
    printf() • It issimilar to input function scanf(), except that its purpose is to display data rather than to enter it into the computer. • Also there is no ampersand (&) symbol before args. printf(“ %s n %d n %f”, name, roll, marks);
  • 29.
    Formatted printf() function •example int a,b,c; printf(“%3d, %3d %3d”, a, b, c); • In the above statement all a,b and c can display minimum of 3 digits or spaces instead of digits.
  • 30.
    Structure of CProgram # include < header file> // # is pre-processor directive #define x 5; //symbolic constant int a, b; //global variable declaration int fxn(); // function declaration main() { int i,j,k; // local variable declaration Input statements; Process; Output Statements; }
  • 31.
    Program 1 # include< stdio.h> main( ) // by default main function is int type { int a=5,b=6,c; c=a+b; printf(“The sum is = %d”, c); }
  • 32.
    Program 2 # include< stdio.h> main( ) { int a,b,c; scanf(“%d, %d”, &a, &b); c=a+b; printf(“The sum is = %d”, c); }
  • 33.
    Program 3 # include< stdio.h> void main() /* void is data type which says function should not return any value*/ { int a,b; scanf(“%d, %d”, &a, &b); printf(“The sum is = %d”, a+b); }
  • 34.
    Program 4 # include< stdio.h> #include <conio.h> void main() { int a,b; clrscr(); printf(“Please enter the value of a & b”); scanf(“%3d, %3d”, &a, &b); printf(“The sum is = %4d”, a+b); }