[go: up one dir, main page]

0% found this document useful (0 votes)
156 views27 pages

C Programming Language: C Is Case-Sensitive

C is a structured programming language that allows programmers to concentrate on solving problems without worrying about machine-level details. It is case-sensitive and uses tokens like keywords, identifiers, string literals, and operators. C supports basic data types like integers, characters, and floating-point numbers of various sizes. Programs declare variables and constants globally or locally and specify their data type and scope. C uses precedence rules and type conversions to evaluate expressions and operators.

Uploaded by

mahbub mitul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views27 pages

C Programming Language: C Is Case-Sensitive

C is a structured programming language that allows programmers to concentrate on solving problems without worrying about machine-level details. It is case-sensitive and uses tokens like keywords, identifiers, string literals, and operators. C supports basic data types like integers, characters, and floating-point numbers of various sizes. Programs declare variables and constants globally or locally and specify their data type and scope. C uses precedence rules and type conversions to evaluate expressions and operators.

Uploaded by

mahbub mitul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

C Programming Language

 C is a structured programming language.

 It is considered a high-level language because it allows the


programmer to concentrate on the problem at hand and not worry
about the machine that the program will be using.

C is case-sensitive

1
Tokens in C
 Keywords
 These are reserved words of the C language. For example int,
float, if, else, for, while etc.

 Identifiers
 An Identifier is a sequence of letters and digits, but must start with a
letter. Underscore ( _ ) is treated as a letter. Identifiers are case sensitive.
Identifiers are used to name variables, functions etc.
 Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If
 Invalid: 324, short, price$, My Name

2
Tokens in C
 String Literals
 A sequence of characters enclosed in double quotes as “…”. For
example “13” is a string literal and not number 13. ‘a’ and “a” are
different.

 Operators
 Arithmetic operators like +, -, *, / ,% etc.
 Logical operators like ||, &&, ! etc. and so on.

 White Spaces
 Spaces, new lines, tabs, comments ( A sequence of characters enclosed
in /* and */ ) etc. These are used to separate the adjacent identifiers,
keywords and constants.

3
Basic Data Types
 Integer Types
 Integers are stored in various sizes. They can be signed or unsigned.

4
Basic Data Types
 Integer Types
 short int Stored as 16 bits. Unsigned 0 to 65535.
Signed -32768 to 32767.
 int Same as either short or long int.
 long int Stored as 32 bits. Unsigned 0 to 4294967295.
Signed -2147483648 to 2147483647

5
Basic Data Types
 Character Types

 char Stored as 8 bits. Unsigned 0 to 255.


Signed -128 to 127.

6
Data Types

7
Constants
 Character and string constants
 ‘c’ , a single character in single quotes are stored as char.
Some special character are represented as two characters in single
quotes.
‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’ = double quotes.
Char constants also can be written in terms of their ASCII code.

 A sequence of characters enclosed in double quotes is called a string


constant or string literal. For example
“Charu”
“A”
“3/9”
“x = 5”

8
Variables
 Naming a Variable
 Must be a valid identifier.
 Must not be a keyword
 Names are case sensitive.
 Variables are identified by only first 32 characters.
 Naming Styles: Uppercase style and Underscore style
 lowerLimit lower_limit
 incomeTax income_tax

An identifier must start with a letter or


underscore; it may not have a space or a hyphen.

9
Declarations
 Declaring a Variable

10
Declarations
 Declaring a Variable
 Each variable used must be declared.
 A form of a declaration statement is
data-type var1, var2,…;
 Declaration announces the data type of a variable and allocates
appropriate memory location. No initial value (like 0 for integers) should
be assumed.
 It is possible to assign an initial value to a variable in the declaration
itself.
data-type var = expression;
 Examples
int sum = 0;
char newLine = ‘\n’;

11
Global and Local Variables
 Global Variables /* Compute Area and Perimeter of a
circle */
 These variables are #include <stdio.h>
float pi = 3.14159; /* Global */
declared outside all
functions. main() {
 Life time of a global float rad; /* Local */
variable is the entire
execution period of the printf( “Enter the radius “ );
scanf(“%f” , &rad);
program.
 Can be accessed by any if ( rad > 0.0 ) {
function defined below the float area = pi * rad * rad;
declaration, in a file. float peri = 2 * pi * rad;

printf( “Area = %f\n” , area );


printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);

printf( “Area = %f\n” , area );


}

12
Global and Local Variables
 Local Variables /* Compute Area and Perimeter of a
circle */
 These variables are #include <stdio.h>
