You are on page 1of 35

C

Computer
Processor
Input Processing Output
Figure 1: Basic operation of a computer

Central Processing Unit (CPU)

Control Unit

Input Output
Arithmetic-Logic Unit
Device Device

Memory Unit

Figure 2: The von Neumann architecture


Levels of Programming Language
 High level
 C
 C++
 Java
 Mid level
 Assembly
 Machine code
 Code that machine understands

High level Assembly


load b
load c
a=b+c
add b,c
store a

Figure 3: High level and assembly equivalent codes


Terms Related to Programming Language
 Unstructured
 FORTRAN
 BASIC
 Structured
 C Total task
 Procedural
 C
 C++ Task 1 Task 2 Task 3
 Java
 Modular
 C
 C++ Task 2.1 Task 2.2
 Java
Figure 4: Dividing a task into smaller tasks
 Object oriented
 C++
 Java
 C is not object oriented
 
Computer Memory
 Combination of 0s and 1s
 1 bit space is required to store a 0 or a 1
 8 bits=1 byte (B)
 bytes = 1KB
 1024 KB = 1MB
 1024 MB = 1GB
 1024 GB = 1TB
 Text, numeric quantity require varied number of memory depending on their type
 Instructions can be of 1, 2, 3, … bytes

7 1 0

Address
00FC

Figure 5: Memory representation


First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
printf("BBBBB"); //statement to display BBBBB
printf("\nCCCCC"); //statement to display CCCCC
/*
printf("\nDDDDD");
*/
return 0; //return value of the main function
}
First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
printf("BBBBB"); //statement to display BBBBB
printf("\nCCCCC"); //statement to display CCCCC

printf("\nDDDDD");

return 0; //return value of the main function


}
First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
printf("\nCCCCC"); //statement to display CCCCC
printf("BBBBB"); //statement to display BBBBB
printf("\nDDDDD");

return 0; //return value of the main function


}
First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
printf("\nCCCCC"); //statement to display CCCCC
printf("BBBbb"); //statement to display BBBbb
printf("\nDDDDD");

return 0; //return value of the main function


}
First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
printf("\nCCCCC"); //statement to display CCCCC
printf("BBB bb"); //statement to display BBB bb
printf("\nDDDDD");

return 0; //return value of the main function


}
First Program
#include<stdio.h> //header file---standard input output
int main() /*main function*/
{
printf("AAAAA"); //statement to display AAAAA
prinTf("\nCCCCC"); //statement to display CCCCC
printf("BBB bb"); //statement to display BBB bb
printf("\nDDDDD");

return 0; //return value of the main function


}
Basic Parts of a C Program
 Preprocessor commands
 Tells a computer to do something before going to actual compilation
 Functions
 Variables
 Statements & expressions
 Comments

 Preprocessor commands #include<stdio.h>


 Functions int main(){}
 Variables
 Statements & expressions printf(“AAAAA”);
 Comments //statement to display AAAAA
Tokens
 Smallest element of a program which is meaningful to compiler

Figure 6: Tokens in C
Tokens
 Keywords
 Reserved words
 Special meaning to compiler
 Specific, predefined task for each keyword
 Can not be used anywhere else for any other task

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Tokens
 Operators
 +,-,*,/,%,<,>,……
 x=(a+b)*(((a+c)+(c+d))/(g%f))
 Strings
 Sequence of characters
 Enclosed in double quotes
 “17 series of RUET”
 Constants
 Fixed values which can not be modified
 Special characters
 Symbols having special meaning
 () {} [] , ; * = #
 Identifiers
 Name used to identify variable, function, array or any other user defined item
 
Variable
 Memory is needed to store some value
 Tough to remember any specific memory address
 Memory address is given a name
 Easy to remember that name

int a=123;

1002 1003 Address


0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 Content
a Name given to address
Figure 7: Storing a value using a variable in any memory address
Variable Declaration
 Mandatory to declare a variable before using
 Informing the compiler about the type of data that will be used with a variable
