C Programming
C Programming
C Programming
Main
Memory
Computer Programming
Language
High-level
language Interpreter/ Executable code
Object code Linker
program Compiler (machine code)
Library
C Tokens
• In a C program the smallest individual units are known as C tokens.
C Tokens
Keywords:
• All keywords have their fixed meanings and these meanings cannot
be changed.
• All keywords must be written in lowercase.
Identifiers:
• Identifiers refer to the names of variables, functions and arrays.
• Both uppercase and lowercase letters are permitted.
• First character must be an alphabet or underscore ( _ ).
• Must consist of only letters, digits or underscore.
• Cannot use a keyword.
• Must not contain white space.
Constants versus Variables
Constants:
• In C constants refer to fixed values that do not change during the
execution of a program.
Constants
Numeric Character
constants constants
• Documentation Section
• Link Section
• Definition Section
• Global Declaration Section
• main ( ) Function Section
{
declaration part
executable part
}
• Subprogram Section
• Declaration of Variables:
data-type variable names;
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division or remainder division
Ex: 13 % 3 = 1, 9 % 10 = 9
Relational Operators
Operator Meaning
< is less than
Ex: 4.5 < 4 False
12 < 19 True
<= is less than or equal to
Ex: 7 <= 6 False
12 <= 13 True
> is greater than
>= is greater than or equal to
== is equal to
Ex: a == b True (if a and b equals)
!= is not equal to
Ex: a != b True (if a is not equal to b)
Logical Operators
Operator Meaning
&& logical AND (a && b)
|| logical OR (a || b)
! Logical NOT (!a)
Assignment Operators
• ++
++m; or m++ ; (Equivalent to m = m+1)
• --
--m; or m-- ; (Equivalent to m = m-1)
• m = 5;
y = ++m;
In this case y = 6 and m = 6.
• m = 5;
y = m++;
True
False
Ex:
a = 10;
b = 15;
x = (a > b) ? a : b;
So, x will be 15.
Bitwise Operators
Operator Meaning
& bitwise AND (a & b)
| bitwise OR (a | b)
^ bitwise ex-OR (a^b)
<< shift left
>> shift right
Special Operators
• Decision-making statements
1. if statement
2. switch statement
3. Conditional operator (?:) statement
4. goto statement
False
Test expression
True
label:
.....
.....
goto label;
statement-x;
• Loop operations:
1. The while statement
2. The do-while statement
3. The for statement
while Statement
struct book_bank
{
char title[32];
char author[32];
int pages;
float price;
};
struct book_bank book1, book2, book3;
Accessing structure members:
Under a structure the members themselves are not variables, they should be
linked to the structure variables in order to make meaningful members.
The link between a member and a variable is established using the member
operator ‘.’ which is also known as ‘dot-operator’ or ‘period operator’.
Example:
book1.price = 120.50;
book1.pages = 250;
scanf(“%s\n”, book1.title);
scanf(“%d\n”, &book1.pages);
Arrays of structures:
struct book_bank
{ book[0].price = 120;
char title[32]; book[1].price = 220;
char author[32];
float price;
}struct book_bank book[10];
Arrays within structures:
struct book_bank
{ book[0].price[0] = 120;
char title[32]; book[0].price[1] = 220;
char author[32];
float price[2];
}struct book_bank book[10];
String Operation
Declaring and Initializing String Variables:
• C does not support strings as a data type
• C allows to represent strings as character arrays
Function Purpose
strcat () To concatenate two strings.
strcmp () To compare two strings.
strcpy () To copy one string over another.
strlen () To find the length of a string.
File Management in C
Basic file operations:
• Naming a file
• Opening a file
• Reading data from a file
• Writing data to a file
• Closing a file
File handling functions (High level I/O functions):