Programming in C - Unit 1 PDF
Document Details
Tags
Summary
This document provides an overview of the C programming language, including its features, character set, tokens, keywords, and operators. It includes a general structure of a C program.
Full Transcript
# Programming in C - Unit 1 ## Overview of C Programming Language * A high-level language. * A procedure-oriented programming language * Developed in 1972 by Dennis Ritchie at Bell Laboratories of AT&T (American Telephone & Telegraph), U.S.A. ## Features of C Programming Language * **Robust prog...
# Programming in C - Unit 1 ## Overview of C Programming Language * A high-level language. * A procedure-oriented programming language * Developed in 1972 by Dennis Ritchie at Bell Laboratories of AT&T (American Telephone & Telegraph), U.S.A. ## Features of C Programming Language * **Robust programming language** * Contains rich set of operators and built-in function * **Middle level language** * Suitable for writing both system programs and application programs * **Portable** * Programs written in one machine can be easily executed on any other machine * **Structured** * We can break the program into parts using functions. * **Extensibility** * Sub programs can be added as and when required to extend the functionality. ## Character Set The C Programming language has the following character set: * **Lower case alphabets:** a to z * **Upper case alphabets:** A to Z * **Digits:** 0 to 9 * **Other characters:** + - * & $!@#% ({})[]:;, ! etc * **White space characters:** space, new line, tab etc ## C Tokens The smallest individual unit in a C program is known as a **Token**. * **Keywords** * **Special Symbols** * **Identifiers** * **Variables** * **Constants** * **Operators** ## Keywords Keywords are reserved words that have predefined meaning and the meaning cannot be changed by the programmer. C Programming has 32 keywords. | Keyword | Keyword | Keyword | Keyword | |---|---|---|---| | auto | double | int | struct | | break | else | long | switch | | case | enum | register | typedef | | char | extern | return | union | | const | float | short | unsigned | | continue | for | signed | void | | default | goto | sizeof | volatile | | do | if | static | while | ## Special Symbols In C language, symbols have certain special meaning. **Examples (few):** * **;** → used to terminate every statement * **, ** → used to separate variable the time of declaration * **# ** → to write pre-processor directive. ## Identifiers Identifiers refer to the name given by the programmer to the elements such as variables, constants, functions, and so on. **Rules of naming an identifier:** * Can contain alphabets, digits, and underscore (_) * Cannot start with digits * Case-sensitive: Lower case and Upper case are different * Keyword cannot be used as identifier * Blank spaces cannot be used **Examples:** * **Valid:** n, name, Name, sum, addition(), _rno, marks1 * **Invalid:** 2n, my name, int ## Variables Variables are quantities that change during the execution of the program. Sometimes we call it to be a name given to a storage area that a program can manipulate. **Rules of naming variables:** * Cannot start with digits * Case-sensitive: Lower case and Upper case are different * Keyword cannot be used as identifier * Can contain alphabets, digits, and underscore (_) * Blank spaces cannot be used ## Constants Constants are the quantities that do not change during the execution of the program. Rules of identifier should be followed while naming constants. In C, constants can be defined using **#define** preprocessor directive and such type of constants are known as **symbolic constants**. The **#define** must be used after **#include<>** statement and before global section. Example: ```C #define PI 3.142 #define MAX 100 ``` **Note:** In the above examples, PI and MAX are the symbolic constants. If any attempt is made to change the value of these constants, then compiler will through errors. ## Operators Operators are the symbols used to perform certain operations. **Classification of operators based on the number of operators they operate upon:** * **Unary Operators:** Operate upon one operand * **Binary Operators:** Operate upon two operands * **Ternary Operators:** Operate upon three operands ## General Structure of C Program Following is the general structure of C program and its meaning: ```C // Comments #include<stdio.h> #define int main() { // Variable declaration; // Executable statements; } return 0; function1() {} function2() {} function3() {} ``` * // Comments → Documentation section * #include<stdio.h> → Link section * #define → Definition (pre-processor) section * int main() → Global section * { → Beginning of program execution * → Beginning of main() function * → Body of main() function * } → End of main() function * return 0 ; → Subroutine section * function1() {} → (User-defined functions) * function2() {} * function3() {} ## Documentation Section Used to write the program details such as title of the program, program logic used, etc. **Comments are:** * Non-executable statement * Compile ignores the statements * Comments improve the readability of the program **Two types of comments:** * **Single line comments:** Written using two forward slashes (//) * **Multi-line comments:** Written using /* as starting point and */ as ending point. ## Link Section Link sections are defined using **#include** statement. These are used to link the header files to the program. Example: ```C #include<stdio.h> ``` Here **stdio.h** Is a header file that contains libraries of standard input and output operations. ## Definition Section Includes number of **#define** statements used for pre-processor directives. This section is used to declare and define symbolic constants. Example: ```C #define PI 3.142 ``` ## Global Section **Global section** is used to declare global variables. Global variables are accessible within the entire program. ## main() function Section The **main()** function is a user-defined function. Sometimes we call it to be implemented by user and prototype declared by the compiler. Program execution always starts with **main()** function. The body of the **main()** function is written within a pair of curly brackets - {} It contains **Consists** of two sections: 1. **Declaration section** - Variables are declared which are known as local variables 2. **Executable section** - Executable statements are included. Every executable statements always ends with semicolon (;) ## Subroutine Section This section consists of number of user-defined functions. User-defined functions are used to perform a task. ## Basic Structure of C Program Following sections are mandatory section to be included in every C Program. ```C #include<stdio.h> int main() { // } return 0; ```