You are on page 1of 15

Programming in C and C++

Dr. M. Babul Islam


Dept. of Applied Physics and Electronic Engineering
Rajshahi University
Digital Computer

Keyboard, Mouse, Mic, etc. Monitor, Printer, Speaker, etc.

Input CPU Output


Device (ALU & CU) Device

Main
Memory
Computer Programming
Language

Low-level language High-level language


• Machine language • Closer to programmer
• Expressed in binary number • Examples: Fortran, C, C++, Java,
• Closer to machine etc.
• Not portable, i.e., machine dependent • Requires a translator program
• Difficult to write programs interpreter or compiler
• Assembly language • Portable, i.e., machine independent
• Use English like words called mnemonics
instead of binary number
• Closer to machine
• Machine dependent
• Easier than machine language
• Required a translator program called
assembler
Assembly Machine
language Assembler language
program program

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 Constants Strings Operators


float, while 12.2, 13 “ABC”, “roll” +, -, *

Identifiers Special Symbols


main, sum [ ], { }

• Every C word is classified as either a keyword or an identifier.


Keywords versus Identifiers

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

Integer Real (floating Single String


point) character
constants constants constants constants
12, 32 3.2, 5.9 ‘a’, ‘x’ “sam”, “a”
Variables:
• A variable is a data name that may be used to store a data value.
Unlike
constants, a variable may take different values during the execution.
• Valid variable names:
student total marks x
sub_total a1 temp Area

• Invalid variable names:


123 1st (area)
%x sub total x&
Data Types
C supports three classes of data types:
1. Primary or fundamental data types:
integer (int), character (char), floating point (float), double-precision
floating point (double) and void
2. Derived data types:
array, function, structure and pointer
3. User-defined data types
Size of data types on a 16-bit machine and their keywords
Type Keywords Size (bits)
character or signed character char 8
unsigned character unsigned char 8
integer or signed integer signed int or int 16
unsigned integer unsigned int or unsigned 16
short integer or signed short int or short int 8
signed short integer or short
unsigned short integer unsigned short int 8
or unsigned short
long integer or signed long int or 32
signed long integer long int or long
unsigned long integer unsigned long int 32
or unsigned long
floating point float 32
double-precision floating point double 64
long double long double 80
Basic Structure of a C Program

• 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;

Example: int x, count;


float avg, z;
char a;

• Assign Values to Variables:


Assignment statement
variable name = value;
Example: count = 0;
Assignment operator
• Reading Data from Keyboard:
scanf (“control string”, &variable1, &variable2, . . .);
Example-1:
int x;
scanf (“%d”, &x);
Example-2:
float r;
scanf (“%f”, &r);
Example-3:
int a, b;
float x;
scanf (“%d %d %f ”, &a, &b, &x);
• Printing output/data/message on the screen:
printf (“control string”, variable1, variable2, . . .);
Example-1:
int x = 5;
printf (“x = %d”, x);
Example-2:
float r;
printf (“%f”, r);
Example-3:
printf (“This is my first program!”);

You might also like