1.1 Introduction to C Language
Total Page:16
File Type:pdf, Size:1020Kb
1.1 Introduction to C Language 1 Department of CSE Objectives • To understand the structure of a C-Language Program • To write a minimal C program • To introduce the include preprocessor command • To be able to create good identifiers for quantities in a program • To be able to list, describe and use the basic data types in C • To be able to create and use variables and constants in a C program 2 Department of CSE Agenda • Background of C Language • Structure of a C program • C Comments • Identifiers in C • Data types in C • Variables in C • Constants in C 3 Department of CSE Background of C • C is a middle level language it combines the best elements of high-level languages with the control and flexibility of assembly language • Like most modern languages, C is also derived from ALGOL 60 (1960) • Developed by Dennis Ritchie in 1972 using many concepts from its predecessors – ALGOL,BCPL and B and by adding the concept of data types • American National Standards Institute (ANSI) began the standardization of C in 1983 which was approved in 1989 • In 1990 International Standards Organization (ISO) adopted the ANSI standard version known as C89 • Minor changes were made to C89 in 1995 which came to be known as C95 • Much more significant updates were made in 1999 and named as C99 4 Department of CSE Features of C • C is a structured programming language which allows compartmentalization of code and data • A structured language offers a variety of programming possibilities. For example, structured languages typically support several loop constructs, such as while, do-while, and for. • A structured language allows one to place statements anywhere on a line and does not require a strict field concept • In a structured language, the use of goto is either prohibited or discouraged and is not the common form of program control 5 Department of CSE Structure of a C Program Preprocessor Directives To include standard input/output library file in the program Global Declarations Describes the data which is visible to all parts of the program (optional) • main function is the starting point of every C int main (parameter list) program { • One and only one function must be named main in a C program Local Declarations •Describes the data used in the function •Visible only inside the function (optional) Statements } •Follows the local declaration section •Contains the instructions for the computer to do something (e.g., add two numbers) Other functions as required Optional functions also called modules similar to main to accomplish specific tasks 6 Department of CSE Dissecting a minimal C program #include <stdio.h> Preprocessor Directive to int main(void) include stdio.h file { main function printf(“Welcome to Computer Programming”); return 0; Instruction to print a message } Instruction to stop the program The first C program has •Preprocessor Commands • which are placed at the beginning of the program starting with # sign • can start at any column but traditionally start at column 1 • tells the compiler to include standard input/output library file (stdio.h) in the program which is needed for printing the welcome message on the screen • must be typed exactly as shown above without any space between # and include and the file name is written between < and >. 7 Department of CSE Dissecting the minimal C program • General Syntax (writing format) of a Preprocessor Directive is #include<filename.extension> Examples: #include<stdio.h> - Required for using input/ output instructions #include<string.h> - Required for using pre-defined string functions #include<math.h> - Required for using pre-defined math functions • Main function • Executable part of the program begins with main function General Syntax: int main(void) • The word int before the main indicates that the function will return an integer value to the operating system • Since in this program main does not require any parameters, the parameter list is specified as void Creating and Compiling the minimal C program • In the linux environment, locate gedit and open the editor • Type in the minimal C program exactly with all puncuations as follows #include <stdio.h> int main(void) { printf(“Welcome to Computer Programming”); return 0; } • After typing the preceding source code, save the program as welcome.c. • Instead of welcome, any name can be chosen, but the extension must be .c. • This extension is the common convention in C programs and identifies the contents of the file as C source code. • Most compilers will expect the source file to have the extension .c, and if it doesn‟t, the compiler may refuse to process it. • After saving the program, locate terminal and type cc welcome.c near the $ symbol Executing the minimal C program • If the program is present in the correct path and is error free, the compilation results in a $ being displayed right below the previous command typed • If there is a syntax error, open the file again using gedit, locate the line number indicated by the compiler and do the necessary corrections • If the compiler says file not found, change into the folder which contains the program to be compiled and repeat the compilation step • Upon successful compilation, type ./a.out at the $ prompt and the program must print Welcome to Computer Programming Summary of writing and executing a C program C Comments • Placed by the programmer in the code to help the reader to understand the meaning of sections of code which are not very clear • Comments are merely internal program documentations • Compiler ignores the comments when it translates the program into executable code • To identify a comment, C uses two different comment formats: • Line Comment • Block Comment Line Comment • The Second format, the line comment uses two slashes - // to identify a comment • This format does not require an end-of-comment token • The end of line automatically ends the comment • Programmers generally use this format for short comments • The line comment can start anywhere on a line and ends with the end of that line • Examples of Line Comment: //This is a whole line comment A=5; //This is a partial line comment Block Comment • Block comment is used when a comment spans several lines • It uses opening and closing comment tokens • Each comment token is made up of two characters without spaces in between them • The opening comment token is represented as /* • The closing comment token is represented as */ • Everything that is placed in between /* and */ is ignored by the compiler • The block comments can start in any column and they need not both be on the same line • The only restriction is that the opening token /* must precede the closing token */ Block Comment - Examples /* This is a block comment that covers one line*/ /* This is a block comment that covers two lines*/ /* **Block comments can also be designed with the opening **comment on a separate line and closing comment on a **separate line. Some programmers also put at the beginning of **each line in between to clearly mark the documentation part. */ Caution with Comments • Comments cannot be nested i.e., comments cannot be given inside comments • Example: • /* This is an /*inner comment*/outer comment*/ ignored Matches the first /* Left without an opening /* • Once the compiler sees an opening block comment token /*, it ignores everything it sees until it finds a closing token */ • Therefore, the opening token of the nested comment is not recognized and the closing token of the nested comment matches the outer opening comment thereby leaving the closing token of the outer comment without a matching opening token Minimal C program revisited with comments /* Welcome to the minimal C program. This program demonstrates a small C program Written by: name Date: program written date */ #include <stdio.h> // this is a preprocessor directive int main(void) // this is the main function { //main begins here //No local declarations needed for this simple program //Statements printf(“Welcome to Computer Programming”); //This is a print statement return 0; //returns the control back to the operating system } //main ends here The C Character Set • Character set specifies the valid symbols that can be used in a C program • The C character set consists of the following symbols and escape sequence characters Escape Sequence characters • Escape sequence characters are a ASCII Character Escape representation combination of two characters but null character „\0‟ treated as a single character. alert (bell) „\a‟ • Often used in printf statements,they are not printed explicitly on the backspace „\b‟ screen but the effect of these horizontal tab „\t‟ characters can be observed. newline „\n‟ • The table beside shows some ASCII vertical tab „\v‟ characters and their corresponding escape character format form feed „\f‟ carriage return „\r‟ single quote „\‟‟ double quote „\”‟ backslach „\\‟ Exercises • Write a program that will output your name and address using a separate printf() statement for each line of output • Write a program to print the pattern of asterisks as shown below • Write a program to print the following figure using suitable characters Solutions to Exercises #include<stdio.h> int main() { printf("Raghesh Krishnan K, "); printf("Asst.Professor, CSE Dept, "); printf("Amrita School of Engineering, "); printf("Amrita Vishwa Vidyapeetham, "); printf("Amrita Nagar Post, "); printf("Coimbatore 641 112 "); return 0; } Solutions to Exercises //Prints a triangular pattern - Author: Raghesh Krishnan K #include<stdio.h> int main() { printf("*\n");