Lect07-C Fundamentals PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

C Fundamentals

Dr. Odelu Vanga

Department of Computer Science and Information Systems


Birla Institute of Technology and Science Pilani
Hyderabad Campus
[email protected]

January 28, 2020

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 1 / 36


Today’s Topics

Variables and Data Types


Reading Variables and Printing
Scope of Variable
Character digit to Number digit conversion

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 2 / 36


C-Tokens

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 3 / 36


C-Tokens

Keywords - lowercase

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 3 / 36


C-Tokens

Keywords - lowercase

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 3 / 36


Variables

A memory location is identified by its address

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Variables

A memory location is identified by its address


In a HLL, a location is identified with a name, called a variable

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Variables

A memory location is identified by its address


In a HLL, a location is identified with a name, called a variable

Variable Name (Not keywords)


Any combination of letters, numbers, and underscore ( )

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Variables

A memory location is identified by its address


In a HLL, a location is identified with a name, called a variable

Variable Name (Not keywords)


Any combination of letters, numbers, and underscore ( )

Rules for variable names:


Case sensitive
− “name” is different than “Name”

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Variables

A memory location is identified by its address


In a HLL, a location is identified with a name, called a variable

Variable Name (Not keywords)


Any combination of letters, numbers, and underscore ( )

Rules for variable names:


Case sensitive
− “name” is different than “Name”
Cannot begin with a number
− Usually, variables beginning with underscore
are only used in special library routines

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Variables

A memory location is identified by its address


In a HLL, a location is identified with a name, called a variable

Variable Name (Not keywords)


Any combination of letters, numbers, and underscore ( )

Rules for variable names:


Case sensitive
− “name” is different than “Name”
Cannot begin with a number
− Usually, variables beginning with underscore
are only used in special library routines
Only first 31 characters are considered

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 4 / 36


Constants

Constants:
Fixed values that the program cannot alter during its execution

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 5 / 36


Data Types
C has three basic data types:
int stores integer (4 bytes)
float stores real number (4 bytes)
char stores character (1 byte)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 6 / 36


Data Types
C has three basic data types:
int stores integer (4 bytes)
float stores real number (4 bytes)
char stores character (1 byte)
Integer
123 /* decimal */
−123
0x123 /* hexadecimal */

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 6 / 36


Data Types
C has three basic data types:
int stores integer (4 bytes)
float stores real number (4 bytes)
char stores character (1 byte)
Integer
123 /* decimal */
−123
0x123 /* hexadecimal */
Floating point
6.023
6.023e23 /* 6.023 × 1023 */

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 6 / 36


Data Types
C has three basic data types:
int stores integer (4 bytes)
float stores real number (4 bytes)
char stores character (1 byte)
Integer
123 /* decimal */
−123
0x123 /* hexadecimal */
Floating point
6.023
6.023e23 /* 6.023 × 1023 */
Character
‘c‘
‘\n‘ /* newline */

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 6 / 36


Variable Declaration, Reading and Printing

Syntax: DataType VariableName;


Every statement in C end with a semicolon (;)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 7 / 36


Variable Declaration, Reading and Printing

Syntax: DataType VariableName;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 8 / 36


Variable Declaration, Reading and Printing

Syntax: DataType VariableName;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 9 / 36


Variable Declaration, Reading and Printing

Syntax: DataType VariableName;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 10 / 36


Variable Declaration, Reading and Printing

Syntax: DataType VariableName;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 11 / 36


Variable Declaration, Reading and Printing
Syntax: DataType VariableName;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 12 / 36


Variable Declaration, Reading and Printing
Syntax: DataType VariableName;

Input: 3 4 u
Output: x = 3, y = 4.000000, ch = u
Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 12 / 36
Syntax of printf() and scanf()

printf(formatString, list-of-variables);

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 13 / 36


Syntax of printf() and scanf()

printf(formatString, list-of-variables);

format string - specify the text to be print


printf (“Sum of x and y = %d\n”, x+y);

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 13 / 36


Syntax of printf() and scanf()

printf(formatString, list-of-variables);

format string - specify the text to be print


printf (“Sum of x and y = %d\n”, x+y);
format specifier %d - works as a placeholder - embedded in the
output as a decimal number in place of %d.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 13 / 36


Syntax of printf() and scanf()

printf(formatString, list-of-variables);

format string - specify the text to be print


printf (“Sum of x and y = %d\n”, x+y);
format specifier %d - works as a placeholder - embedded in the
output as a decimal number in place of %d.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 13 / 36


Syntax of printf() and scanf()

scanf(formatString, list-of-addresses-of-variables);

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 14 / 36


Syntax of printf() and scanf()

scanf(formatString, list-of-addresses-of-variables);

formatString contains only format specifier of the variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 14 / 36


Syntax of printf() and scanf()

scanf(formatString, list-of-addresses-of-variables);

formatString contains only format specifier of the variables


