Chapter 4
Fundamentals of C
• Keywords
• Identifiers
• Variables
• Constants – character, integer, float, string, escape sequences
• Data types – built-in and user defined(enumerated)
• Operators and Expressions Operator types (arithmetic, relational,
logical, assignment, bitwise, conditional and other operators),
precedence and associativity rules
• Input and Output – character input and output, formatted input and
output
C Character Set
• Consists of upper and lower case alphabets, digits and special symbols
• Alphabets: A to Z
a to z
• Digits: 0 to 9
• Special Symbols: , . : #, ! @ % ^ & * ( ) _ + - / { } [ ] < > ? \ |
C Tokens
• Smallest unit of a program that has some meaning is called a
token
C Tokens
Keywords Identifiers Constants Operators
Keywords
• Keywords are the reserved words
• They have their pre-defined functionality
• Cannot be used by programmer in any other way
• C language supports 32 keywords
Identifiers
• All the words in a program other than the keywords are called Identifiers
•They are user-defined names given to variables, functions and constants
Rules for using an Identifier:
1. Identifier name must contain alphabets or digits and must begin with an
alphabet or an underscore.
2. No other symbol other than the underscore is allowed
3. Keywords cannot be used as identifiers
4. C is case-sensitive. Therefore uppercase and lowercase letters are treated
differently
5. The length of the identifiers should not be more than 31 characters.
6. Identifier names should be short and meaningful
Example: num, roll_no, value1, VAL
Variables
• All data is stored in computer’s memory
Byte 1 Byte 2 Byte 3 Byte 4
1001 1002 1003 1004 1005 1006
Byte N
• We can provide names to the memory location where we are storing the
data
• Giving name to the location makes it easier to access the data
• The name given to the meCSm
C1o
10r1y
(Balso
iccPa
rotgiro
amnmiinsg cusainlglCed a Variable
Variables
• Variable is an identifier assigned to the memory location where the data is
stored.
• Example: a = 8
Byte Byte Byte Byte
1 2 3 4
1001 1002 1003 1004 1005 1006
8
Nth
byte
a variable name
8 variable value
CSC11100015(BasicProgrammminegmuosrinyglCocation or memory address
Declaring Variables
• All variables need to declared at the beginning of the program
• Declaring a variable tells the compiler the names of the variables to be used in
the program
• It also specifies what type of data the variable will hold
Syntax: datatype variable1, variable2, ...., variable n ;
Example: int a, b ;
float area ;
char ch;
Initializing Variables
• Assigning values to variables is called initialization
Example: int a = 8 ;
int sum = 0, product = 1 ;
Constants
• Constants are the fixed values directly used in the program.
• There are various constants like:
• Integer Constant
• Floating Constant
• Character Constant
• String Literal
Integer Constants
• Integer constants are the whole numbers
• It can be a positive or negative integer
Example: 1234, -543
Floating point Constants
•They are the real numbers with decimal point
Example: 952.89, -3.5, 3.142
Character Constants
• They are any single character from the C character set
• They are enclosed in single quotes
• Every character constant is associated with a numeric value called ASCII value
• ASCII stands for American Standard Code for Information Interchange
• Starts from ASCII value 0 to 127
ASCII values
• ASCII stands for American Standard Code for Information
Interchange
• Starts from ASCII value 0 to 127
0 – 9 48 – 57
A- Z 65 – 90
a – z 97 – 122
Data Types in C
• Data types specifies the type and size of the data to be stored
Data Types
Simple Data Types Structured / Derived Data Types
array
User-defined structure
Standard/ Basic/
union
Fundamental
enum
int
char
float
double
void
Data type Use Size Range
int for an integer number 2 bytes (16-bit machine) -32768 to 32767
4 bytes (32 bit machine) -2,147,483,648 to
2,147,483,648
char a single character 1 byte -128 to 127
float floating point numbers 4 bytes
double floating point numbers 8 bytes
void empty data type 0 byte
Operators
• Operator is a symbol that performs some operation
Operators
Unary Binary Ternary
Unary minus - Arithmetic
Conditional
Increment ++ Relational
Decrement -- Logical
sizeof( ) Assignment
Bitwise
Increment and Decrement Operators
• These operators are use to increase or decrease value of a
variable by one.
• Syntax: variable_name++ ; /* Post increment */
++variable_name ; /* Pre increment */
variable_name-- /* Post decrement */
--variable_name /* Pre decrement */
Example:
a = 5;
a++ ; /* After incrementing Value of a = 6 */
a++ ; /* After incrementing Value of a = 7 */
a --; /* After decrementing Value of a = 6 */
Arithmetic Operators
• C provides all the basic arithmetic operators
Operator Use Example
+ Adding two operands a+b
- Subtracting two operands a–b
* Multiplication a*b
/ Division a/b
% Modulo Division a%b
Relational Operators
• These operators are used to compare the values of two
variables.
Operator Use Example
< Less than a<b
<= Less than or equal to a <= b
> Greater than a>b
>= Greater than or equal to a >= b
== is Equal to a == b
!= Not equal to a != b
Logical Operators
• Logical Operators are used to test more than one condition and
make some decision
Operator Use Example
&& Logical AND For a = 3, b = 2
It returns true when all conditions if a > b && a != 0 returns
are true TRUE
|| Logical OR if a > b || a!=0 returns true
It returns true when atleast one
condition is true if a < b || a == 0 returns false
! Logical NOT if !(a == 0) is true
Assignment Operators
• Assignment Operators are used to assign values to variables
• There are 2 types of assignment operators in C language:
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=,
%=, &=, ^= )
Operator Example
= a=4
+= a+=5; Equivalent to a = a + 5;
-= a-=5; Equivalent to a = a - 5;
*= a*=5; Equivalent to a = a * 5;
/= a/=5; Equivalent to a = a / 5;
%= a%=5; Equivalent to a = a % 5;
Bitwise Operators
• Used to manipulate data at bit-level
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right Shift
Bitwise Operators
a b a&b a b a|b a b a^b
0 0 0 0 0 0 0 0 0
0 1 0 0 1 1 0 1 1
1 0 0 1 0 1 1 0 1
1 1 1 1 1 1 1 1 0
Left Shift (<<) Right Shift (>>)
a 1110 0000 0000 1101 a 1110 0000 0000 1101
a << 3 0000 0000 0110 10 0 0 a >> 3 0001 1100 0000 0001
• The leftmost three bits are shifted out • The rightmost three bits will be shifted out
• Trailing empty space filled with zeros • Leading empty spaces are filled with zeros
Conditional Operators
• Conditional operator returns one value if condition is true and returns another
value is condition is false.
•It is also called as ternary operator.
Syntax: Condition ? value1 : value2
If the condition is true, the expression returns vaule1 otherwise it returns value2
Example:
a = 5;
b = 10;
max = a > b ? a : b;
Input and Output Functions:
• All input and output in C is done through streams.
• Stream is a sequence of bytes of data
• Stream flowing into program is an input stream and stream flowing out of
program is output stream
Input and Output Functions
Character Input and Output String Input and Output Formatted Input and Output
getchar() putchar() gets() puts() scanf() printf()
getche()
getch()
Input and Output Functions
scanf() :
• This is a input function defined in stdio.h
• Used to input data of any specific type.
Syntax: scanf(“Format String”, address_of_variable1, address_of_variable2......);
format string: specifies which type of data to be entered
Format String Meaning
%d Specifies data is of integer type
%f Specifies data is of float type
%c Specifies data is of character type
%s Specifies data is a string
address_of_variable1 : specifies the memory location where the data is to be
stored named by the variable name
int a;
char ch; a ch
float perc;
scanf(“%d”, &a); 2005
scanf(“%c”, &ch);
scanf(“%f”, &perc); perc
1005
Input and Output Functions
printf() :
• This is a output function defined in stdio.h a b
•Used to print the data of any specific type.
Syntax:
printf(“Format String”, variable1, variable2......);
5 3
format string: specifies which type of data to be entered
variables: name of variables whose value is to be printed
Example: sum
int a = 5, b = 3, sum = 0;
sum = a + b; 8
printf(“ Addition = %d”, sum);
C Escape Sequence
• A sequence of characters starting with backslash ( \ ) that performs a specified function,
which is already defined by C, is called as an escape sequence in C.
• It is named so as it doesn’t represent itself as a character, when it is used inside a string.
• Escape sequences in C are:
Alarm or It is used to generate a bell sound in Horizontal It inserts some whitespace to the left of the
\a \t
Beep the C program. Tab cursor and moves the cursor accordingly.
It is used to move the cursor \v Vertical Tab It is used to insert vertical space.
\b Backspace
backward.
\\ Backlash Use to insert backslash character.
It is used to move the cursor to the \’ Single Quote It is used to display a single quotation mark.
\f Form Feed
start of the next logical page.
Double
\” It is used to display double quotation marks.
It moves the cursor to the start of the Quote
\n New Line
next line.
Question
\? It is used to double quotation marks.
Carriage It moves the cursor to the start of the Mark
\r
Return current line.
\0 NULL It represents the NULL character.
Sample Programs:
1. Write a program to add two numbers
#include <stdio.h>
int main()
{
int a, b, sum; // Decalre variables
printf("\n Enter value of a: ");
scanf("%d“, &a); // Accept input from user
printf("\n Enter value of b: ");
scanf("%d“, &b); // Accept input from user
sum = a + b;
printf("\n Sum = %d",sum); // Print the output
}
Output:
Enter value of a: 5
Enter value of b: 3
Sum = 8
Sample Programs:
2. Write a program to accept a character and print the next character
#include <stdio.h>
int main()
{ Output:
char ch; Enter the character: H
printf("\n Enter the character: ");
scanf("%c",&ch); Next character = I
printf("\n Next character = %c",++ch);
}
Sample Programs:
3. Write a program to calculate area of a circle
#include <stdio.h>
int main()
{ Output:
float radius, area; Enter the radius: 9
printf("\n Enter the radius: ");
scanf("%f",&radius); Area of circile = 254.501999
area = 3.142 * radius * radius;
printf("\n Area of circile = %f",area);
}
References
• Programming in ANSI C By Balagurusamy
• www.javatpoint.com