dataType variableName;
int a;
1002 1003 Address
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Content
a Name given to address

dataType variableName=value;
int a=123;
1002 1003 Address
0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 Content
a Name given to address
Variable Declaration
 variableName
 First letter can not be a number
• 1a, 5variables
• a1, variables5
 No special character is allowed [Only underscore (_), dollar sign ($) allowed]
• @cse, ball#er, ball-er
• _cse, ball_er
• $cse, ball$er
 No space is allowed within a variable name
• variables 5, ball er
• variables_5
 No keyword can be used
• auto, union
• Auto, Union
Variable Declaration
 Same variableName can not be used multiple times in a program
int a1;
int variables5;
char a1;
 variableName(s) of same type can be declared in a single line or in separate lines
int a1, variables5;
---------------------
int a1=123, variables5=231;
----------------------------------
int a1=variables5=231;
Data Types in C
 char (character)
 Letters, single character
 ‘a’, ‘b’, …
 int (integer)
 Whole numbers
 No decimal point
 123, 321
 float (floating point)
 Numbers with decimal place
 123.2541, 321.15976, …
 double (double precision floating point)
 Floating point numbers with more significant figures
 123.254185637104
Data Types in C
 char (character) 1002
 8 bits 0 0 0 0 0 0 0 0
 1 Byte
 int (integer)
1002 1003
 16 bits 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 2 Bytes
 float (floating point)
 32 bits
 4 Bytes
 double (double precision floating point)
 64 bits
 8 Bytes

1002 1003 1004 1005


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Data Types in C
Escape Sequences in C
Character Escape Sequence
Bell \a
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t
Vertical tab \v
Quotation \”
mark
Apostrophe \’
Question \?
Backslash \
Null \0
 
Data Types in C
 char (character)
 1st bit as the sign bit
 Rest of the 7 bits to store data
1002
S 0 0 0 0 0 0 0

 Range

 char example=‘G’;
 
Data Types in C
 ASCII (American Standard Code for Information Interchange)
 Character code
 char example=‘G’;

1002 Address
S 1 0 0 0 1 1 1 Content
example Name given to address
 
Data Types in C
 int (integer)
 1st bit as the sign bit
 Rest of the 15 bits to store data
1002 1003
S 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 Range


 
Data Types in C
 float (floating point)
 1st bit as the sign bit
 Rest of the 31 (8 (exponent)+23 (mantissa)) bits to store data
 6 decimal places (123.253614)

 (Normalizing)
 127+3=130 (Adding integer bias)
 (Conversion)
 0 10000010 10110100000000000000000 (Sign Exponent Mantissa)
 00000000 00000000 01011010 1000001 (Reverse)

 double (double precision floating point)


 1st bit as the sign bit
 Rest of the 63 (11 (exponent)+52 (mantissa)) bits to store data
 15 decimal places (123.253614777788888)
Data Types in C
 Modifier
 Changes the range/ capacity of basic data types
 modifier dataType variableName;
 char
 signed char example;
 unsigned char example;

1002
S 0 0 0 0 0 0 0

1002
0 0 0 0 0 0 0 0
 
Data Types in C
 int
 signed int a; // same functionality as int
 unsigned int a;
 short int a; // same functionality as int
 long int a; // 4 bytes so
 unsigned short int a;
 unsigned long int a; // 4 bytes without sign bit so
 All of these could have been written without writing int. So both of the following are same
signed int a;
signed a;
1002 1003
S 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1002 1003
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Data Types in C
 float
 No modifier for float
 double
 long double a; // 10 bytes or 12 bytes
printf()
 To display output on the screen

Format Specifier Use Format Specifier Use


%c Single character %o Octal string
%d Integer %s Null terminated
%e Floating point character string
number in e %u Unsigned integer
%E Floating point %x Hexadecimal
number in E integer (a…f)
%f Floating point %X Hexadecimal
number integer (A…F)
printf()
printf()
printf()
scanf()
 To take input from the user
 Using &

You might also like