You are on page 1of 13

SIVASREE MS

FOP NOTES – Introduction to C

INTRODUCTION
1. ORIGINS AND DEVELOPMENT

• The C programming language was created by Dennis M. Ritchie at Bell


Laboratories in the USA in the early 1970s.

• Ritchie wanted to make a language that is easy to use and can be used on
different types of computers.

2. ASSOCIATION WITH UNIX

• C is closely connected with the UNIX operating system. It has been the
language of choice for making a big part of the UNIX system and its apps
since the 1970s.

• One of C's strengths is that it can work on different computers. This made
UNIX one of the first portable operating systems. This means that the code
that makes up UNIX doesn't need to know or care about the specific
computer it's running on.

3. HISTORICAL CONTEXT

• C has its roots in earlier programming languages. It grew from BCPL (Basic
Combined Programming Language) and B, both made at Bell Laboratories.

• The previous version of C was called B. Ken Thompson made it for the DEC
PDP7 computer. Ritchie made it even better and that's how C came to be.
4. STANDARDIZATION

• In 1983, a group decided on a set of rules for using C. The goal was to
create a clear way of writing C that could work on any computer.

• These rules are called ANSI C, and they became the standard way to use C.
There's also another set of rules by ISO that's very similar.

5. LOOKING AHEAD: THE PROSPECT OF NEW STANDARDS

• Around the late 1990s and early 2000s, people were talking about making
some new rules for C, which might be called "C9X." This shows that C is
still changing and improving.

BASIC STRUCTURE OF A C PROGRAM


1. DOCUMENTATION SECTION

Purpose: Document the logic, objectives, and details of the program.

Syntax: Comments are written using /* and */.

2. LINKING SECTION

Purpose: Specify header files for linking keywords or functions used in the
program.

Example: #include <stdio.h>

3. DEFINITION SECTION

Purpose: Define constants and assign values to them.

Example: #define MAX 25

4. GLOBAL DECLARATION SECTION

Purpose: Declare variables used throughout the program (accessible globally).

Example: int i; (placed before main())

5. MAIN FUNCTION SECTION

Purpose: Define the starting point for program execution.


Structure:

• Declaration Section: Declare variables and their data types.


• Executable Section: Write the code to perform the desired task.

Example:

int main()
{
// Declaration section
// Executable section
return 0;
}

6. SUB PROGRAM OR FUNCTION SECTION

Purpose: Define additional subprograms or functions needed by the program.

Example of a Simple C Program

/* Simple program in C */

#include <stdio.h>

int main()
{
printf("Welcome to C programming");

return 0;
}

/* End of main */
C TOKENS: THE SMALLEST UNITS

Tokens in C are the smallest units, comprising operators, special symbols, strings,
constants, identifiers, and keywords. Each serves a distinct purpose in the
language.

1. OPERATORS

These symbols perform operations on variables or values.

Examples: + (addition),- (subtraction), * (multiplication)

2. SPECIAL SYMBOLS

These characters have special meanings in C.

Examples: [ ] (square brackets), { } (curly braces)

3. STRINGS

Strings are sequences of characters, enclosed in double quotes.

Example: "Sivasree"

4. CONSTANTS

These are fixed values that don't change during program execution.

Examples: 15.4 (floating-point number), 'a' (character), 200 (integer)

5. IDENTIFIERS

Identifiers are names given to variables, functions, etc.

Examples: rate, no_of_hours

6. KEYWORDS

Keywords are reserved words with specific meanings in C.

Examples: int (datatype), printf (output function)


KEYWORDS IN C LANGUAGE
Keywords in C are special words with specific meanings. They are reserved for
certain operations and shouldn't be used as variable names or identifiers. Each
keyword serves a unique purpose in C programming.

The standard 'C' keywords are:

IDENTIFIERS IN C

Identifiers are names given to variables and other program elements like
functions, arrays, etc. They must adhere to specific rules:

1. Identifiers can consist of letters (A-Z, a-z), digits (0-9), and underscores (_).

2. The first character of an identifier should be a letter or an underscore. It


cannot start with a digit.

3. Identifiers cannot be a reserved keyword.

Examples of Identifiers:

• userName
• totalAmount
• num1
After naming a variable, it must be declared to the compiler along with its data
type. The format for declaring variables is:

DataType variable1, variable2;

Where DataType can be float, int, char, or any other data type. variable1,
variable2, variable3, etc., are the names of the variables.

Examples of Variable Declarations:

int age, quantity, total;


float height, width, area;
char grade;

CONSTANTS IN C
Constants are values that stay the same while a program runs. There are two
main types:

1. NUMERIC CONSTANTS:

Integer Constants are whole numbers, like 786 or -127.

Floating Point Constants have a decimal point or 'e' (like 1e2 for a very small
number).

