Unit 2 Fundamental of C Programming PDF

Summary

This document provides an overview of the basics of C programming, including its fundamental concepts and structure. It explains the purpose of programming, the process of computer programming, and the characteristics of the C language.

Full Transcript

UNIT 2 FUNDAMENTAL OF ‘C’ WHAT IS PROGRAMMING? Program is a set of instructions which is written in any language on which a computer can understand and Task to write the program is known as programming. COMPUTER PROGRAMMING? Computer programming is the process of designing and building an execut...

UNIT 2 FUNDAMENTAL OF ‘C’ WHAT IS PROGRAMMING? Program is a set of instructions which is written in any language on which a computer can understand and Task to write the program is known as programming. COMPUTER PROGRAMMING? Computer programming is the process of designing and building an executable computer program for completing a specific task. C is a middle level and general purpose programming language that is used for developing portable applications. C is procedure oriented programming language developed in 1972 by Dennis Ritchie” at the Bell laboratory. INTRODUCTION OF STRUCTURED PROGRAMMING LANGUAGE Structured programming facilitates program understanding and modification and has a top – down approach, where a system is divided into subsystems or into small logical modules. Structured programming language is also known as Modular Programming Language. ALGOL, PASCAL, C Programming are examples of Structured Programming Language. C is Structured/ Procedure/ Modular Programming Language because C language is divided into small logical functions or structures. C / Computer programming is the process of designing and building an executable computer program for completing a specific computing task. C Programming is a Middle Level and General – purpose programming language that is used for developing portable applications. C Programming is a language developed in 1972 by Dennis M. Ritchie at the bell telephone company. CHARACTERISTICS OF C LANGUAGE: Simple and clear Small in Size Structured Language Middle Level Language Portable Modularity Case Sensitive Easy Error Detection II. BASIC STRUCTURE OF C PROGRAM Documentation Section Link Section Definition Section Global Declaration Section main () Function Section { Declaration part Executable part } Subprogram section { Function 1 Function 2 … Function n } 1. Documentation Section This Section Consists of a Set of Comment Line Giving the Name of Program. 2. Link Section Provide instruction to the compiler to link functions from the system. (#include) 3. Definition Section Definition Section is used to defines all Symbolic Constants (Like #define 3.14) 4. Global Declaration Section Used to declare variables globally (that is outside of all the functions). This section also declares the entire user defined function. 5. main () Function Section Every C program must have one main() function. This Section contains two parts; - Declaration part declares all the variables used in the executable part. - These two parts must be appearing between opening and closing braces. 6. Subprogram Section The Subprogram Section contains all the user defined functions that are called in the main function. Example of Structure of C Program #include //Header Files #include void main () // Main Function Section { printf (“Hello world”); body of main function } Output of Program: Hello world The #includeis a Header File Which is Necessary Standard Input / Output. It includes all the Input / Output functions in this file and hence it must be included. In above program printf () is define by header file in The main() is a Special Function in Every C Program Which Shows the Entry Point in C Program. The word void written before main() means it Returns Nothing. so, the main() does not return any value. The empty pair of round brackets “( )” (parentheses) after word main indicates that main is a function and it has no arguments. The left curly bracket “{” shows the start of the main function and the right curly bracket “}” shows end of the function. printf( ) function is used to display or print any message on the output screen. The semicolon “;” at the end of the statement shows the termination of the statement. III. STANDARD DIRECTORIES IN C: The C Standard Directories / Libraries provides many inbuilt functions,string handling functions, math functions, macros, input/output processing functions, memory management and many more services. is Mandatory Header file in C programming; is used to call printf( ) and scanf( ) functions. is optional; and it’s used to call getch( ) and clrscr( ) type of function. IV. BENEFITS / ADVANTAGES OF C PROGRAMMING I. Middle Level Language As a C Middle level language, C combines the features of both high level and low-level language. II. Building block for many other Programming Languages Many Programming languages such as Python, C++, JAVA etc are built with the base of the C language. III. Powerful and Efficient Language C is a Robust Language (Means Strong Memory Management) as it contains many data type and operators to perform all kinds of operations. IV. Portable Language C is a Very Flexible and Machine Independent Language that helps you to run your code in any Machine. V. Built- in Functions There are 32 keywords and Many built-in-functions which help you to build a program. VI. Structured – Programming Language C is structured – based. It means the issues or complex problems are divided into smaller blocks or functions, also called modular structure programming. V. CHARACTER SET IN C The character used in a C language is divided into four categories. Letters or Alphabets Digits Special Characters White Spaces The letters include the uppercase (A,B,C….Z) and lowercase(a,b,c….z) alphabets. The digits include(0,1,2….9). The special characters link semicolon(;), bracket { }, single quote(‘ ’), double quotes (“ ”), +, - *, /, %, , = etc. The white spaces are used to separate the words or tokens like tab(\t) and newline(\n). VI. TOKENS IN C Tokens are the smallest elements of a program, which are meaningful to the compiler. Example of Tokens, Keywords or datatype like int, float Operators (i.e. +, -, *, /, %) Punctuation marks like Semicolon (;) bracket { } etc. Constants of a program are also considered as tokens. VII. KEYWORDS AND CONSTANT KEYWORDS: The keywords are special words used in C programming having specific meaning and the meaning of the words cannot be changed. They are also known as the “Reserve Words”. There are main 32 Keywords in C Programming: CONSTANTS IN C: auto extern sizeof double break float static else case for struct enum continue goto switch return char if typedef short const int union signed default long unsigned volatile do register void while In C programming constants are the value which never changes, C uses the following type of constants. Numeric constants Non-Numeric constants Integer Real Character String Integer Constants : Represents the whole numbers. They use digits 0 to 9 and optional sign + or – before the number and space, comma and other symbols are not allowed. Valid Integer constants: 123, -1223, 4564, 0, +34 Invalid constants: 10,000, $50, 15 450 Real Constants: Real constants represent the decimal numbers i.e. digit after decimal point. Example: 0.005, -0.123, +456.0, 215.78 Real constants can be written in specific format also (i.e., scientific) Example: 6.8et4, -8.g2E-1, 1.7et2 Character Constants: Character constants represent a single character and they are always enclosed in single quotes. The characters are stored in computer memory in the form of their ASCII values. Example: ‘A’ , ‘a’ , ‘:’ , ‘&’(ASCII value of ‘A’ is 65 and ‘a’ is 97) The C language also uses the special characters known as escape sequences. (i.e. \n, \b, \v, \t, \o etc (backslash (\) String Constants: String constant is a sequence of characters and it is enclosed in double quotes. The strings are used to represent the name, address, city, name etc. Example: “ABC”, “1998” , “Hello word” VIII.IDENTIFIERS & VARIABLES IN C IDENTIFIERS: Identifiers are used for naming of variables, functions, array etc. These are User Defined names which Consist of Alphabets, Number, Underscore. Identifier’s names should not be the same and Keywords are not used as Identifiers. The C is Case Sensitive and hence the uppercase and lowercase letters are not treated as Same (for example, a and A are different) VARIABLES: Variables are a Placeholder which is used to store Data Temporary. C Variable is a named location in a memory where a program can update / manipulate the data. The value of the variable may get change in program. Each Variable is identified by Unique Identifier. C Variable might belong to any of the data types like int, float, char etc. DATATYPES IN C : Data Type in a C refers to the type of a variable or used for declaring variable or Function. Data Type determines how much space is occupied in storage. There is a basic data type supported by C. Type Size in bytes Range char 1 -128 to +128 int 2 -32768 to +32768 float 4 3.4e-38 to 3.4et38 double 8 1.7e-308 to 1.7et308 The C provides different qualifiers like signed, unsigned, short and long. By applying the qualifiers to the basic data types, we can change the range of our data type as per the need. Data Types: Primary Data Types, Derived Data Types (int, char, float, double) (Array, Structure, Union, Pointer) Type Size in byte Range char(signed char) 1 -128 to +128 Unsigned char 1 0 to 255 int(signed int) 2 -32768 to +32768 Unsigned int 2 0 to 65535 short int (signed short 2 -32768 to +32767 int) Unsigned short int 2 0 to 65535 long int (signed long 4 -21474865463 to int) +21474865463 unsigned long int 4 0 to 4248514 float 4 3.4e-38 to 3.4e+38 double 8 1.7e-308 to 1.7e+308 long double 10 3.4e-4932 to 1.1e+4932 The Exact int type depends on the range in which your values are. The C supports float and double type for storing the Real values (decimal values) RULES FOR VARIABLE NAME : - Only uppercase and lowercase letters, digits and underscore can be used. - Variable names always begin with letters and space is not valid in variable names. - Only first 32 characters are significant. - Keywords can not be used. - Variable names are case sensitive. The count and Count are two different. - The underscore character is used to separate the word in a variable. DECLARATION AND INITIALIZATION OF VARIABLE: DECLARATION OF VARIABLE The variables are used to store the data values, before storing a value into the variable it must be declared. Syntax: Datatype Variable1, Variable2,........, Variable n; Here, Datatype is one of the valid Datatype of the C and followed by the list of variables Separated by commas. The Variable1,Variable2,.....Variable n are name of variable. The semicolon(;) shows the end of the declaration statements. Example: int max; float a,b,c; INITIALIZATION OF VARIABLE After declaring variables, they are assigned values or initialize using assignment operator(=). Syntax Variable_name = Value; Example: max=10; a=2.5; It is also Possible to assign the value to the variable (initialize) at the & time of declaration with Following Format. Syntax: data_type variable_name=constant / value; Example: int length=5; AIM: Write a Program to Demonstrate the Variable Declaration, Initialization and print it on the output screen. Program: #include #include void main() { char c; //declaration of character variable int i; //declaration of integer variable float f; //declaration of float variable double d; //declaration of double variable clrscr(); c=’N’; //Value Initialization to c variable i=123; //Value Initialization to i variable f=10.23; //Value Initialization to f variable d=345.6789; //Value Initialization to d variable printf(“%c”,c); printf(“%d”,i); printf(“%f”,f); printf(“%f”,d); getch(); } Output: N 123 10.23 345.6789 DYNAMIC INITIALIZATION: Dynamic Initialization of Variable refers to initializing the variables at run time. i.e; Value of the variable is provided during run time. In C we can take value from the user with the use of scanf( ) function. Syntax: scanf(“Format_Specifier” , &Variable_Name); Example: scanf(“%d”,&a); AIM: Write a Program to Read Two integer Value from user and perform addition of that two numbers. #include #include void main() { int a,b,sum; clrscr(); printf(“Enter the value of a:”); scanf(“%d”,&a); printf(“Enter the value of b:”); scanf(“%d”,&b); sum=a+b; printf(“\nAddition of a and b is : ”,sum); getch(); } Output: Enter the value of a: 10 Enter the value of b: 20 Addition of a and b is : 30 TYPE CONVERSION AND MODIFIERS IN C: Type Conversion refers to changing a variable of one data type into another. When we want to change the datatype of a variable then type conversion is used. Type conversion in C can be classified into the following two types: 1. Implicit Type Conversion 2. Explicit Type Conversion 1. Implicit Type Conversion: It is also called Automatic Conversion. When the type conversion is performed automatically by the compiler without programmer’s intervention, such type conversion is known as implicit type conversion. It is use to convert smaller data type into larger data type and it is also called “Type Promotion”. The implicit type conversion always happens with the compatible data type. (Small Data Type -------into-------→ Large Data Type) (int----------------into--------→float) Example 1 #include #include void main() { short int a=10; int b; b = a; //storing value of a into b printf(“Value of a is: %d\n”,a); printf(“Value of b is: %d\n”,b); getch(); } Output: Value of a is:10 Value of b is:10 Example 2 #include #include void main() { int no 1=12; float no2=2.3; mul=no1*no2; printf(“Multiplication of number1 and number2 is : %f”,mul); getch(); } Output: Multiplication of number1 and number2 is : 27.59998 2. Explicit Type Conversion: It is also called Forcefully Conversion, which converts explicitly to another type. It is used to convert large data type to smaller one. (So, there is a chance of data loss) It is also known as Type Casting There is a data loss, because the conversion is done forcefully. Like, int to short (1.85->1) (0.85) is data lost float to int Larger Datatype → Smaller Datatype Syntax: (datatype)expression Example div=(float)a/b; Example 1 #include #include void main() { clrscr(); int a=25; int b=12; float div; div=a/b; printf(“Without Explicit conversion a/b is : %f”,div); div=(float)a/b; printf(“With Explicit conversion a/b is :%f”,div); getch(); } Output: Without Explicit conversion a/b is: 2.000000 With Explicit conversion a/b is: 2.083333 USE OF CONSTANT AND VOLATILE VARIABLE: CONSTANTS IN C We can use constant with two methods: 1. Symbolic Constant in C 2. Constants with const keyword 1. Symbolic Constant in C C Constants are also like normal variables but only difference is, their values cannot be modified by the program once they are defined. Constants may belong to any of the data type. Constants refer to fixed values. They are also called literals. Symbolic constants may repeat several times in a program. The well-known constant pie is an example of symbolic constants. (as we use pi as 3.14 value at many places in the program). Syntax #define Variable_name Constant_value Example #define PIE 3.14 After defining a variable for a constant, we can use this value at every place where constant is used. The Main Advantage of symbolic constant is that if we want to change the value of constant, we need to change it at only one place in the definition section. The compiler automatically uses new values at each place. Normally, the uppercase letters are used for defining symbols to differentiate them from the normal variable. AIM: Write a Program with the use of Symbolic Constants #include #include #define PIE 3.14 void main() { float r, area; clrscr(); printf(“Enter the radius :\n”); scanf(“%f”,&r); area=PIE*r*r; printf(“After of circle: %f\n”,area); getch(); } Output: Enter the radius: 4.5 Area of circle: 63.584 2. Constants with const keyword C provides another way to define the constants with keyword const. Constant refers to the fixed values that means that values cannot be changed. Constant can be of any of the basic data types like an integer constant, character constant, string constant, float constant etc. Syntax: const datatype constant_name=value; Where, const is a keyword / reserved word. datatype is the type of constant variable. a constant name is a name of variable just like other variable. value denotes value of that constant Example const int max=5; const float pi=3.14; AIM: Write a Program that shows the use of const keyword. #include #include void main() { clrscr(); float r, area; const float pie = 3.14; printf(“Enter the radius :\n”); scanf(“%f”,&r); area=pie*r*r; printf(“After of circle: %f\n”,area); getch(); } Output: Enter the radius: 4.5 Area of circle: 63.584 Note: If you do not Initialize the constant value at a time of declaration, automatically initialize it to 0. Example: const int a; (a is defined as constant with value 0) VOLATILE VARIABLE: C provides a volatile variable is a variable which can be modified at any time by external sources. Volatile keyword is a qualifier that is applied to a variable when it is declared. It tells a compiler that the value of a variable may change at any time-without any action being taken by code. Example: Volatile int count; Use of Volatile Variable: Accessing hardware control / data register. Mainly used at the kernel side. Global variable within a multi-threaded application. TYPES OF COMMENTS IN C: In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers In C/C++ Programming There are two types of comments; 1. Single line comment Denoted by // 2. Multi-line comment Denoted by INPUT OUTPUT STATEMENTS IN C: Input means to provide some data to the program and Output means to display some data on screen. An input/output statement or IO statement is a portion of a program that instructs a computer how to read and process information. There are many inbuilt functions for IO Statements which are defined in the stdio.h Header file. For Taking input: scanf( ) , getc( ) , getchar( ) , gets( ) etc…. For Displaying Output: printf( ) , putc( ) , putchar( ) , puts( ) etc….

Use Quizgecko on...
Browser
Browser