0% found this document useful (0 votes)
81 views65 pages

Unit I C Programming Basics

This document provides an overview of the basics of C programming, including the structure of a C program, data types, operators, expressions, and input/output operations. It covers essential components such as identifiers, keywords, variable declarations, and various types of operators. Additionally, it discusses compiling and debugging processes, as well as managing input and output using standard library functions.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views65 pages

Unit I C Programming Basics

This document provides an overview of the basics of C programming, including the structure of a C program, data types, operators, expressions, and input/output operations. It covers essential components such as identifiers, keywords, variable declarations, and various types of operators. Additionally, it discusses compiling and debugging processes, as well as managing input and output using standard library functions.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C

PROGRAMMING
BASICS
UNIT I
UNIT I
C PROGRAMMING BASICS
Structure of a C program – Compiling and Debugging a C
program - C Character set, Identifiers and Keywords, Data
Types, Declarations, Expressions, Statements and
Symbolic constants,
Operators – Arithmetic Operators – Unary operators –
Relational and Logical Operators – Assignment operators
– Conditional operators. Managing Input and Output
operations, preprocessor directives and storage classes.
STRUCTURE OF
A C PROGRAM
Structure of a C program
•The structure of a C program typically consists of the following
components:
•Preprocessor Directives
•Global Declarations
•Function Definitions
•Statements
•Comments
COMPILING AND
DEBUGGING A C
PROGRAM
•Compiler – CodeBlocks
•Or visual studio
C CHARACTER
SET
C Character set
Type of Descripti Characters
Character on
Lowercase a to z a, b, c, d, e, f, g, h, i, j, k, l,
Alphabets m, n, o, p, q, r, s, t, u, v, w,
x, y, z
Uppercase A to Z A, B, C, D, E, F, G, H, I, J, K,
Alphabets L, M, N, O, P, Q, R, S, T, U,
V, W, X, Y, Z
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Characters – `~@!$#^*%&()[]{
}<>+=_–|/\;:‘“,.?

White Spaces – Blank Spaces, Carriage


Return, Tab, New Line
ASCII Values
ASCII Value Character
32 Space
48 0
49 1
50 2
65 A
66 B
97 a
98 b

[Link]
in-c/
IDENTIFIERS
AND KEYWORDS
Identifiers and Keywords
•Identifiers, as the name suggests, are used to identify or name
variables, symbolic constants, functions, etc.
•E.g. int money;

double accountBalance;
•Keywords have predefined meanings and cannot be changed by the
user.
Rules for naming identifiers
•A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
•The first letter of an identifier should be either a letter or an
underscore.
•You cannot use keywords like int, while etc. as identifiers.
•There is no rule on how long an identifier can be. However, you may
run into problems in some compilers if the identifier is longer than
31 characters.
DATA TYPES,
DECLARATIONS
Basic data types
•The C language supports the following basic data types:
Datatyp Explanation Size & Range Format
Specifier
e
char a single byte that can 1 byte (-128 to 127) %c
hold one character

int an integer 4 bytes (- %d


2147483648 to
2147483647)
float a single precision 4 bytes (±1.2×10⁻³⁸ to %f
floating point number ±3.4×10³⁸)

double a double precision 8 bytes (±2.23×10⁻³⁰⁸ %lf


floating point number to ±1.79×10³⁰⁸)
Qualifiers
•A qualifiers alters the characteristics of the data type, such as size or
sign.
•To alter the size: short and long is used.
•To alter the sign: signed and unsigned is used.
•E.g.
short int Signed long int
Long int Unsigned long int
Long double Singed char
Signed short int Unsigned char
Unsigned short int
Signed int
Unsigned int
Declarations
•In C we need to declare the variable before using it.
Types of variables in c:
•local variable - declared inside the function which can be
used only inside the function
•global variable - declared outside the function which can
be used throughout the program.
•static variable - It is used to retain its value between
multiple function calls. It is declared using static keyword.
•external variable - share a variable in multiple C source
files by using external variable. It is declared using extern
keyword.
•Automatic variable - declared inside the block is known as
automatic variable by default

20
Declaring, Defining and
Initializing a variable
#include <stdio.h>
// Variable declaration(optional)
extern int a, b; extern int c;
int main ()
{
/* variable definition: */
int a, b;
/* actual initialization */
a = 7; b = 14;
/* using addition operator */
c = a + b;
/* display the result */ printf("Sum is : %d \n", c);
return 0; } 21
EXPRESSIONS,
STATEMENTS
AND SYMBOLIC
CONSTANTS
Expressions
• Types of expressions in C that are frequently used, including:

1. Arithmetic expressions
2. Relational expressions
3. Logical expressions
4. Bitwise expressions
5. Assignment expressions
6. Conditional expressions (also known as ternary operators)
7. Comma expressions
8. Pointer expressions
9. Function call expressions
10. Array subscript expressions
Arithmetic expressions
int x = 10, y = 5;
int sum = x + y; // addition
int diff = x - y; // subtraction
int product = x * y; // multiplication
int quotient = x / y; // division
int remainder = x % y; // modulus
Relational expressions
int a = 10, b = 20;
if (a == b) {
printf("a is equal to b\n");
}
if (a != b) {
printf("a is not equal to b\n");
}
if (a < b) {
printf("a is less than b\n");
}
if (a <= b) {
printf("a is less than or equal to b\n");
}
Logical expressions
int x = 5, y = 10;
if (x < y && y > 5) {
printf("x is less than y and y is greater than 5\n");
}
if (x < y || y < 5) {
printf("x is less than y or y is less than 5\n");
}
if (!(x < y)) {
printf("x is not less than y\n");
}
Assignment expressions
int x = 10;
x += 5; // equivalent to x = x + 5
x -= 2; // equivalent to x = x - 2
x *= 3; // equivalent to x = x * 3
x /= 2; // equivalent to x = x / 2
x %= 4; // equivalent to x = x % 4
Bitwise expressions
unsigned int a = 60; // 0011 1100
unsigned int b = 13; // 0000 1101
unsigned int c = 0;

c = a & b; // bitwise AND: 0000 1100


c = a | b; // bitwise OR: 0011 1101
c = a ^ b; // bitwise XOR: 0011 0001
c = ~a; // bitwise NOT: 1100 0011
c = a << 2; // left shift: 1111 0000
c = a >> 2; // right shift: 0000 1111
Ternary expression (Conditional
Expression)
Syntax:
condition ? expression1 : expression2

E.g-
int a = 10, b = 20, max;
max = (a > b) ? a : b;
//expres.c // Logical expression
#include <stdio.h> if (a && b) {
printf("Both a and b are true \
int main() { n");
int a, b, c; }
printf("Enter A and B value: ");
// Bitwise expression
scanf("%d %d", &a, &b); c = a | b; // bitwise OR
// Arithmetic expression printf("a | b = %d \n", c);
c = a + b;
// Assignment expression
printf("a + b = %d \n", c); a = b;
printf("a = %d \n", a);
// Relational expression
// Conditional expression
if (a > b) { a = 10;
printf("a is greater than b \n"); b = (a == 1) ? 20 : 30;
printf("Value of b is %d \n", b);
} else {
printf("b is greater than a \n"); return 0;
} }
7. Comma expressions - int x = 1, y = 2, z = 3;

8. Pointer expressions - A variable that stores the memory address of


another variable.
E.g- int *ptr; // declares a pointer to an integer variable

9. Function call expressions - Invokes a function with specified


arguments and returns the result of the function.
E.g - int result = add(3, 5);

10. Array subscript expressions - Accesses an element of an array


using an index value.
E.g - int arr[5] = {1, 2, 3, 4, 5};
int x = arr[2];
Formatted input
•The input is read according to a specific format or pattern specified
by the programmer. The scanf() function is used for formatted input
in C.
•E.g.

int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is: %d\n", age);
Unformatted input
•The input is read as a string of characters without any specific
format. The getchar() and gets() functions are used for unformatted
input in C.
•E.g.

char c, name[20];
printf("Enter a character and Name: ");
c = getchar();
gets(name);
printf("The character you entered is: %c \n", c);
printf("Your name is: %s \n", name);
Statements
•In C, statements are instructions that perform specific tasks or
actions. Some common types of statements in C include:
•Assignment statement
•Conditional statement
•Loop statement
•Function call statement
•Return statement and many more..
Assignment statement
•This statement assigns a value to a variable. For example, x = 10;
assigns the value 10 to the variable x.
Conditional statement
•This statement allows you to execute a block of code if a certain
condition is met. The most common conditional statement is the if
statement. For example:
if (x > 5) {
printf("x is greater than 5");
}

Loop statement
•This statement allows you to repeat a block of code a certain number
of times or until a certain condition is met. The most common loop
statements are the for loop and the while loop.
Function call statement
•This statement calls a function and passes it any necessary
arguments.
•For example, printf("Hello, world!\n"); calls the printf function and
passes it the string "Hello, world!\n".

Return statement
•This statement exits a function and returns a value to the calling
function. For example:
int square(int x) {
return x * x;
}
•Switch statement •Do-while statement

•Goto statement •Label statement

•Null statement •Case statement

•Compound statement •Default statement

•Declaration statement •Structured statement

•Expression statement •Return statement

•Break statement •Preprocessor statements

•Continue statement •Increment and decrement


statements
Constants
•Its value is fixed throughout the program that means
constants are those variables which value is not changed
throughout the program.

38
1. Using #define
•It is some constant value or an expression that can be defined
using the #define command in our C Program.
#include <stdio.h>
#define value 10
void main()
{
int data;
data = value*value;
printf("value of data : %d",data);
}
Output
value of data : 100

39
Contd..
Ex.2:
#include <stdio.h>
#define sum(a,b) (a+b)
void main()
{
int data, a=1, b= 5;
data = sum(a,b);
printf("value of data : %d",data);
}
Output
value of data :6

40
2. Using const keyword
#include <stdio.h>
void main()
{
const int value = 10;
int data;
data =value*value;
printf("value of data : %d",data);
}
Output
value of data : 100

41
OPERATORS
Operators
•Operators are used to perform operations on variables and values.

•C divides the operators into the following groups:


1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
UNARY
OPERATORS
Unary Operator
•These are the type of operators that act upon just a single operand
for producing a new value.
•There are following types of unary operators found in the C language:

1. unary minus (-)


2. unary plus (+)
3. decrement (- -)
4. increment (++) Pre /
5. NOT (!) Post
6. sizeof ()
7. Address of operator (&)
Pre or post
increment / decrement
#include<stdio.h>
int main()
{
int a, b;
a= 10;
b = ++a;
printf("%d\n", b);
printf("%d\n", b++);
printf("%d\n", b);
return 0;
}
sizeof operator
•The sizeof operator in C is a unary operator that returns the size, in
bytes, of its operand.
•The operand can be a type name or an expression.
•Syntax:

sizeof (type) or sizeof expression


#include <stdio.h>
int main() {
int i;

printf("Size of int: %zu \n", sizeof(int));

printf("Size of i: %zu \n", sizeof(i));

int arr[5] = {1, 2, 3, 4, 5};

printf("Size of array: %d \n", sizeof(arr));


printf("Length of array: %d \n", sizeof(arr)/sizeof(arr[0]));

return 0;
}
address-of (&) operator
•In C, the "address-of" operator (&) is used to get the memory address
of a variable.
#include <stdio.h>

int main() {
int num = 42;

printf("Value of num: %d \n", num);


printf("Address of num: %p \n", &num);

return 0;
}
DATA TYPES,
DECLARATIONS
Data Types
• C supports several data types, including:

1. Integer types: char, short, int, long, long long


2. Floating-point types: float, double, long double
3. Void type: void
4. Enumerated types: enum
5. Pointer types: *
6. Array types: [ ]
7. Structure types: struct
8. Union types: union
9. Function types: ( )
enum
•enum is a user-defined data type that allows you to define a set of
named values or constants.
•enum stands for "enumeration", and it is often used to define a set of
related constants that can be referred to by name, rather than by
their underlying numerical values.
#include<stdio.h>

enum color{
Red, Green, Blue
};

int main(){
enum color input = Blue;
printf("%d",input);
return 0;
}
MANAGING
INPUT AND
OUTPUT
OPERATIONS
Managing i/o operations
•input and output (I/O) operations are managed through the standard
I/O library, which provides functions for reading from and writing to
files, as well as for reading from and writing to the console.
•Some of the commonly used operations are:

1. printf ( )
2. scanf ( )
3. fprintf( )
4. fscanf ( )
5. fgets ( )
6. getchar ( )
7. gets ( )
PREPROCESSOR
DIRECTIVES AND
STORAGE
CLASSES
Preprocessor Directives
•Preprocessor directives are special instructions that are processed by
the preprocessor before the actual compilation of the program
begins.
•They start with a # character and typically appear at the beginning of
a C source file.
• Commonly used preprocessor directives in C include #include,
#define, #ifdef, #ifndef, #if, and #endif.
Storage Classes
•Storage classes in C specify the scope, lifetime, and visibility of
variables and functions.
•There are four types of storage classes in C:

1. auto,
2. register,
3. static, and
4. extern.
auto
•The auto storage class is the default storage class for local variables.
•E.g

#include <stdio.h>

int main() {
auto int x = 5;

printf("The value of x is %d \n", x);

return 0;
}
register
•The register storage class is used to declare variables that should be
stored in a register instead of memory.
•This can result in faster access times for frequently used variables.
•E.g - #include <stdio.h>

int main() {
register int i;

for (i = 0; i < 1000000; i++) {


// do something
}

return 0;
}
static
•The static storage class is used to declare variables and functions that
retain their values between function calls.
static
#include <stdio.h>
void func() {
static int count = 0;
count++;
printf("This function has been called %d times\n", count);
}

int main() {
func();
func();
func();
return 0;
}
extern
•extern keyword is used to declare a variable or function that is
defined in another source file or module.
•E.g

// file1.c
#include <stdio.h>

extern int x; // declare that x is defined in another source file

int main() {
printf("The value of x is %d \n", x);
return 0;
}
// file2.c
#include<stdio.h>

int x = 10; // define the global variable x

int main() {
return 0;
}

You might also like