COMPONENT OF C LANGUAGE
(VARIABLE DATA TYPES, OPERATORS BETWEEN VARIABLES)
Variables
A variables is a temporary value that contains an identifier.
x=7
y=3
z=x+y
In the example, x and y are variables. Variable x is a memory location that has the identifier x and store
value 7 while variable y is a memory location that has the identifier x and store value 3. In the last line, z
also a variable. However, it use to hold the result of x+y. What do you think is the value held by z?
Variables
 A, B and C are variables in the pseudocode
 Variable names takes away the need for a programmer to access memory locations using their address
 The operating system takes care of allocating space for the variables
 To refer to the value in the memory space, we need to only use the variable name
Variables Names
A variable name is a user-defined identifier. There are general guidelines for variables name:
a) Do not use C identifiers such as printf, scanf, etc.
b) Do not use reserved words such as void, return, int, double, etc.
c) Digits as well as underscores are allowed
d) Do not use digits as the first characters.
Variables Names
Identifier Valid/Invalid Explanation
total Valid Valid name
_sales Valid Identifier may start with an underscore
2003sales Invalid Identifier cannot start with a digit
double Invalid It is a reserved word
puts Invalid It is an identifier
Account’s Invalid Character ‘ is not allowed
X*y Invalid Character * is not allowed
Sales2003 Valid Valid name
Net profit Invalid Spaced in between is not allowed
NETPROFIT Valid Uppercase is acceptable. However, lower case is
preferred. Upper case usually used for a constant
name
Data Types
We can store variables in our computer memory. Since each type of data takes
different amount of memory, like integer takes 2 bytes, decimal numbers take 4 byte,
and SINGLE character takes 1 byte. So, we must tell computer about the type of data
we are going to store in our variable.
1. Predefined/Inbuilt: These data type are inbuilt. E.g.: char, int, float, double
2. Derived data type: These data types are inherited from predefined data type. E.g.:
arrays, pointer.
3. User defined data type: These are created by user for his/her own purpose. E.g.:
typedef, enum, structure, union
Data Types
Data Type
Primitive
char
Int
Float
Double
Derived
Array
Pointer
Function
User
Defined
Enum
Structure
union
Data Types and its Format
Data Types and its Format
Character Set
 Character set is the set of the character
 This characters are used to form the word, numbers and expression.
 The characters in the c are grouped into the following categories.
Letters : Uppercase A…Z, Lowercase a…z
Digits : All decimal digits 0 to 9
Special Character : ,(comma) .(period) ; (semicolon) :(colon), & (ampersand), #
(number sign) etc.
White spaces : Blank Space, Horizontal Space, New Line.
Keywords
 Are simply the reserved words whose meaning has been explained in library of C
language.
 There are 32 keywords in c language.
 We can’t use keywords as identifiers or variables because if we do so we are trying
to assign a new meaning to the keyword, which is not allowed by the computer
int float;
char if;
int void;
Above will give ERROR as we are using keywords as identifier or variable name
List of Keywords
Constant
 Constant names are usually all in upper case to differentiate them from variable
names.
First method: A constant is declared like a variable, except that keywords const is
added before the variable’s data type:
const int DOZEN = 12;
const double PI = 3.14;
Second method: a constant is defined using the #define pre=processor. #define is a
directive that cause the computer to replace the identifier with value it is to.
example:
#define PI 3.14
Identifier
Value
Backslash character constant
 C supports special backslash character constants that are used in output functions.
 These character combinations are known as escape sequences.
Comments
 Comments are that part of program, which are ignored by compiler. These are used
for better readability of user. Compiler doesn‘t compile the text written inside
comments.
 The comment describes the purpose of the code in the file and might include some
license or copyright information.
 It is not compulsory to have comments in our program, but its good practice and
what most C programmers will expect to find.
1. Single Line Comment : // is used to make single line comment. All the text in a
line after // will become part of comment and will be ignored by compiler. E.g.,
int a; // this is ignored
2. Multiline Comment: This starts from /* and ends at */. All the text in between
these will be part of comment. E.g., /* This Is Multiline Comment */
Input/Output
 Almost all C programs have at least one output statement. Otherwise, the program