List of variables are separated by commas and should be
address of the variable

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 14 / 36


Syntax of printf() and scanf()

scanf(formatString, list-of-addresses-of-variables);

formatString contains only format specifier of the variables


List of variables are separated by commas and should be
address of the variable and hence generally have & (ampersand)
before variable name.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 14 / 36


Syntax of printf() and scanf()

scanf(formatString, list-of-addresses-of-variables);

formatString contains only format specifier of the variables


List of variables are separated by commas and should be
address of the variable and hence generally have & (ampersand)
before variable name.
scanf(“%d%f”, &x, &y);

where x is integer type and y is float type

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 14 / 36


Scope: Global and Local

Scope of Variable
accessible
throughout program
within the block

Global: accessed anywhere in program

Local: only accessible in a particular region


Compiler infers scope from where variable is declared
− programmer no need to explicitly state

Variable is local to the block in which it is declared


− block defined by open and closed braces {}
Global variable is declared outside all blocks

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 15 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 16 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 17 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 18 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 19 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 20 / 36


Example: Global and Local Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 21 / 36


Example: Scope of Variables

Note: G defined only once


Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 22 / 36
Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 23 / 36


Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 24 / 36


Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 25 / 36


Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 26 / 36


Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 27 / 36


Example: Scope of Variables

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 28 / 36


Assignment Operator

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 29 / 36


Assignment Operator

Assignment associates right to left.


y = x = 3;
y gets the value 3, because (x = 3) evaluates to the value 3.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 29 / 36


Assignment Operator

Assignment associates right to left.


y = x = 3;
y gets the value 3, because (x = 3) evaluates to the value 3.
int x, y=5, z=5;
x = y == z;
printf(“%d”, x); precedence of ‘ =0 is less than 0 == ‘.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 29 / 36


Assignment Operator

Assignment associates right to left.


y = x = 3;
y gets the value 3, because (x = 3) evaluates to the value 3.
int x, y=5, z=5;
x = y == z;
printf(“%d”, x); precedence of ‘ =0 is less than 0 == ‘.
int a = 1,2,3;
printf(“%d”,a);
Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 29 / 36
Quotient and Remainder Operator

Quotient: a/b returns quotient, for any a and b


integers.
Example: 2/3 = 0, 3/2 = 1, 5/2 = 2

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 30 / 36


Quotient and Remainder Operator

Quotient: a/b returns quotient, for any a and b


integers.
Example: 2/3 = 0, 3/2 = 1, 5/2 = 2

Modulus (remainder): a%b returns remainder, for


any a and b integers.
Example: 2%3 = 2, 3%2 = 1, 5%2 = 1

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 30 / 36


ASCII Table for Alphabets and Digits

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 31 / 36


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 31 / 36


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 31 / 36


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant
The integer equivalent to the character is its ASCII value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 31 / 36


ASCII Table for Alphabets and Digits

A character represent within single quotes, ex. ‘A’


Each character we call character constant
The integer equivalent to the character is its ASCII value

printf(“ASCII of A is %d\n”, ‘A’);


Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 31 / 36
Example: Char digit to Number digit

Write an algorithm to convert


a given digit character to digit number

2 + 2 = 4, but ‘2‘ + 2 = 52 ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 32 / 36


Example: Char digit to Number digit

Write an algorithm to convert


a given digit character to digit number

2 + 2 = 4, but ‘2‘ + 2 = 52 ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 32 / 36


Example: Char digit to Number digit

Read a number as individual character digits,


and convert it into integer
Ex: Read characters ‘1’, ‘2’, ‘3’, ‘4’ and
convert it to integer 1234.

Draw a flowchart for the above algorithm

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 33 / 36


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 34 / 36


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 34 / 36


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.
3. Write an algorithm to find the sum of last two
digits. For above inputs, output 1 + 3 = 4 and
2 + 4 = 6.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 34 / 36


Example

1. Write an algorithm to delete the last digit. input


13613, output 1361. input 324, output 32.
2. Write an algorithm to delete last two digits. input
13613, output 136. input 324, output 3.
3. Write an algorithm to find the sum of last two
digits. For above inputs, output 1 + 3 = 4 and
2 + 4 = 6.
4. Write an algorithm to print the second last digit.
input 23617, output 1.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 34 / 36


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 35 / 36


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 35 / 36


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.
7. Write an algorithm to exchange last two digits.
e.g. input 23617 output 23671.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 35 / 36


Example

5. Write an algorithm to double the last digit. e.g.


input 23613, output 23616. input 324, output 328.
(assume that last digit is less than 5)
6. Write an algorithm to delete the second last digit.
e.g. input 23617 output 2367.
7. Write an algorithm to exchange last two digits.
e.g. input 23617 output 23671.
8. Write an algorithm to exchange last and third last
digit. e.g. input 23617 output 23716.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 35 / 36


.

Thank You

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 28, 2020 36 / 36

You might also like