2. CHARACTER CONSTANTS

• These are single characters, like 'a', written between single quotes.

• Some special characters, like '\n', represent things like a new line.

• Strings are a bunch of characters together, like "I am Sivasree", written


between double quotes.

Don't confuse single character like 'a' with a string like "a". They're different in C.
ESCAPE SEQUENCES:
• \a : Alert
• \b : Backspace
• \f : Form feed
• \n : New Line
• \r : Carriage return
• \t : Horizontal Tab
• \v : Vertical Tab

DATA TYPES IN C
Data types are crucial in programming as they specify how data is stored and
what operations can be performed on it. C supports four classes of data types:

1. Basic Data Types


2. Derived Data Types
3. User Defined Data Types
4. Pointer Data Types

1. BASIC DATA TYPES

Arithmetic operations like addition and subtraction can be performed on basic


data types.

Examples:

int a, b;
char c;

2. DERIVED DATA TYPES

Derived data types are used to store a set of data values. Examples include
Arrays and Structures.
Examples:

int a[10];
char name[20];

3. USER DEFINED DATA TYPES

C provides typedef for creating new data type names defined by the user. For
example, the declaration:

typedef int Integer;

makes Integer a synonym of int. Now, Integer can be used in declarations, casts,
etc., like:

Integer num1, num2;

which will be treated by the C compiler as the declaration of num1 and num2 as
int variables. typedef is especially useful with structures and pointers.

4. POINTER DATA TYPES:

Pointer data type is necessary to store the address of a variable.

OPERATORS IN C
Operators are symbols that instruct the compiler to perform specific
mathematical or logical operations. They are used to form expressions. C
operators can be classified into several categories:

1. ARITHMETIC OPERATORS

• + : Addition
• - : Subtraction
• * : Multiplication
• / : Division
• % : Modulo Division (Remainder)
Example:

int a = 10, b = 5;
int sum = a + b; // sum will be 15

2. RELATIONAL OPERATORS

• < : Less than


• > : Greater than
• <= : Less than or equal to
• >= : Greater than or equal to
• == : Equal to
• != : Not equal to

Example:

int x = 10, y = 5;
if (x > y)
{
printf("x is greater than y");
}
else
{
printf("x is not greater than y");
}

3. LOGICAL OPERATORS

• && : Logical AND


• || : Logical OR
• ! : Logical NOT
Example:

int condition1 = 1, condition2 = 0;


if (condition1 && condition2)
{
printf("Both conditions are true");
}
else
{
printf("At least one condition is false");
}

4. ASSIGNMENT OPERATORS

• = : Assignment Operator
• +=, -=, *=, /=, %= : Shorthand Assignment Operators

Example:

int x = 5;
x += 3; // Equivalent to x = x + 3, x will be 8

5. INCREMENT AND DECREMENT OPERATORS

• ++ : Increment
• -- : Decrement

Example:

int count = 10;


count++; // Equivalent to count = count + 1, count will be 11

6. CONDITIONAL OPERATOR

• ?: : Ternary Conditional Operator


Example:

int age = 20;


char result = (age >= 18) ? "You can vote" : "You cannot vote";
printf("%s", result);

7. BITWISE OPERATORS

• & : Bitwise AND


• | : Bitwise OR
• ^ : Bitwise XOR (Exclusive OR)
• << : Left Shift
• >> : Right Shift
• ~ : One's Complement (Bitwise NOT)

Example:

int x = 5, y = 3;
int result = x & y; // Bitwise AND, result will be 1

8. SPECIAL OPERATORS

• , : Comma Operator
• sizeof : Size of Operator
• & : Address-of Operator
• * : Pointer Operator
• . : Member Selection Operator (Dot Operator)
• > : Member Selection Operator (Arrow Operator)
INPUT AND OUTPUT IN C

C uses printf for output and scanf for input. The printf statement displays the
prompt, and the scanf statement reads the input and stores it in variable.

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

printf("Enter an integer: ");


scanf("%d", &num);

printf("You entered: %d\n", num);

return 0;
}

GIVE OUTPUT:

printf("Enter an integer: ");

• This statement uses the printf function to print a message to the console.
In this case, it displays the message "Enter an integer: ".

GET INPUT:

scanf("%d", &num);

• This statement uses the scanf function for input.


• scanf is a function used for reading input from the user. It takes two
arguments: the format specifier and the memory address where the
value should be stored.
• In this case, the format specifier is %d, which is used to indicate that an
integer value is expected.
• &num provides the memory address of the variable num. The & operator
is used to get the address of num.
• The user inputted integer will be stored at the memory location pointed
by &num.

These two statements work together to prompt the user to enter an integer, and
then store that integer value in the variable num.

You might also like