won’t output anything on the screen and there is no knowing if the program ran
successfully or not.
 The most common input/output functions are printf() and scanf() both of which are
defined in the header file stdio.h.
 Use printf() (Print with Format) for outputting data to the console and scanf() (Scan
with Format) for inputting data from the keyboard.
Input/Output [printf ( )]
 The syntax of the printf() function is
 Format is the typesetting of the output that you can control, and argument is a list
of variables to be printed.
 The printf() function prints the value(s) of variables in argument to the standard
output (screen) following the formatting command defined by format.
Input/Output [scanf ( )]
 The scanf() is the inverse of printf(), i.e., it scans the value(s) of variable(s) from the
standard input (keyboard) with format. The formatting part (i.e., % …) in scanf() is the
same as printf(). However, the variable name must be preceded by an &
(ampersand).
OPERATORS BETWEEN VARIABLES
Operators of C
 C supports a rich set of operators. Operators are used in programs to manipulate
data and variables.
 They usually form a part of the mathematical of logical expressions.
 C operators are classified into several categories. They include:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Increment and Decrement operators
Arithmetic Operators
Integer Arithmetic
 When both the operands in a single arithmetic expression are integers, the
expression is called an integer expression , and the operation is called integer
arithmetic.
 During modular division, the sign of the result is always the sign of the first operand.
 That is
 -14 % 3 = -2
 -14 % -3 = -2
 14 % -3 = 2
Real Arithmetic
 An arithmetic operation involving only real operands is called real arithmetic. If x
and y are floats, then we will have:
1. x = 6.0 / 7.0 = 0.857143
2. y = 1.0 / 3.0 = 0.333333
 The operator % cannot be used with real operands.
Mix-Mode Arithmetic
 When one of the operands is real and the other is integer, the expression is called a
mixed-mode arithmetic expression and its result is always a real number.
Eg: 15 / 10.0 = 1.5
Relational Operators
 The relational operators are mathematical operators often used in the if statement.
 It is important to understand the difference between a single equal sign (=) and
double equal signs (==) as the single equal sign (=) is used for an assignment while
the double equal signs (==) are used as mathematical equality.
Relational Operators
1. if (a==b) printf("a and b are equal.n");
else printf("a and b are not equal.n");
The statement above means that if the two variables, a and b, are the same, a string, “a and b
are equal.”, is printed, otherwise a string, “a and b are not equal.”, is printed.
One equal sign is for assignment and two equal signs are for logical equality. The double equal
signs in C are equivalent to the equal sign in regular mathematical equations.
Relational Operators
1. a = 10;
a = a + 1;
The statement a = a + 1 does not make sense as a mathematical expression. However, it makes
perfect sense as a C statement. a + 1 is first evaluated to be 11 and this value of 11 is then
assigned to a. Effectively, the value of a was incremented by 1.
Logical Operators
 Logical operators in C are mostly used within the if statement.
Logical Operators
Increment/Decrement/Substitution Operators
 C has two very useful operators that are not generally found in other languages.
 These are the increment and decrement operator:
 ++ and –
 The operator ++ adds 1 to the operands while – subtracts 1. It takes the following
form:
++m; or m++
--m; or m—
Increment/Decrement/Substitution Operators
Rules for ++ & -- operators
 These require variables as their operands.
 When postfix either ++ or – is used with the variable in each expression, the
expression is evaluated first and then it is incremented or decremented by one
 When prefix either ++ or – is used with the variable in each expression, it is
incremented or decremented by one first and then the expression is evaluated with
the new value
Increment/Decrement/Substitution Operators
Shorthand notations for assignment operations
EXERCISE
1. Print a character, “a”.
2. Print an integer 10
3. Print floating number 10.5
4. Print two floating numbers, 10.0 and -2.3
5. Write a program that reads temperature in Celsius and convert it to Fahrenheit.
Note
𝐶 = 𝐹 − 32 𝑋
5
9
Expected result