float pi = 3.14159; /* Global */
declared inside some
functions. main() {
 Life time of a local float rad; /* Local */
variable is the entire
execution period of the printf( “Enter the radius “ );
scanf(“%f” , &rad);
function in which it is
defined. if ( rad > 0.0 ) {
 Cannot be accessed by any float area = pi * rad * rad;
other function. float peri = 2 * pi * rad;

printf( “Area = %f\n” , area );


printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);

printf( “Area = %f\n” , area );


}

13
Precedence and Order of evaluation

14
Precedence and Order of evaluation

15
Operators
 Arithmetic Operators
 +, - , *, / and the modulus operator %.
 + and – have the same precedence and associate left to right.
3 – 5 + 7 = ( 3 – 5 ) + 7  3 – ( 5 + 7 )
3 + 7 – 5 + 2 = ( ( 3 + 7 ) – 5 ) + 2
 *, /, % have the same precedence and associate left to right.
 The +, - group has lower precedence than the *, / % group.
3 – 5 * 7 / 8 + 6 / 2
3 – 35 / 8 + 6 / 2
3 – 4.375 + 6 / 2
3 – 4.375 + 3
-1.375 + 3
1.625

16
Operators
 Arithmetic Operators
 % is a modulus operator. x % y results in the remainder when x is divided
by y and is zero when x is divisible by y.
 Cannot be applied to float or double variables.
 Example
if ( num % 2 == 0 )
printf(“%d is an even number\n”, num)’;
else
printf(“%d is an odd number\n”, num);

17
Type Conversions
 The operands of a binary operator must have a the same type and the
result is also of the same type.
 Integer division:
c = (9 / 5)*(f - 32)
The operands of the division are both int and hence the result also would
be int. For correct results, one may write
c = (9.0 / 5.0)*(f - 32)
 In case the two operands of a binary operator are different, but
compatible, then they are converted to the same type by the compiler.
The mechanism (set of rules) is called Automatic Type Casting.
c = (9.0 / 5)*(f - 32)
 It is possible to force a conversion of an operand. This is called Explicit
Type casting.
c = ((float) 9 / 5)*(f - 32)

18
Automatic Type Casting
1. char and short operands are converted to int Hierarchy
2. Lower data types are converted to the higher data Double
types and result is of higher type.
3. The conversions between unsigned and signed types float
may not yield intuitive results. long
4. Example
Int
float f; double d; long l;
int i; short s; Short and
d + f f will be converted to double char
i / s s will be converted to int
l / i i is converted to long; long result

19
Explicit Type Casting
 The general form of a type casting operator is
 (type-name) expression
 It is generally a good practice to use explicit casts than to rely on
automatic type conversions.
 Example
C = (float)9 / 5 * ( f – 32 )
 float to int conversion causes truncation of fractional part
 double to float conversion causes rounding of digits
 long int to int causes dropping of the higher order bits.

20
Operators
 Relational Operators
 <, <=, > >=, ==, != are the relational operators. The expression
operand1 relational-operator operand2
takes a value of 1(int) if the relationship is true and 0(int) if relationship is
false.
 Example
int a = 25, b = 30, c, d;
c = a < b;
d = a > b;
value of c will be 1 and that of d will be 0.

21
Operators
 Logical Operators
 &&, || and ! are the three logical operators.
 expr1 && expr2 has a value 1 if expr1 and expr2 both are
nonzero.
 expr1 || expr2 has a value 1 if at least one of expr1 and
expr2 is nonzero.
 !expr1 has a value 1 if expr1 is zero else 0.
 Example
 if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
 If ( marks < 40 || attendance < 75 ) grade = ‘N’

22
Operators
 Assignment operators
 The general form of an assignment operator is
 v op = exp
 Where v is a variable and op is a binary arithmetic operator. This
statement is equivalent to
 v = v op (exp)
a = a + b can be written as a += b
a = a * b can be written as a *= b
a = a / b can be written as a /= b
a = a - b can be written as a -= b

23
Operators
 Increment and Decrement Operators
 The operators ++ and –- are called increment and decrement operators.
 a++ and ++a are equivalent to a += 1.
 a-- and --a are equivalent to a -= 1.
 ++a op b is equivalent to a ++; a op b;
 a++ op b is equivalent to a op b; a++;
 Example
Let b = 10 then
(++b)+b+b = ?
b+(++b)+b = ?
b+b+(++b) = ?
b+b*(++b) = ?

24
First C Program

25
First C Program

26
Comments

27

You might also like