Unit-2 C Program Basics PDF

Document Details

ExaltingSunflower7925

Uploaded by ExaltingSunflower7925

Marwadi University

Ritu Meghani

Tags

C programming programming languages computer science software development

Summary

These notes provide an introduction to C programming, covering topics like the history of C, characteristics of the language, its uses in programming software, and various structural elements of a C program. Detailed explanations of different data types and the declaration, initialization, and usage of variables and constants are included. An overview of tokens and simple examples are also presented.

Full Transcript

UNIT - 2 Introduction to C “Your Life Begins when you get out of your comfort zone.” – Asst. Prof. Ritu Meghani HISTORY OF C C was developed in 1972 by Dennis Ritchie at Bell Laboratories. C was initially developed for writing system software. Today, C has b...

UNIT - 2 Introduction to C “Your Life Begins when you get out of your comfort zone.” – Asst. Prof. Ritu Meghani HISTORY OF C C was developed in 1972 by Dennis Ritchie at Bell Laboratories. C was initially developed for writing system software. Today, C has become a popular language and various software programs are written using this language. “Your Life Begins when you get out of your comfort zone.” Characteristics of C/Features A high level programming language Small size. C has only 32 keywords. This makes it relatively easy to learn Makes extensive use of function calls Stable language Quick language “Your Life Begins when you get out of your comfort zone.” Characteristics of C/Features C is a core language C is a portable language (platform independent language) C is an extensible language “Your Life Begins when you get out of your comfort zone.” USES OF C C language is primarily used for system programming. The portability, efficiency, the ability to access specific hardware addresses and low runtime demand on system resources makes it a good choice for implementing operating systems and embedded system applications. “Your Life Begins when you get out of your comfort zone.” STRUCTURE OF A C PROGRAM 1. Documentation Block 2. Preprocessor Directives Block 3. Global Declarations Block STRUCTURE OF A C PROGRAM 4. Main Block main() { Local declarations Statements } 5. Function Block Function 1() Function N() { { Local Local declarations declarations Statements Statements } } STRUCTURE OF A C PROGRAM 1. Documentation Block This block is also known as comment block in which programmer can write definition of program and other details related to that program. This part is an optional. There are two ways to insert comment in C program. (1) Single line Comment using // (2) Multi line Comment using “Your Life Begins when you get out of your comfort zone.” STRUCTURE OF A C PROGRAM 2. Preprocessor Directives Block This block contain special instructions that indicate how to prepare program for compilation. One of the most important and commonly used preprocessor command is include which tells the compiler that to execute the program, some information is needed from the specific header file. “Your Life Begins when you get out of your comfort zone.” STRUCTURE OF A C PROGRAM 3. Global Declarations Block This block is used to declare all global variables which will be accessible in an entire program / in all the functions. if you declare any variable out side the main function, it is considered as a global variable. STRUCTURE OF A C PROGRAM 4. Main Block The main() function is most important function and is a part of every C program. The execution of a C program begins at this function. “Your Life Begins when you get out of your comfort zone.” STRUCTURE OF A C PROGRAM 5. Function Block A function is a subprogram or block of statement which performs a particular task. Generally there are two types of function. 1. Library or Built in functions 2. User Defined Function All user defined functions are declared out side the main function. YOUR FIRST C PROGRAM // Program that display Welcome Message #include #include void main() { clrscr(); printf("\n Welcome to the world of C "); getch(); } FILES USED IN A C PROGRAM Files in a C program Source Header Object Executable File File File File FILES USED IN A C PROGRAM Source code file The source code file contains the source code of the program. The file extension of any C source code file is “.c”. The main() is the starting point of execution when you successfully compile and run the program. “Your Life Begins when you get out of your comfort zone.” FILES USED IN A C PROGRAM Header Files Header files contain the set of predefined standard library functions. When working with large projects, it is often desirable to make sub-routines and store them in a different file known as header file advantage of header files can be realized when : a) The programmer wants to use the same subroutines in different programs. b) The programmer wants to change, or add, subroutines, and have those changes be reflected in all other programs. “Your Life Begins when you get out of your comfort zone.” FILES USED IN A C PROGRAM Header Files Conventionally, header files names ends with a “.h” extension and its name can use only letters, digits, dashes, and underscores. While some standard header files are available in C, but the programmer may also create his own user defined header files “Your Life Begins when you get out of your comfort zone.” FILES USED IN A C PROGRAM Object Files Object files are generated by the compiler as a result of processing the source code file. Object files contain compact binary code of the function definitions. “Your Life Begins when you get out of your comfort zone.” FILES USED IN A C PROGRAM Object Files Linker uses this object file to produce an executable file (.exe file) by combining of object files together. Object files have a “.o” extension, although some operating systems including Windows and MS-DOS have a “.obj” extension for the object file. “Your Life Begins when you get out of your comfort zone.” FILES USED IN A C PROGRAM Binary Executable File The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be directly executed. On Windows operating system, the executable files have “.exe” extension. COMPILING AND EXECUTING C PROGRAMS The compilation process is done in two steps. In the first step, the preprocessor program reads the sources files as text, and produces another text file as output. In Second Step the linker combines the object file with library routines to produce final executable file. “Your Life Begins when you get out of your comfort zone.” Tokens In C programs, each word and punctuation is referred to as a token. C Tokens are the smallest building block or smallest unit of a C program. For instance, without words, you cannot create any sentence- similarly, you cannot create any program without using tokens in C language. Thus, we can also say that tokens are the building blocks or the very basic components used in creating any program in the C language. Following are considered as tokens. 1. Keywords 2. Identifiers 3. Constants 4. Special Characters 5. Operators 6. String etc. KEYWORDS C has a set of 32 reserved words often known as keywords. All keywords are basically a sequence of characters that have a fixed meaning. By convention all keywords must be written in lowercase (small) letters. “Your Life Begins when you get out of your comfort zone.” KEYWORDS 32 Keywords are as follows. 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 IDENTIFIERS Identifiers are names given to program elements such as variables, arrays, functions, structures. The identifiers are user-defined words in the C language. These can consist of lowercase letters, uppercase letters, digits, or underscores, but the starting letter should always be either an alphabet or an underscore. “Your Life Begins when you get out of your comfort zone.” RULES FOR NAMING IDENTIFIER AND RULES OF VARIABLE DECLARATION 1. It cannot include any special characters or punctuation marks (like #, $, ^, ?,., etc) except the underscore"_". 2. Keywords cannot be used as identifiers RULES FOR NAMING IDENTIFIER 3. The names are case sensitive. So, example, “FIRST” is different from “first” and “First”. 4. It must begin with an alphabet or an underscore. 5. It can be of any reasonable length. Though it should not contain more than 31 characters.  Example: Roll_no,marks,HRA,DA,etc. CONSTANTS  Constant is basically a value of a variable that does not change throughout a program. The constants remain the same, and we cannot change their value whatsoever.  EXAMPLES: 1. const int a = 10 ; 2. #define PI 3.14 SPECIAL CHARACTERS We also use some of the special characters in the C language, and all of them hold a special meaning that we cannot use for any other purpose. EXAMPLES: 1. () Simple brackets 2. [ ] Square brackets 3. (,) Comma 4. { } Curly braces 5. (*) Asterisk 6. (#) Hash/preprocessor 7. (.) Period 8. (~) Tilde NOTE : Operators and string tokens will be covered in next chapter. DATA TYPES Data types are used to store various types of data that is processed by program. Data type attaches with variable to determine the number of bytes to be allocated to variable and valid operations which can be performed on that variable. C Provides a standard, minimal set of basic data types. Some times these are called primitive data types. A data type specifies the type of data that a variable can store such as integer, floating, character, etc. BASIC DATA TYPES Keyword Size in Format Data Types Range Use Used Bytes Specifier Character char 1 %c -128 to 127 To Store Characters Integer int 2 %d -32768 to 32767 To Store integer Numbers 3.4E-38 to Floating float 4 %f To Store floating 3.4E+38 Point point numbers 1.7E-308 to Double double 8 %f To store big floating 1.7E+308 point numbers Valueless void 0 ------ Valueless ---------------------------- DATA TYPES IN C However, the void does not have these modifiers. These type modifiers changes the meaning of the basic type. The qualifier/modifiers are classified into two types (1) Size qualifier , e.g. short and long (2) Sign qualifier , e.g. signed and unsigned “Your Life Begins when you get out of your comfort zone.” DATA TYPES IN C In addition, C has four type of qualifiers/modifiers, which preceded the basic type. They are as follows : 1. short 2. long (to increase the size of int or double type) 3. signed (+ and - values) 4. unsigned (only + values) “Your Life Begins when you get out of your comfort zone.” DATA TYPES IN C SIZE IN DATA TYPE RANGE BYTES char 1 -128 to 127 unsigned char 1 0 to 255 signed char 1 -128 to 127 int 2/4 -32768 to 32767 unsigned int 2 0 to 65535 signed short int 2 -32768 to 32767 signed int 2 -32768 to 32767 short int 2 -32768 to 32767 unsigned short int 2 0 to 65535 long int 4 -2147483648 to 2147483647 unsigned long int 4 0 to 4294967295 signed long int 4 -2147483648 to 2147483647) float 3.4E-38 to 3.4E+38 (means 3.4 x 1038 ) (UPTO 7 digits it can store the 4 number) double 1.7E-308 to 1.7E+308 8 (means 1.7 x 10308) upto 15 digits it can store no) long double 10 3.4E-4932 to 1.1E+4932 Integer data type example #include int main() { int a =11252486; short int b=10000; long long c=51454456154585454; long d=499962313469; printf("a is %d \n b is %hd \n c is %lld \n d is %ld \n",a,b,c,d); return 0; } VARIABLES IN C A variable is defined as a meaningful name given to the data storage location in computer memory. When using a variable, we actually refer to address of the memory where the data is stored. C language supports two basic kinds of variables. 1. Numeric variables 2. Character Variables VARIABLES IN C 1. Numeric variables It can be used to store either integer values or floating point values. Example : int a = 10, b=-20; VARIABLES IN C 2. Character variables It can include any letter from the alphabet or from the ASCII chart and numbers 0 – 9 that are put between single quotes. char c = ‘a’; char a=‘1’ VARIABLES IN C Declaring Variables : Each variable to be used in the program must be declared. To declare a variable, specify the data type of the variable followed by its name. Example: int a; VARIABLES IN C Initializing Variables : While declaring the variables, we can also initialize them with some value. Example : int roll_no=5; CONSTANTS Constants are identifiers whose value does not change. Constants are used to define fixed values like PI so that their value does not get changed in the program even by mistake. To declare a constant, precede the normal variable declaration with const keyword and assign it a value. For example, const float pi = 3.14; CONSTANTS Constants in C Integer Floating Character String Type Point Type Type Type CONSTANTS TYPE OF CONSTANTS DATA TYPE EXAMPLE OF DATA TYPE Integer constants unsigned int 2000u, 5000U, etc. int long int, long long int 325,647 1,245,473,940 Floating-point or Real float 20.987654 constants Octal constant int Example: 013 Example: 0x90 character constants char Example: ‘X’, ‘Y’, ‘Z’ string constants char Example: “PQRS”, “ABCD” CONSTANTS 1. Integer Constant A constant of integer type consist of a sequence of digits. For Ex: 1. const int a = 1234; 2. const unsigned int b = 12U; (‘U’ or ‘u’) 3. const unsigned long int c = 1234567L; (‘L’ or ‘l’) CONSTANTS 2. Floating Point Constant Integer numbers are inadequate to express numbers that have a fraction part. A Floating point constant therefore consist of an integer part, a decimal point , fraction part and an exponent field containing e or E(e means exponent) For Ex: 0.02, +0.34,0.02E,etc. 1. const float PI=3.14; 2. const float height = 99.99; CONSTANTS 3. Character Constant The character constants are symbols that are enclosed in one single quotation. The maximum length of a character quotation is of one character only. (NOTE : Some predefined character constants exist in the C programming language, known as escape sequences. ) For example : 1. const char letter = 'A'; CONSTANTS 4. String Constant A String Constant is a sequence of characters enclosed in double quotes, and they may include letters, digits, special characters, and blank spaces. For Ex: 1. const char letters = "ABC"; CONSTANTS Declaring Constants in other way / Defining Symbolic Constant One can also use the ‘#define’ preprocessor directive to create the constants. And when we create the constants by making use of the preprocessor directive, we must define it in the very beginning of the program. It is because we must write all the preprocessor directives before the global declaration. Here is the syntax that we must use for creating a constant by making use of the ‘#define’ preprocessor directive: #define CONSTANTNAME value Let us look at an example to understand this better, 1. #define PI 3.14 2. #define height 100 3. #define letter 'A' 4. #define letters "ABC" 5. #define backslash_char '\?' CONSTANTS Rules that needs to be applied to a #define statement which defines a constant. Constant names are usually written in capital letters to visually distinguish them from other variable names which are normally written in lower case characters. Note that this is just a convention and not a rule. CONSTANTS 1. No blank spaces are permitted in between the # symbol and define keyword 2. Blank space must be used between #define and constant name and between constant name and constant value 3. #define is a pre-processor compiler directive and not a statement. Therefore, it does not end with a semi-colon. Escape Sequence Characters An escape sequence in C language is a sequence of characters that doesn't represent itself when used inside string character. Escape Sequence Characters \a Alarm or Beep \b Backspace \f Form Feed(Page Breaking in Print) \n New Line \r Carriage Return \t Tab (Horizontal) \\ Backslash \' Single Quote \" Double Quote \0 Null \nnn octal number \xhh hexadecimal number \v Vertical Tab(deprecated) \? Question Mark(deprecated) Question BANK 1 List characteristics of C language. 2 List and explain structure of C program in short. 3 Explain source file and header file in C. 4 What do you mean by Tokens in C Language ? 5 What do you mean by Identifier ? Explain the rule for giving identifier name. 6 What do you mean by Data Type ? Explain data types available in C. 7 What do you mean by variable ? Explain briefly. 8 What do you mean by constant ? Explain briefly. 9 What do you mean by Escape Sequences ? Explain briefly. 10 What do you mean by modifier ? Explain with example 11 Write a simple program to enter principal amount, interest rate and year. Count simple interest and display. (Any program can be asked from unit – 2) 53 THANK YOU

Use Quizgecko on...
Browser
Browser