Module No.
- 2
Fundamentals Of C-Programming
Structure Of C-Programming -
• C is a general-purpose, high-level programming language that was originally developed
by Dennis Ritchie in 1972 at Bell Labs.
• Program Structure in C : A basic C program consists of functions and statements, with
the main() function being the entry point.
• #include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Key Components of C-Program :
• (#include<stdio.h>) – Includes external files like libraries (in this case , stdio.h for
input/output functions.
• Main Function – (int main()) – The entry point of every C – Program. The program starts
execution from here. The int before main specifies that the function will return an
integer value, usually 0 to indicate successful execution.
• Statements : inside the main function , statements are written , such as variable
declarations, function calls, and other operations.
Execution Of a Program
1. C program (source code) is sent to preprocessor first. The preprocessor is responsible
to convert preprocessor directives into their respective values. The preprocessor
generates an expanded source code.
2. Compilation – the source code is converted into object code by the compiler.
3. Linking – All object files and libraries are combined to create the final executable.
4. Execution – the resulting program can be run on the machine.
Programming Error in C -
• There are mainly three types of errors in C Programming :
1. Syntax Errors : Mistakes in the code structure , such as missing semicolons, wrong
function calls, or incorrect operators.
• E.g.- printf(“Hello world”) // missing semicolon
2. Runtime Errors : Errors that occur during execution , such as division by zero, or
accessing invalid memory.
• E.g. - int a = 10, b = 0;
printf("%d", a / b); // Division by zero.
2. Logical Errors : These occur when the program runs successfully but does not
produce the correct output due to a flaw in the algorithm or logic.
E.g. - int a = 5, b = 3;
printf("%d", a - b); // Incorrect logic might give wrong results.
• Character Set in C –
• The C programming language uses a character set that includes :
• Letters : a to z , A to Z
• Digits : 0 to 9
• Special Characters : !,*,@,#,& etc.
• White spaces – Spaces, tabs and newlines.
• Escape Characters - \n, \t etc.
Identifiers & Keywords -
1. Identifiers : Names used to identify variables , functions , arrays, etc. They must start with
a letter (Uppercase or Lowercase) or an underscore, followed by letters , digits or
underscores. Identifiers are case-sensitive. E.g. int num;
2. Keywords : Reserved words in C that have special meanings and cannot be used as
identifiers.
• E.g. – int , char , if , else , return , while , for etc.
Tokens –
1. Tokens : The smallest units in a program. C programs are made up of tokens, such as :
• Keywords – int , for, char etc.
• Identifiers – e.g. main , x
• Constants – e.g. 3.14, ‘a’ , 10
• Operators – e.g. : + , - , * , %, / , && etc.
• Punctuation : e.g. - ; , {, } etc.
Data Types in C :
• C has several basic data types, each used to store different kinds of data. Some common data
types are: int: Integer type, used for whole numbers.
• float: Single-precision floating-point type, used for decimal numbers.
• double: Double-precision floating-point type, used for more accurate decimal numbers.
• char: Character type, used for single characters
Additionally , C allows modifiers like short , long , signed, unsigned to modify the basic data
types :
• Short int – A smaller range of integers
• Long int – A larger range of integers
Enumerated Data Type and It’s Size :
• An enumerated data type (enum) is user-defined type that consists of a set of named
integer constants. E.g. - enum day {Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday};
• Literals , Constants and Variables :
• Literals : Fixed values usec in the program.
• E.g. Integer literal = 100 , floating-point literal = 3.14 , Character Literal : ‘a’ , String
Literal : ‘Hello’
• Constants : Values that cannot change during the execution of the program. Constants
can be defined using the ‘const’ keyword or #define.
• E.g. Const pi=3.14; // define pi value
• Variables: Named storage locations used to hold data that can change during the
program’s execution. e.g. - int num = 10;
float rate = 4.5;
• Input and Output in C -
• C uses standard input and output functions for reading and writing data.
• Printf() - Used for output (printing data to the screen)
• Scanf() – Used for input (reading data from the user)
• Comments in C
• Comments are used to explain parts of the code. C supports two types of comments :
• Single-line Comment - // is used for comments that only span one line.
• Multi-Line Comment - /* comment */ is used for longer comments that span multiple lines.
• Control Structures in C : Control structures are used to control the flow of execution
in the progra,. The primary control structures are :
• Conditional statements – if , else , else-if and switch for decision-making.
• Loops – For , while , do-while
e.g. –
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
• Operators in C :
• Operators in C are special symbols that perform operations on variables and values. They help in
performing mathematical computations, logical operations, bit manipulations, and data handling.
• For example, in a + b, the + is an operator that adds a and b.
• Types Of Operators In C Language –
1) Arithmetic Operators
2) Relational(Comparison) Operators
3) Logical Operators
4) Bitwise Operators
5) Assignment Operators
6) Increment/Decrement Oeprator
7) Ternary (Conditional) Operator
1) Arithmetic Operator :
• These Operators perform basic mathematical operations.
Operator Symbol Description Example(a=1 Result
0,b=5)
Addition + Add Two a+b 15
Numbers
Subtraction - Subtracts Two a-b 5
numbers
Multiplication * Multiplies two a*b 50
numbers
Division / Divides (return a/b 2
quotient)
Modulus % Returns a%b 0
Remainder
• Example :
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
1) Relational (Comparison) Operators :
• Used to compare values and return a Boolean result ( 1 for true , 0 for true)
Operator Symbol Description Example(a=1 Result
0,b=5)
Equal to == Checks if two a==b 0
values are equal
or not
Not Equal to != Checks if two a!=b 1
values are not
equal
Greater Than > Checks if left a>b 1
operand is
greater
Less Than < Checks if left a<b 0
operand is
smaller
Greater than >= Checks if left a>=b 1
equal to operand is
greater or equal
Less than equal <= Checks if left a<=b 0
1) Logical Operators :
• Used for logical operations (AND,OR,NOT).
Operator Symbol Description Example(a=1 Result
0,b=5)
Logical AND && Returns true if a>b && b>0 1
both conditions
are true
Logical OR || Returns true if a>b || b<0 1
one condition is
true
Logical NOT ! Reverse the truth !a>b 0
table
1) Bitwise Operators :
• Perform bit-level operations on integers.
Operator Symbol Description Example(a=5, Result
b=3)
AND & Performs bitwise 5&3 = 0101 & 0001(1)
AND 0011
OR | Returns true if 5&3 = 0101 | 0111(5)
one condition is 0011
true
XOR ^ Performs bitwise 5^3 0110 (6)
XOR
Left Shift << Shifts bits left 5<<1 = 1010 10
Right Shift >> Reverse the truth 5>>1 = 0010 2
table
1) Assignment Operators :
• Used to assign values to variables
Operator Symbol Description Example(a=1 Result
0)
Assign = a=10 a=10 -
Add & Assign += a+=5 a+=5 a=a+5
Subtract & -= a-=5 a-=5 a=a-5
Assign
Multiply & Assign *= a*=5 a*=5 a=a*5
Divide & Assign /= a/=5 a/=5 a=a/5
Modulus & %= a%=5 a%=5 a=a%5
Assign
1) Increment & Decrement Operators :
• Used to increase or decrease a variable’s value
Operator Symbol Description Example(a=1 Result
0)
Increment ++ Increases value a++ 6
by 1
Decrement -- Decreases value b++ 4
by 1
1) Ternary Conditional Operator :
• Used to increase or decrease a variable’s value
• condition ? expression1 : expression2;
• E.g.
#include <stdio.h>
int main() {
int a = 10, b = 5;
int min = (a < b) ? a : b;
printf("Minimum value: %d\n", min);
return 0;
}
1) Typecasting in C:
• Typecasting is the process of converting one data type into another. C provides two
types of typecasting : 1) Implicit Typecasting (Automatic Conversion) 2) Explicit
Typecasting (Manual Conversion)
1) Implicit Typecasting :
• Happens automatically when a smaller data type is promoted to a larger data type.
#include <stdio.h>
int main() {
int a = 10; float b = a; // Implicit conversion from int to float
printf("Value of b: %f\n", b);
return 0; }
2) Explicit Typecasting :
• Requires the programmer to manually convert a variable from one type to another
using (type)
e.g.
#include <stdio.h>
int main() {
float a = 10.5;
int b = (int) a; // Explicit conversion from float to int
printf("Value of b: %d\n", b);
return 0;
}
2) Global and Local Variable Declaration :
1) Global variables :
• Declared outside all functions.
• Accessible throughout the program.
e.g. - #include <stdio.h>
// Global variable
int globalVar = 10;
void display() {
printf("Global Variable: %d\n", globalVar); }
int main() {
display();
return 0; }
2) Local variables :
• Declared inside functions.
• Accessible only within that function.
e.g. -#include <stdio.h>
void display() {
int localVar = 5; // Local variable
printf("Local Variable: %d\n", localVar);}
int main() {
display();
// printf("%d", localVar); // Error: localVar not accessible
return 0; }
• Data Input and Output :
1. printf() – Print formatted output
• The printf() function is used to display text and variable values on the console.
• Syntax : printf("string", arguments);
2. scanf() – Read formatted input
• The scanf() function is used to take input from the user.
• scanf("format string", &variable);
3. putchar() – print a single character
• The putchar() function prints a single character on the screen.
• Syntax : putchar(character);
e.g. - #include <stdio.h>
int main() {
char ch = 'A';
putchar(ch);
return 0;
4. getchar() – Read a single character
• The getchar() function reads a single character from the user.
• Syntax - character_variable = getchar();
e.g. #include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Reads one character
printf("You entered: ");
putchar(ch); // Prints the character
return 0; }
• Key difference between getchar() and scanf() –
• Getchar() reads a single character without needing format specifiers.
• Scanf(“%c”,&ch) can also be used for reading a character.
5. puts() – Print a string
• The puts() function prints a string followed by a newline (\n) .
• Syntax- puts(string);
e.g. int main() {
char str[] = "Hello, World!";
puts(str);
return 0; }
5. Gets () – Read a String
• The gets() function reads a full line of text (including spaces) until the user presses
enter.
• Syntax- puts(string);
e.g. #include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
gets(name);
printf("Hello, %s!\n", name);
return 0; }
1. Fundamentals Of The Graphics Design (Graphics functions in C)
1. Putpixel()
• This is very simple function , it just displays a pixel specified by co-ordinate location.
• Syntax : void putpixel(int x, int y, color);
• E.g. putpixel (100,200,white) (Pixel co-ordinates , color of dot)
2. Line()
• Used to draw line from a point (x1,y1) to (x2,y2)
• Syntax : void line (int x1, int y1, int x2, int y2);
• E.g. line(20,20,30,30)
3. Rectangle()
• This function is used to draw a rectangle. Co-ordinates of left top and right bottom
corner is required to draw the rectangle.
• Syntax : void rectangle(int left, int top , int right, int bottom);
• E.g. rectangle(10,10,30,30);
3. Bar() – ( Same as Rectangle)
• This function is used to draw 2D rectangle filled bar.
• Syntax : void bar(int left, int top , int right, int bottom);
• E.g. bar(10,10,30,30);
3. circle() –
• Circle function is used to draw a circle with center(x,y) and third parameter specifies
the radius of the circle.
• Syntax : void circle(int x, int y, int radius)
• E.g. – circle(3,3,50);
* Difference Between Web Design & Graphic Design :
Web Design Graphic Design
Designing functional and user-friendly Creating visually appealing content for print
websites. and digital media.
Layout , UI/UX, responsiveness, interactivity. Branding , visual storytelling
Websites , web applications , mobile Logos, brochures, posters, social media posts,
interfaces advertisement.
HTML/CS , Wordpress, Sketch Adobe photoshop , illustrator, InDesign.
Interactive & dynamic (buttons, animations, Static visuals with no interactivity.
hover effects)
Requires regular updates , bug fixes and No maintenance needed after creation
optimizations.
Works with developers & UX designers. Works with marketing teams and branding
experts.