Unit-2 Introduction to C
C programming is a general-purpose, procedural programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. e The UNIX OS was totally written in C.
History of C
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the c language. C was developed to overcome the problems of previous languages such as B, BCPL, etc.
Basic Structure of C program
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
main() Function- User-Defined Functions
In C language, all these six sections together make up the Basic Structure of C Program
1. Documentation Section
Programmers write comments in the Documentation section to describe the program. The compiler ignores the comments and does not print
them on the screen. Comments are used only to describe that program.
/* File Name -: Hello.c
Author Name -: Rajesh parajuli
Founder of parajulirajesh.com.np
Date -: 12/09/2023
Description -: Basic Structure of C program */
//This is a single line comment
2. Link Section or Preprocessor Section
Includes header files needed by the program. Header files contain predefined functions. Within the Link Section, we declare all the Header Files that are used in our program. From the link section, we instruct the compiler to link those
header files from the system libraries, which we have declared in the link section in our program.
#include <stdio.h>
3. Definition Section
The Definition Section of a C program is used to define constants and macros using the #define directive. The values defined in this section do not change during program execution. It helps make the program easy to understand, avoids repeating fixed values, and allows changes to be made in one place. This section is written before the main() function and improves the readability and maintainability of the program.
#define PI 3.14
#define SQUARE(x) (x * x)
4. Global Declaration Section
The Global Declaration Section of a C program is where global variables and function prototypes are declared. Variables declared here, called global variables, can be used in any part of the program, including main() and other functions. Function prototypes tell the compiler about functions that will be defined later. This section is written before the main() function and helps make the program organized, easy to understand, and maintain.
int total; // global variable
void display(); // function prototype
5.main() Function
The main() function is the starting point of a C program where execution begins. All the program’s instructions are written inside it, and it can call other functions. It usually returns 0 to indicate that the program ran successfully.
int main() {
// statements
return 0;
}
6.User-Defined Functions
User-defined functions are the functions written by the programmer to perform specific tasks in a C program. They help make the program organized, reusable, and easy to understand.
#include <stdio.h>
void greet() { // user-defined function
printf(“Hello!”);
}
int main() {
greet(); // calling the function
return 0;
}
Character Set
As every language contains a set of characters used to construct words, statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of 256 characters. Every character in C language has its equivalent
ASCII (American Standard Code for Information Interchange) value.
Every C program contains statements. These statements are constructed using words and these words are constructed using characters from C
character set. C language character set contains the following set of characters:
1. Alphabets
2. Digits
3. Special Symbols
Alphabets:
C language supports all the alphabets from the English language. Lower and upper case letters together support 52 alphabets.
lower case letters — a to z
UPPER CASE LETTERS — A to Z
Digits: C language supports 10 digits which are used to construct numerical values in C language.
Digits — 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols:
C language supports a rich set of special symbols that include symbols to perform mathematical operations, to check conditions, white spaces,
backspaces, and other special symbols.
Special Symbols -~ @#$%*&*()_-+={}[]5:5″/?.>,<\]
A token is a smallest individual element of a program which is meaningful to the compiler. The compiler that breaks a program into the smallest
units is called tokens and these tokens proceed to the different stages of the compilation. Tokens in C are building blocks which means a program can’t be created without tokens.
Classification of Tokens are:
- Keywords
- Identifiers
- Constants
- Strings
- Special Symbols
- Operators
Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and
they cannot be used as an identifier.
C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all Keywords allowed in ANSI C.
Identifier
Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in C are the user-defined words. It can be composed of
uppercase letters, lowercase letters, underscore, or digits, but the starting letter should be either an underscore or an alphabet. Identifiers cannot
be used as keywords. Rules for constructing identifiers in C are given below:
- An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters and digits) and underscore( _ ) symbol.
Identifier names must be unique - The first character must be an alphabet or underscore.
- You cannot use a keyword as an identifier.
- Only the first thirty-one (31) characters are significant.
It must not contain white spaces. - Identifiers are case-sensitive.
For Example:
int cprogram;
Char Bicte_firstsemester;
here, int and Char is keywords and cprogram and Bicte_firstsemester is identifier.
Constants:
The constants in C are the read-only variables whose values cannot be modified once they are declared in the C program. The type of constant
can be an integer constant, a floating pointer constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.
What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared in the program. We can not make any
change in the value of the constant variables after they are defined.
Syntax to Define Constant
const data_type var_name = value;
Strings:
Sequence of Characters is known as Strings.
e Every String is terminated by \0
¢ String Constant is a sequenced os 0 or more characters enclosed between double quotes ”” is known as string constant e.g. “S”, “XYZ”,
“123”, “hello world”
¢ All characters are converted into their corresponding ASCII value and then stored in memory as contiguous allocation.
¢ String Variable is the array of character type. For e.g. char[10];
char greeting[6] = {H’, ‘e’, ‘I’, I’, ‘o’, ‘\O’};
If you follow the rule of array initialization then you can write the above statement as follows —
char greeting[] = “Hello’;