You are on page 1of 10

C Programming

1. History of C
2. Datatypes and Variables
3. C Tokens
4. Preprocessor Directives
5. Control Statements
6. Loops
7. Pointers
8. Functions
9. Escape sequences
10. Strings
11. Data structures
12. Sorting Algorithm

History of C

Dennis ritchie at Bells Lab in the year 1972


C is very flexible and versatile, allows maximum control with minimal commands.

Features of C

High level Language => Language that is written in English or any User readable
language is known as High level language.
Structured Language => Procedure Oriented Programming POPs
Rich Library => Most of the Arithmetic functions are available in the in-built functions
Extensible
Recursion
Pointers => Interaction with physical memory
Faster execution
Memory management => Many functions such as malloc, calloc are very useful in
creating/ modifying the memory needed.

Tokens
Keywords
Variables that have special meaning that are defined earlier in the C Libraries.

Ex.: float, continue, for, if, static, auto, const, break, default, goto, etc..
Can’t rename, redefine or reprogram.

Constants
The values of the variables are fixed unlike the other variables which can be changed whenever
or wherever.

Strings
Collection of characters, ends with a Null character that denotes the end of the string.
Syntax: char stringName[Length_of_Characters] = “vengadesh”;

Special symbols
Characters that have special meaning that are predefined in the compiler and used only in their
specified space. (For ex.: In scanf ‘&’ is used, printf ‘%’ is used, etc.)

Identifiers
Names that are declared for the variable, function name or whatever we need a name to access.

Rules:
1. Can start with an alphabet, Can start with an Underscore
2. Int hi hello; (wrong) int hiHello; int hi_Hello; => Shouldn’t have a space in-between.
3. Shouldn’t begin with a number, a_1 a1 possible
4. Shouldn’t use keywords, special characters (exception for underscore).

Operators
Types
1. Arithmetic (+, -, /, *, %)
2. Increment/ Decrement (++, --)
3. Assignment (=)
4. Relational (>, <, >=, <=, ==, !=)
5. Logical (&&, ||, !)
6. Bitwise (&, |, ~, ^, <<, >>) 5<<1=10, 10>>1=5
5 = 0101
LS => 1010 = 10 (*2)
10 = 1010
RS => 0101 = 5 (/2)

Datatypes and Variables

Types:
1. Basic
2. Derived
3. Enumeration
4. Void
Variables is defined as the reserved memory space for the which stores a value of a definite
datatype. The value of the variable isn’t constant, instead it allows changes.
Types:
1. Local => Scope: within the func
2. Global => Scope: throughout the program
3. Static => Why: Retaining the value Scope: inside/ outside
4. Automatic => Similar to local
5. Extern => extern keyword scope: once the specified header file is given extern can be
used

Rules:
1. Can start with an alphabet, Can start with an Underscore
2. Int hi hello; (wrong) int hiHello; int hi_Hello; => Shouldn’t have a space in-between.
3. Shouldn’t begin with a number, a_1 a1 possible
4. Shouldn’t use keywords, special characters (exception for underscore).

Preprocessor Directive

Starts with the #

Code -> Preprocessor -> Compiler -> Linker

Types:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives

Control Statements

Enable us to specify the flow of control of the program. Specifies the order in which the
instructions in a program must be executed.
Variants:
1. if
2. if-else
3. if-else…-if-else ladder
4. Nested if
5. Switch
6. Ternary (x>5)?true:false
7. Break

P#1: Check whether the Given number is odd/ even. (without %2)
1 = 0001 (Perform AND)
8 = 1000 9 = 1001
0000 = 0 0001 = 1
Even Odd

P#2: For the given marks calculate the total marks and assign concern grades (Atleast have
3 levels of Grades).
P#3: Using Nested if, find the largest among the given 3 numbers.
P#4: Create a simple calculator.
P#5: Using ternary, find the largest among the given 2 numbers.

Loops

Iterative statements
Types:
1. for
2. while
3. do while

P#6: Find the sum of 1^2 + 2^2 + 3^2 + 4^2 +....... + K^2, where K will be given as an
input. (Sum of square of numbers Formula)
P#7: Count the number of digits in a given number.
P#8: Pattern

a) b) c)

d) e) f)

P#9: Check whether the given number is Prime/ Not.


P#10: Write a C program to check if nth bit is set or not in a 32 bit integer.
0000 0000 0000 0000 0000 0000 0000 1111
MSB LSB
ACQ, MSB->LSB
COMPILER, LSB->MSB
--------------------------------------------------------------------------------
Logic:
N = 15
K = 29 => 32-K = K

N=N>>K; //=> N = 1
if(N&1 == 1)
“Bit is ON”
else
“Bit is OFF”

Pointers

Variable that stores the address of another variable, can be of any primitive data types. Declared
using * symbol.
Say,
int n = 10;
int* p = &n;

Advantages
● Can return multiple values from a function
● Access any memory location
● Dynamically allocate memory using malloc() and calloc()
● Widely used in arrays, functions, structures to reduce the code and improve the
performance

P#11: Print an array using a pointer along with the array element’s address.
P#12: Find the output
#include <stdio.h>
int main()
{
int* pc;
int c;
c = 22;
printf("Address of c: %p\n", &c); //1000
printf("Value of c: %d\n\n", c); //22

pc = &c;
printf("Address of pointer pc: %p\n", pc); //1000
printf("Content of pointer pc: %d\n\n", *pc); //22
c = 11;
printf("Address of pointer pc: %p\n", pc); //1000
printf("Content of pointer pc: %d\n\n", *pc); //11

*pc = 2;
printf("Address of c: %p\n", &c); //1000
printf("Value of c: %d\n\n", c); //2
return 0;
}

P#13: Swap two numbers within the main( ) using pointers.

Escape Sequence
These are non-printable and are used to communicate with display devices.
They can be defined by - “\”+ A letter or a digit.

Functions
A Function can be defined as a subdivide program of a main program enclosed with in
flower brackets.
Advantages - Reusability and modularity
Types - Library and User-defined.

Function Structure in C:
return_type function_name (Arguments if needed){
// code
// return statement if return_type is specified
}
Different aspects of Functions:
● Without argument Without return value
● Without argument With return value
● With argument Without return value
● With argument With return value

P#14: Have a switch case for the “Four aspects of Functions” and perform addition of 3
variables.
P#15: Below is the code for swapping two numbers, complete the code and/ or find the
unwanted lines.
#include <stdio.h>

// function to swap the two numbers


void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

int main()
{
int num1,num2;

printf("Enter value of num1: ");


scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);

//displaying numbers before swapping


printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

//calling the user defined function swap()


swap(&num1,&num2);

//displaying numbers after swapping


printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

return 0;
}

You might also like