C Language : C Language Kiran SVS
Class Agenda : Class Agenda Introduction
C Character set and Keywords
Constant
Variables and Identifiers
Data Type
Modifiers
Operators
Printf() and Scanf()
Format Specifier
Escape Sequences
Structure of a C program
Conditional Operator
Slide 3 : Sizeof() operator
Increment and Decrement operators
Shift Operators
Bitwise Operators
Introduction : Introduction C is a programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratory to be used with UNIX.
C is usually called a Middle or High level language.
Assembly and machine languages are low level languages.
Programming language is an artificial language using which instructions are sent to the computer, that gets translated to the machine language and are executed by the computer.
C Character Set and Keywords : The C Character set consists of
1.Letters (A to Z and a to z ) 2. Digits (0 to 9 ) 3. Special Characters ( ! , %, &, { ,……) 4. White Spaces ( Blank Space, Horizontal Tab , Carriage Return, New Line, Form Feed )
Computers can only understand numbers, so each character is given a numerical value called as ‘ ASCII ’ code.
Eg: A (65) , a (97) , Null (0) , 0 (48), $ (36)
ASCII stands for American Standard Code for Information Interchange.
Keywords (or) Reserved words
"Keywords" are words that have special meaning to the C compiler and are always written in lower case.
These should not be used for any other purposes in the program. There are 32 keywords in C. C Character Set and Keywords
Keywords : Keywords
Constant : Constant A constant value is one which does not change during the execution of a program.
C primarily supports following constants.
Integer-constant
floating-point-constant
character-constant
Integer Constants : An "integer constant" can be a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value.
decimal integers: consists of a set of digits 0 to 9
Valid Invalid
123 13 600 -31 10,500 562321 3500$ + 78
Octal Integers: consists of any combination of digits from 0 through 7.
A preceding 0 at the beginning represents it is an octal constant.
eg: 026 , 0347
Hexadecimal integer : consists of digits from 0 to 9 and alphabets from A to F ( a to f). The alphabets A to F refers to 10 to 15 in decimal integer.
constant is preceded by 0X or 0x
eg: 0X25 0x8C 0xbcd Integer Constants
Floating-point-constants : Floating-point-constants Constants with fractional part are called Floating-point constants
Eg: 15.75
0.0025
3.142
Floating-point constants have type float, double, or long double.
A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. For example:
100.0L /* Has type long double */
100.0F /* Has type float */
Character Constants
A "character constant" is formed by enclosing a single character from the representable character set within single quotation marks (' ').
eg: ‘t‘ , ‘4‘, ';‘ , ‘ ‘
All character constants have an equivalent integer value which are called ASCII Values.
Constant samples : Constant samples 7 - integer constant
‘7’ - character constant
“7” – string constant
23 – integer constant
034 – octal integer constant
038 – (error)
0x6a – Hexadecimal integer constant
‘23’ – (error)
“23” - string
10.56 - float
‘10.56’ - (error)
“10.56” – string
‘a’ - character constant
‘USA’ – (error)
“USA” - string
“USA” has no ASCII value but each character in USA has ASCII value
Variables and Identifiers : a 15 a
a = a + 5 10 Variable is the name given to a memory location, where data is stored.
All variables in C must be explicitly defined before use.
int a = 10; Variables and Identifiers int a = 10; type identifier Value
passed
Identifier : Identifier is the name given to a variable ( or array, function, label etc ).
Rules for Identifiers:
Should start with Alphabet (or underscore)
Only Alphanumeric characters are allowed.
No special characters are allowed except underscore( _ )
Is case sensitive
Should not be a reserved word Identifier
Identifier samples (or) variable name samples : Identifier samples (or) variable name samples Valid
svs
_1stname
Country_name
Ab_1_2
member_at_wiziq Invalid
123a
1stname
Country name
case
member@wiziq Int WIZIQ ; int Member;
float Wiziq; float Member;
Data Type ( or type) : Data Type ( or type) A Variable always have a type, that describes
Kind of data that can be stored
Amount of memory occupied
There are basically two types of data
Numeric ( Whole numbers and decimal numbers )
Text
C provides four primary data types
Integer type
Floating point type
Character type
Void
Continued… : Continued… Integer Type:
Integers are used to represent whole numbers, and has machine dependent range of values. Denoted by keyword int.
Floating point type:
Floating point types are used to represent fractional numbers with 6 digits precession. Floating point types are denoted by keyword float.
To increase the range and accuracy of the floating point numbers we can use double. Double is same as float but with longer precession. Also there is long double for further accuracy.
Character type:
Are used to represent single character. Denoted by keyword char.
Void:
When a function doesn’t return anything we mention the return type as void.
Modifiers : Modifiers Modifiers are used to modify the range of values stored by variables of a datatype. We have 4 types of modifiers available
Short
Long
Signed
unsigned
Integers can be prefixed with all four to get the following
short int signed int
Long int unsigned int
Char can be prefixed with signed and unsigned
Signed char unsigned char
Data types and Range of values : Data types and Range of values Type Size Range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
Short 2 bytes –32,768 to 32,767
unsigned short 2 bytes 0 to 65535
Int 2 bytes -32768 to 32767
unsigned int 2 bytes 0 to 65535
long 4 bytes -2147483648 to 2147483647
unsigned long 4 bytes 0 to 4294967295
float 4 bytes 3.4 e-38 to 3.4 e+38
double 8 bytes 1.7 e-308 to 1.7 e+308
Long Double 10 bytes 3.4 e-4932 to 3.4 e+4932
Variable samples : Variable samples int a = 25; Variable a is created with 2 bytes of memory a
Value of a is 25 , address of a is &a (924)
& is address operator 924 int b; b
Variable b contains garbage value , since not initialized
865 int a = 32768;
we cannot store more than 32767 where a becomes -32768
Otherwise we can declare unsigned int a = 32768; float b = 10.68; b
Variable b is created with 4 bytes of memory
Value of b is 10.68 and address of b is &b 726 25 ---- 10.68
Continued… : Continued… char ch = ‘g’ ;
Variable ch is created with 1 byte of memory.
ch contains ‘g’ and address is &ch char ch = 65 ;
ch cannot store int values, so ASCII value corresponding to 65 is stored.
ch = ‘A’ long p = 10000;
double s = 2500000.689;
unsigned x = -25;
Operators : Operators Operator performs an action on operand/operands. C ++ supports the following types of operators
Arithmetic operators
Assignment operator
Relational operators
Logical operators
Bitwise operators
Increment and decrement
Conditional operator
Other operators
Operators Continued… : Operators Continued… Arithematic operators
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulus
Modulus(%) evaluates the remainder between operands after division.
E.g int y = 9%2; ( y contains 1 )
% is an integer operator.
Division ( / ) evaluates the coefficient between operands.
E.g float x = 9/2; ( x has 4 ) float x = 9.0/2 ( x has 4.5 )
Operators Continued… : Operators Continued… Relational operators: evaluate to TRUE or FALSE
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Assignment operator ( = ) (+= , -=, *=, /=, %= )
Assigns a value from the right hand side of the expression to the variable on the left hand side.
E.g x = 4; ( RHS is a value )
a = 2x + 3y; (RHS is an expression )
Operators Continued… : Operators Continued… Logical operators: evaluate to TRUE or FALSE
&& (logical AND ) evaluates to true if both the operands are true
|| (logical OR ) evaluates to true if at least one of the operands are true
! ( logical NOT ) is simply a negation
E.g x = 10; y = 4;
x<3 && y!=5
!(x<3 && y!=5 )
y=3 && x>9
x==10 || y>5
!(x==10 || y>5 ) FALSE
TRUE
TRUE
TRUE
FALSE
Operator samples : Operator samples int x = 9 , y = 2, z = 0, w = -3
int res ;
res = x*y;
/* res has 18 */
res = x/y;
/* res has 4 */
res = (float)x/y;
/* res has 4.5 */
Note: Atleast one operand should be float to get float division
res = x%y;
/* res has 1 */
res = 1;
res += 5;
/* res has 6 */ ( res = res + 5 ) res *= y;
/* res has 12 */
res = x > z;
/* res has 1 */
res = !w
/* res has 0 */
res = !0
/* res has 1 */
res = (y == 3)
/* res has 0 */
Printf() : Printf() This is an output function used to display given information on the monitor during execution time. This is a call by value function.
Syntax: printf(“format strings”, var1 , var2 , …);
Eg: 1) int a = 25;
printf(“%d”, a);
25 is displayed on the monitor
Eg: 2) int a = 10 , b = 20, c = 15;
printf(“%d %d %d”, a, b, c);
o/p is 10 20 15
Eg:3) int x = 17;
float y = 10.5;
char ch = ‘g’;
printf(“%d %f %c”, x , y, ch)
o/p is 17 10.5 g
What is format specifier? : a
986 10 For eg :
int a = 10; What is format specifier? But internally 10 is stored as a binary number 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
But dsjfdslkfjdkl When we print the value stored in the variable a, we should tell the format in which the value should be printed.
printf(“%d”, a) ;
the value stored in the address of a ( 986) is printed and %d tells to print the value in the integer format. 986 987 2 bytes
Format Specifiers : Format Specifiers %c char
%d or %i int
%f float
%ld long int
%lf long float (double)
%s String
%u Unsigned int
%o octal int
%x hexadecimal int
%e Signed floating-point value in E notation
%g Signed value in %e or %f format, whichever is shorter
Escape sequences (or) Back slash character constants : Escape sequences (or) Back slash character constants Character combinations consisting of a backslash (\) followed by a character are called “escape sequences”
\ ( escape character) changes the general meaning of next immediate character and implements new meaning.
\a beeps
\b back space (moves cursor one place back)
\f form feed ( ejects printer page, may clear screen on some computers )
\n new line ( moves cursor to beginning of next line )
\t horizontal tab
\r carrier return ( moves cursor to the beginning of the line )
\v vertical tab ( only for printer)
\‘ Single quotation mark
\“ Double quotation mark
\\ Backslash
Printf samples : Printf samples Eg: 1) int a = 32;
float b = 34.6;
printf(“%d\n%f”, a, b);
o/p is 32 34.6
printf(“%d\t%f”, a, b);
o/p is 32 34.6
Eg:2) printf(“India\n”)
printf(“America\n\n”)
printf(“Russia”)
o/p is India
America
Russia
Eg: 3) int a = 10;
int b = 15;
printf(“Result is %d”, a + b);
o/p is Result is 25.
Scanf() : Scanf() Scanf() function read information from a standard input device (keyboard) during execution time.
Syntax :scanf(“format specifier", &var1, &var2,…);
Eg:1) int a;
scanf(“%d”, &a);
If &a is 1000, the value entered by user is stored in the memory location at address 1000.
Scanf doesn’t read a value if & is missing.
Eg:2) int a, b, c;
scanf(“%d %d %d”, &a ,&b ,&c);
If user enters 10, 20, 30 they are stored in a, b, c .
The values can be separated by spacebar or enter key
If we want to be a particular seperator
scanf(“%d,%d,%d”, &a ,&b ,&c);
Now the values must be separated by comma( , ) and we should not use anything else.
Scanf() returns no of successful inputs.
printf() and scanf() are defined in
Structure of C Program : Structure of C Program #include
#include
main()
{
Declaration statements;
clrscr();
Executable statements;
getch();
}
Every C program must have a main, it is the entry point for execution.
# is called preprocessor directive and is processed by the preprocessor.
“#include” inserts the contents of the specified file at that point in the code
Every statement in C ends with semicolon( ; )
Conditional Operator ( ?: ) : Conditional Operator ( ?: ) It is a ternary operator
Syntax: var = exp1? exp2 : exp3
Eg: temp = 10>6?100:50
temp has 100
Eg: int x = 10, y = 20;
temp = (x>y) ? (2x+3y) : (4y)
temp has 80
The first operand is implicitly converted to bool
If the first operand evaluates to true(1), the second operand is evaluated
If the first operand evaluates to false(0), the third operand is evaluated
So the final result of conditional operator is the result of either 2nd or 3rd operands evaluation and not both.
Sizeof() Operator : Sizeof() Operator The sizeof() operator gives the amount of storage, in bytes
int a;
sizeof(int) (or) sizeof(a) is 2
float b;
sizeof(float) (or) sizeof(b) is 4
char ch;
sizeof(char) (or) sizeof(ch) is 1
It takes datatype or variable name as parameter.
Operators Continued… : Operators Continued… Increment and decrement operators ( ++, -- )
E.g x = 5
a = x++ ( post increment ) (a = 5, x = 6 )
a = ++x ( pre increment ) ( a = 6, x = 6 )
a = x-- ( post decrement )( a = 5, x = 4 )
a = --x ( pre decrement ) ( a = 4, x = 4 )
Note: (x++) = (++x) = (x = x+1) = (x+=1)
++ and -- are integer operators
a = 10.8
a++ ( error ) , 7++ ( error )
char ch = ‘b’;
ch++ => ch = ‘c’;
ASCII value is incremented
Shift operators : Shift operators Left shift, right shift(<<, >> )
E.g x = 9
0 0 0 0 1 0 0 1
x = x<<2
0 0 1 0 0 1 0 0
x value becomes 36
x = x >> 2
0 0 0 0 0 0 1 0
x value becomes 2
Left shift ( << ) once is nothing but multiplying x value by 2
right shift ( >> )once is divide by 2.
Eg: x = 25
x = x >> 1 => 25/2 = 12
Bitwise operators : Bitwise operators Do the bit level operation b/n operands
bitwise AND(&) compares each bit of first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1, otherwise 0
bitwise OR ( | ) sets the result bit to 1 if either bit is 1
bitwise XOR ( ^ ) sets the result bit to 1 , if one bit is 1 and other is 0. Otherwise sets to 0.
bitwise complement ( ~ )
a = 25 0 1 1 0 0 1
b = 46 1 0 1 1 1 0
a & b = 0 0 1 0 0 0 = 8
b = 46 1 0 1 1 1 0
~ b = 0 1 0 0 0 1 = 17
~ 1 = 0 and ~0 = 1