Unit 1
Unit 1
Programming in C
Course Objectives
Course Outcomes
COs-POs
Syllabus-Unit 1
Syllabus-Unit 2
Syllabus-Unit 3
Syllabus-Unit 4
Books
Books
Computer systems
Computer hardware is the collection of physical
elements that comprise a computer system.
.
Computer systems
.
System Software
System Software: System software is responsible for
managing a variety of independent hardware
components, so that they can work together.
• Device drivers
• Operating systems
• Servers
• Utilities
• Window systems
.
System Software
.
Application Software
Application Software: Application software is developed to aid in any task that
benefits from computation. It is a broad category, and encompasses Software of
many kinds, including the internet browser being used to display this page. This
category includes:
• Business software
• Computer aided design
• Databases
• Decision making software
• Educational software
• Image editing
.
COMPUTER LANGUAGES
Machine Language.
Assembly Language
.
COMPUTER LANGUAGES
Assembly Language
.
COMPUTER LANGUAGES
.
Creating and Running Programs
.
Source Code → (Preprocessor) → Processed Source Code
Processed Source Code → (Compiler) → Object File
Object File → (Linker) → Executable File
Executable File → (Loader) → Running Program
Creating and Running Programs
.
Creating and Running Programs
.
Creating and Running Programs
.
Creating and Running Programs
.
C Program
1. #include <stdio.h>
2. void main(){
3. printf("Hello C Language");
4. }
#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
.
C Program
void main() The main() function is the entry point of every program in c language. The
void keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it
blocks the screen.
.
C TOKENS
In C programming, a token is the smallest unit of meaningful code that the compiler recognizes and
processes.
1: Identifiers
2: Keywords
3: Constants
4: Strings
5: Special Symbols
6: Operators
.
Types of Token in C
.
Types of Token in C
.
Example:
#include <stdio.h> // Token: Preprocessor directive
int main() { // Token: Keyword (int), Identifier (main), Punctuation (())
int x = 5; // Token: Keyword (int), Identifier (x), Operator (=), Constant (5), Punctuation (;)
printf("Value: %d", x); // Token: Identifier (printf), String literal ("Value: %d"), Operator (,), Identifier (x), Punctuation (;)
return 0; // Token: Keyword (return), Constant (0), Punctuation (;)
}
.
Identifiers:
Identifiers refer to the names of variables, constants, functions and arrays. These are user-defined names is called
Identifiers. These identifier are defined against a set of rules.
1. An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore( _ ).
2. The first character of an identifier can only contain alphabet( a-z , A-Z ) or underscore ( _ ).
3. Identifiers are also case sensitive in C. For example name and Name are two different identifier in C.
Ex : Valid Invalid
STDNAME return
SUB $stay
TOT_MARKS 1RECORD
Y2K
Keywords
A keyword is a reserved word. All keywords have fixed meaning that means we
cannot change. Keywords serve as basic building blocks for program statements. All
keywords must be written in lowercase.
Example auto, break, case, char, const, continue, default etc.
.
Data Types/Types
To store data the program must reserve space which is done using datatype. A datatype is a
keyword/predefined instruction used for allocating memory for data. A data type specifies the type of data
that a variable can store such as integer, floating, character etc. It used for declaring/defining variables or
functions of different types before to use in a program. There are 3 types of data types in C language.
.
3. Enumeration Data
Data Types/Types
#include <stdio.h>
Basic or Primary Data Type- int, char, float, double, void int main() {
Derived Data Type- array, pointer, structure, union int number1, number2, sum;
.
Primary Data Types
Datatype size in bytes
char = 1
unsigned char = 1
int = 4
long int = 8
short int = 2
float = 4
double = 8
long double = 16
.
Enumerated Data Type
Constants refer to fixed values that do not change during the execution of a program.
Note: constants are also called literals.
.
TYPES OF C CONSTANT
1. Integer constants
2. Real or Floating point constants
3. Character constants
4. String constants
5. Backslash character constants
.
TYPES OF C CONSTANT
Integer constants:
An integer constant is a numeric constant (associated with number) without any
fractional or exponential part. There are three types of integer constants in C
programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For
. example:Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
TYPES OF C CONSTANT
Floating point/Real constants:
A floating point constant is a numeric constant that has either a fractional form or an
exponent form. For example: -2.0
0.0000234
-0.22E-5
.
TYPES OF C CONSTANT
Character Constant:
Single Character Constant : A character constant is either a single alphabet, a single
digit, a
single special symbol enclosed within single inverted commas.
a) it is value represent in „ „ (single quote).
b) The maximam length of a character constant can be 1 character.
EX : VALID INVALID
. ‘a’ “12”
Escape characters or backslash
characters:
a) \n newline j) \? Question mark (?)
c) \t tab
d) \v vertical tab
e) \b backspace
f) \f form feed (page feed)
g) \a alert (beep)
.
h) \‟ single quote(„)
i) \” double quote(“)
Two ways to define constant in C
1. const keyword
2. #define preprocessor
C const keyword
1. #include <stdio.h>
2. #include <conio.h>
3. void main(){
.
4. const float PI=3.14;
5. clrscr();
8. }
Output:
2) C #define preprocessor
C#define
The #define preprocessor directive is used to define constant or micro substitution. It can use any
1. #define PI 3.14
2. main() {
3. printf("%f",PI);
4. }
Output:
3.140000
.
OPERATORS AND EXPRESSIONS:
Operators : An operator is a Symbol that performs an operation. An operators acts some variables are
called operands to get the desired result.Ex : a+b; Where a,b are operands and + is the operator.Types
of Operator :
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
6) Conditional Operators.
.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction and
.
Relational Operators
#include <stdio.h>
Output
int main() 5 == 5 = 1
{int a = 5, b = 5, c = 10; 5 == 10 = 0
printf("%d == %d = %d \n", a, b, a == b); // true 5>5=0
printf("%d == %d = %d \n", a, c, a == c); // false 5 > 10 = 0
printf("%d > %d = %d \n", a, b, a > b); //false 5<5=0
printf("%d > %d = %d \n", a, c, a > c); //false 5 < 10 = 1
printf("%d < %d = %d \n", a, b, a < b); //false
5 != 5 = 0
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false 5 != 10 = 1
printf("%d != %d = %d \n", a, c, a != c); //true 5 >= 5 = 1
.
printf("%d >= %d = %d \n", a, b, a >= b); //true 5 >= 10 = 0
printf("%d >= %d = %d \n", a, c, a >= c); //false 5 <= 5 = 1
printf("%d <= %d = %d \n", a, b, a <= b); //true 5 <= 10 = 1
printf("%d <= %d = %d \n", a, c, a <= c); //true
Logical Operators.
These operators are used to combine the results of two or more conditions. An
expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making
in C programming
.
Logical Operators.
.
Logical Operators.
#include <stdio.h>
int main() Output
{
int a = 5, b = 5, c = 10, result; (a == b) && (c > b) equals to 1
result = (a == b) && (c > b); (a == b) && (c < b) equals to 0
printf("(a == b) && (c > b) equals to %d \n", result); (a == b) || (c < b) equals to 1
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result); (a != b) || (c < b) equals to 0
result = (a == b) || (c < b); !(a != b) equals to 1
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
!(a == b) equals to 0
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result .= !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
Assignment Operators
Assignment operators are used to assign a value (or) an expression (or) a value of a variable to another
variable.
.
Assignment Operators
#include <stdio.h>
int main() Output
{
int a = 5, c; c=5
c = a; c = 10
printf("c = %d \n", c); c=5
c += a; // c = c+a
printf("c = %d \n", c); c = 25
c -= a; // c = c-a c=5
printf("c = %d \n", c);
c *= a; // c = c*a
c=0
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a;. // c = c%a
printf("c = %d \n", c);
return 0;
}
Increment and Decrement Operators /Unary
Operators:
Unary operators are having higher priority than the other #include <stdio.h>
operators. Unary operators, meaning they only operate
int main()
on a single operand.
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output
++a = 11
. --b = 99
++c = 11.500000
++d = 99.500000
Increment and Decrement Operators /Unary
Operators:
#include<stdio.h> #include<stdio.h>
void main() {
#include<conio.h>
int i = 1;
printf("%d %d %d", i, ++i, i++);
void main() {
} int i = 0, j = 0;
Output : 3 3 1 j = i++ + ++i;
printf("%d\n", i);
printf("%d\n", j);
}
Output :
2
2
.
Conditional Operator/ Ternary operator:
.
Bitwise Operators:
.
Bitwise Operators:
.
Bitwise AND operator &
Let us suppose the bitwise AND operation of two Example #1: Bitwise AND
integers 12 and 25.
12 = 00001100 (In Binary)
#include <stdio.h>
25 = 00011001 (In Binary) int main()
{
Bit Operation of 12 and 25
00001100 int a = 12, b = 25;
& 00011001 printf("Output = %d", a&b);
________
00001000 = 8 (In decimal)
return 0;
}
Output
Output =8
.
Bitwise OR operator |
.
Bitwise XOR (exclusive OR) operator ^
.
Bitwise complement operator ~
.
Bitwise complement operator ~
#include <stdio.h>
int main()
{
printf("complement = %d\n",~35);
printf("complement = %d\n",~-12);
return 0;
}
Output
Complement = -36
Complement = 11
.
Bitwise shift operators
#include <stdio.h>
int main() Output
{ x = 107, z = 108.000000
int x = 10; // integer x
char y = 'a'; // character c
return 0;
}
Output
sum = 2