component of c language.pptx

  • 1.
    COMPONENT OF CLANGUAGE (VARIABLE DATA TYPES, OPERATORS BETWEEN VARIABLES)
  • 2.
    Variables A variables isa temporary value that contains an identifier. x=7 y=3 z=x+y In the example, x and y are variables. Variable x is a memory location that has the identifier x and store value 7 while variable y is a memory location that has the identifier x and store value 3. In the last line, z also a variable. However, it use to hold the result of x+y. What do you think is the value held by z?
  • 3.
    Variables  A, Band C are variables in the pseudocode  Variable names takes away the need for a programmer to access memory locations using their address  The operating system takes care of allocating space for the variables  To refer to the value in the memory space, we need to only use the variable name
  • 4.
    Variables Names A variablename is a user-defined identifier. There are general guidelines for variables name: a) Do not use C identifiers such as printf, scanf, etc. b) Do not use reserved words such as void, return, int, double, etc. c) Digits as well as underscores are allowed d) Do not use digits as the first characters.
  • 5.
    Variables Names Identifier Valid/InvalidExplanation total Valid Valid name _sales Valid Identifier may start with an underscore 2003sales Invalid Identifier cannot start with a digit double Invalid It is a reserved word puts Invalid It is an identifier Account’s Invalid Character ‘ is not allowed X*y Invalid Character * is not allowed Sales2003 Valid Valid name Net profit Invalid Spaced in between is not allowed NETPROFIT Valid Uppercase is acceptable. However, lower case is preferred. Upper case usually used for a constant name
  • 6.
    Data Types We canstore variables in our computer memory. Since each type of data takes different amount of memory, like integer takes 2 bytes, decimal numbers take 4 byte, and SINGLE character takes 1 byte. So, we must tell computer about the type of data we are going to store in our variable. 1. Predefined/Inbuilt: These data type are inbuilt. E.g.: char, int, float, double 2. Derived data type: These data types are inherited from predefined data type. E.g.: arrays, pointer. 3. User defined data type: These are created by user for his/her own purpose. E.g.: typedef, enum, structure, union
  • 7.
  • 8.
    Data Types andits Format
  • 9.
    Data Types andits Format
  • 10.
    Character Set  Characterset is the set of the character  This characters are used to form the word, numbers and expression.  The characters in the c are grouped into the following categories. Letters : Uppercase A…Z, Lowercase a…z Digits : All decimal digits 0 to 9 Special Character : ,(comma) .(period) ; (semicolon) :(colon), & (ampersand), # (number sign) etc. White spaces : Blank Space, Horizontal Space, New Line.
  • 11.
    Keywords  Are simplythe reserved words whose meaning has been explained in library of C language.  There are 32 keywords in c language.  We can’t use keywords as identifiers or variables because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer int float; char if; int void; Above will give ERROR as we are using keywords as identifier or variable name
  • 12.
  • 13.
    Constant  Constant namesare usually all in upper case to differentiate them from variable names. First method: A constant is declared like a variable, except that keywords const is added before the variable’s data type: const int DOZEN = 12; const double PI = 3.14; Second method: a constant is defined using the #define pre=processor. #define is a directive that cause the computer to replace the identifier with value it is to. example: #define PI 3.14 Identifier Value
  • 14.
    Backslash character constant C supports special backslash character constants that are used in output functions.  These character combinations are known as escape sequences.
  • 15.
    Comments  Comments arethat part of program, which are ignored by compiler. These are used for better readability of user. Compiler doesn‘t compile the text written inside comments.  The comment describes the purpose of the code in the file and might include some license or copyright information.  It is not compulsory to have comments in our program, but its good practice and what most C programmers will expect to find. 1. Single Line Comment : // is used to make single line comment. All the text in a line after // will become part of comment and will be ignored by compiler. E.g., int a; // this is ignored 2. Multiline Comment: This starts from /* and ends at */. All the text in between these will be part of comment. E.g., /* This Is Multiline Comment */
  • 16.
    Input/Output  Almost allC programs have at least one output statement. Otherwise, the program won’t output anything on the screen and there is no knowing if the program ran successfully or not.  The most common input/output functions are printf() and scanf() both of which are defined in the header file stdio.h.  Use printf() (Print with Format) for outputting data to the console and scanf() (Scan with Format) for inputting data from the keyboard.
  • 17.
    Input/Output [printf ()]  The syntax of the printf() function is  Format is the typesetting of the output that you can control, and argument is a list of variables to be printed.  The printf() function prints the value(s) of variables in argument to the standard output (screen) following the formatting command defined by format.
  • 18.
    Input/Output [scanf ()]  The scanf() is the inverse of printf(), i.e., it scans the value(s) of variable(s) from the standard input (keyboard) with format. The formatting part (i.e., % …) in scanf() is the same as printf(). However, the variable name must be preceded by an & (ampersand).
  • 19.
  • 20.
    Operators of C C supports a rich set of operators. Operators are used in programs to manipulate data and variables.  They usually form a part of the mathematical of logical expressions.  C operators are classified into several categories. They include: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Increment and Decrement operators
  • 21.
  • 22.
    Integer Arithmetic  Whenboth the operands in a single arithmetic expression are integers, the expression is called an integer expression , and the operation is called integer arithmetic.  During modular division, the sign of the result is always the sign of the first operand.  That is  -14 % 3 = -2  -14 % -3 = -2  14 % -3 = 2
  • 23.
    Real Arithmetic  Anarithmetic operation involving only real operands is called real arithmetic. If x and y are floats, then we will have: 1. x = 6.0 / 7.0 = 0.857143 2. y = 1.0 / 3.0 = 0.333333  The operator % cannot be used with real operands.
  • 24.
    Mix-Mode Arithmetic  Whenone of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression and its result is always a real number. Eg: 15 / 10.0 = 1.5
  • 25.
    Relational Operators  Therelational operators are mathematical operators often used in the if statement.  It is important to understand the difference between a single equal sign (=) and double equal signs (==) as the single equal sign (=) is used for an assignment while the double equal signs (==) are used as mathematical equality.
  • 26.
    Relational Operators 1. if(a==b) printf("a and b are equal.n"); else printf("a and b are not equal.n"); The statement above means that if the two variables, a and b, are the same, a string, “a and b are equal.”, is printed, otherwise a string, “a and b are not equal.”, is printed. One equal sign is for assignment and two equal signs are for logical equality. The double equal signs in C are equivalent to the equal sign in regular mathematical equations.
  • 27.
    Relational Operators 1. a= 10; a = a + 1; The statement a = a + 1 does not make sense as a mathematical expression. However, it makes perfect sense as a C statement. a + 1 is first evaluated to be 11 and this value of 11 is then assigned to a. Effectively, the value of a was incremented by 1.
  • 28.
    Logical Operators  Logicaloperators in C are mostly used within the if statement.
  • 29.
  • 30.
    Increment/Decrement/Substitution Operators  Chas two very useful operators that are not generally found in other languages.  These are the increment and decrement operator:  ++ and –  The operator ++ adds 1 to the operands while – subtracts 1. It takes the following form: ++m; or m++ --m; or m—
  • 31.
    Increment/Decrement/Substitution Operators Rules for++ & -- operators  These require variables as their operands.  When postfix either ++ or – is used with the variable in each expression, the expression is evaluated first and then it is incremented or decremented by one  When prefix either ++ or – is used with the variable in each expression, it is incremented or decremented by one first and then the expression is evaluated with the new value
  • 32.
  • 33.
    EXERCISE 1. Print acharacter, “a”. 2. Print an integer 10 3. Print floating number 10.5 4. Print two floating numbers, 10.0 and -2.3 5. Write a program that reads temperature in Celsius and convert it to Fahrenheit. Note 𝐶 = 𝐹 − 32 𝑋 5 9 Expected result

Editor's Notes

  • #2 NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.