C Programming Lecture Notes (Till Format Specifiers) PDF

Document Details

SmittenLeibniz9035

Uploaded by SmittenLeibniz9035

Dr. Homi Bhabha State University

2024

Shivkumar Chandey

Tags

c programming computer programming programming language computer science

Summary

This document provides lecture notes on C programming, covering its history, evolution, applications, why to learn it, and fundamental concepts such as data types and format specifiers. The document is suitable for undergraduate-level computer science students.

Full Transcript

C Programming ~ by Mr. Shivkumar Chandey, Coordinator, Department of B.Sc. (AI & ML, CS & DF, DS & A) Dr. Homi Bhabha State University History of C Know C Programming ✓ C programming is a general-purpose, procedural, imperative computer programming language develo...

C Programming ~ by Mr. Shivkumar Chandey, Coordinator, Department of B.Sc. (AI & ML, CS & DF, DS & A) Dr. Homi Bhabha State University History of C Know C Programming ✓ C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. ✓ It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers. 7/31/2024 3 Know C Programming ✓ C programming is considered as the base for other programming languages, that is why it is known as mother language. It can be defined by the following ways: ✓ Mother language ✓ System programming language ✓ Procedure-oriented programming language ✓ Structured programming language ✓ Mid-level programming language 7/31/2024 4 Evolution of Unix ✓ In 1969, a team of developers of Bell Labs started a project to make a common software for all the computers and named it as 'Unix’. ✓ It was simple and elegant, used 'C' language instead of assembly language and its code was recyclable. 7/31/2024 5 Evolution of Unix ✓ As it was recyclable, a part of its code now commonly called 'kernel' was used to develop the operating system and other functions and could be used on different systems. Also its source code was open source. ✓ Initially, Unix was only found in large organizations like government, university, or larger financial corporations with mainframes and minicomputers (PC is a microcomputer). 7/31/2024 6 Why to Learn C Programming? ✓ C programming language is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Software Development Domain. 7/31/2024 7 Why to Learn C Programming? Here are some of the important reasons why you should learn C Programming − ✓ It is a structured programming language and you can use the skills learned in C to master other programming languages. ✓ You can use C program to write efficient codes and develop robust projects. ✓ C is a low-level language and you can use it to interact more directly with the computer's hardware and memory. 7/31/2024 8 Unix Expansion ✓ But his project failed in gaining popularity. ✓ Many other Unix like operating system came into existence but none of them was able to gain popularity. 7/31/2024 9 Facts about C ✓ C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C. Here are some facts about the C language: ✓ C was invented to write an operating system called UNIX. The UNIX OS was totally written in C. ✓ C is a successor of B language which was introduced around the early 1970s. ✓ The language was formalized in 1988 by the American National Standard Institute (ANSI). 7/31/2024 10 Hello World using C Programming ✓ Just to give you a little excitement about C programming, I'm going to give you a small conventional C Programming Hello World program. 7/31/2024 11 #include int main() { printf("Hello, World! \n"); return 0; } To write, compile, and run your first C program, follow these steps:  Step 1: Open a text editor  Step 2: Write the C program  Step 3: Save the file with a.c extension such as first_program.c  Step 4: Compile the program  Step 5: Run the program  After successful compilation, you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt  The program will execute, and you will see the output on the console. What is a compilation?  The compilation is a process of converting the source code into object code. It is done with the help of the compiler.  The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. Applications of C Programming ✓ C was initially used for system development work, particularly the programs that make-up the operating system. ✓ C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. ✓ Some examples of the use of C are - 7/31/2024 17 Applications of C Programming ✓ Operating Systems ✓ Language Compilers ✓ Assemblers ✓ Text Editors ✓ Print Spoolers ✓ Network Drivers ✓ Modern Programs ✓ Databases ✓ Language Interpreters ✓ Utilities 7/31/2024 18 Features of C Programming 7/31/2024 19 Advantages of C Language ✓ The following are the advantages of C language 7/31/2024 20 Efficiency and speed ✓ C is known for being high−performing and efficient. ✓ It can let you work with memory at a low level, as well as allow direct access to hardware, making it ideal for applications requiring speed and economical resource use. 7/31/2024 21 Portable ✓ C programs can be compiled and executed on different platforms with minimal or no modifications. ✓ This portability is due to the fact that the language has been standardized and compilers are available for use on various operating systems globally. 7/31/2024 22 Close to Hardware ✓ C allows direct manipulation of hardware through the use of pointers and low−level operations. ✓ This makes it suitable for system programming and developing applications that require fine-grained control over hardware resources. 7/31/2024 23 Standard Libraries ✓ For common tasks such as input/output operations, string manipulation, and mathematical computations, C comes with a large standard library which helps developers write code more efficiently by leveraging pre−built functions. 7/31/2024 24 Structured Programming ✓ C helps to organize code into modular and easy−to−understand structures. ✓ With functions, loops, and conditionals, developers can produce clear code that is easy to maintain. 7/31/2024 25 Procedural Language ✓ C follows a procedural paradigm that is often simpler and more straightforward for some types of programming tasks. 7/31/2024 26 Versatility ✓ C language is a versatile programming language and it can be used for various types of software such as system applications, compilers, firmware, application software, etc. 7/31/2024 27 Drawbacks of C Language ✓ The following are the disadvantages/drawbacks of C language − 7/31/2024 28 Manual Memory Management ✓ C languages need manual memory management, where a developer has to take care of allocating and deallocating memory explicitly. 7/31/2024 29 No Object−Oriented Feature ✓ Nowadays, most of the programming languages support the OOPs features. But C language does not support it. 7/31/2024 30 No Garbage Collection ✓ C language does not support the concept of Garbage collection. ✓ A developer needs to allocate and deallocate memory manually and this can be error-prone and lead to memory leaks or inefficient memory usage. 7/31/2024 31 No Exception Handling ✓ C language does not provide any library for handling exceptions. ✓ A developer needs to write code to handle all types of expectations. 7/31/2024 32 Sr. Key OOP POP No. OOP stands for Object POP stands for Procedural 1 Definition Oriented Programing. Oriented Programming. OOP follows bottom up POP follows top down 2 Approach approach. approach. A program is divided to A program is divided into 3 Division objects and their funtions and they interacts. interactions. Inheritance Inheritance is not 4 Inheritance is supported. supported supported. Access Access control is supported No access modifiers are 5 control via access modifiers. supported. Encapsulation is used to No data hiding present. 6 Data Hiding hide data. Data is globally accessible. 7 Example C++, Java C, Pascal printf() and scanf() in C ✓ The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file). ✓ The printf() function is used for output. It prints the given statement to the console. ✓ The syntax of printf() function is given below: printf("format string",argument_list); The format string can be %d (integer), %c (character), %s (string), %f (float) etc. 7/31/2024 34 scanf() function ✓ The scanf() function is used for input. It reads the input data from the console. scanf("format string",argument_list); 7/31/2024 35 Variables in C ✓ A variable is the name of the memory location. ✓ It is used to store information. ✓ Its value can be altered and reused several times. ✓ It is a way to represent memory location through symbols so that it can be easily identified. 7/31/2024 36 Variables in C ✓ Variables are key building elements of the C programming language used to store and modify data in computer programs. ✓ A variable is a designated memory region that stores a specified data type value. Each variable has a unique identifier, its name, and a data type describing the type of data it may hold. ✓ Syntax: data_type variable_name; 7/31/2024 37 Rules for declaring variables in C The general rules for naming variables are: ✓ Names can contain letters, digits and underscores ✓ Names must begin with a letter or an underscore (_) ✓ Names are case-sensitive (myVar and myvar are different variables) ✓ Names cannot contain whitespaces or special characters like !, #, %, etc. ✓ Reserved words (such as int) cannot be used as names 7/31/2024 38 Valid and Invalid Variable Names Valid examples of variable Invalid examples of variable  int age;  int 1stNumber; // Starts with a digit  float salary;  float my-salary; // Contains a hyphen (-)  char _status;  char int; // Same as a C keyword  double average_score;  int double; // Same as a C keyword  int studentCount;  float my$var; // Contains an unsupported special character Keywords in C  Keywords in C are reserved words that have predefined meanings and are part of the C language syntax.  These keywords cannot be used as variable names, function names, or any other identifiers within the program except for their intended purpose.  They are used to define the structure flow and behavior of a C program. 7/31/2024 40 Keywords in C auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while 7/31/2024 41 Data Types in C ✓ A data type specifies the type of data that a variable can store such as integer, floating, character, etc. 7/31/2024 42 Data Types in C Types Data Types Basic Data Type int, char, float, double Derived Data Type array, pointer, structure, union Enumeration Data Type enum Void Data Type void 7/31/2024 43 Basic Data Types ✓ The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals. ✓ The memory size of the basic data types may change according to 32 or 64-bit operating system. ✓ Its size is given according to 32-bit architecture. 7/31/2024 44 Data Types Memory Size Range char 1 byte −128 to 127 signed char 1 byte −128 to 127 unsigned char 1 byte 0 to 255 short 2 byte −32,768 to 32,767 signed short 2 byte −32,768 to 32,767 unsigned short 2 byte 0 to 65,535 int 2 byte −32,768 to 32,767 signed int 2 byte −32,768 to 32,767 unsigned int 2 byte 0 to 65,535 short int 2 byte −32,768 to 32,767 signed short int 2 byte −32,768 to 32,767 unsigned short int 2 byte 0 to 65,535 long int 4 byte -2,147,483,648 to 2,147,483,647 signed long int 4 byte -2,147,483,648 to 2,147,483,647 unsigned long int 4 byte 0 to 4,294,967,295 float 4 byte double 8 byte long double 10 byte Derived Data Type ✓ Beyond the fundamental data types, C also supports derived data types, including arrays, pointers, structures, and unions. ✓ These data types give programmers the ability to handle heterogeneous data, directly modify memory, and build complicated data structures. 7/31/2024 46 Array ✓ An array, a derived data type, lets you store a sequence of fixed-size elements of the same type. ✓ It provides a mechanism for joining multiple targets of the same data under the same name. 7/31/2024 47 Array ✓ The index is used to access the elements of the array, with a 0 index for the first entry. ✓ The size of the array is fixed at declaration time and cannot be changed during program execution. ✓ The array components are placed in adjacent memory regions. 7/31/2024 48 #include int main() { int numbers; // Declares an integer array with a size of 5 elements // Assign values to the array elements numbers = 10; numbers = 20; numbers = 30; numbers = 40; numbers = 50; // Display the values stored in the array printf("Values in the array: "); for (int i = 0; i < 5; i++) { printf("%d ", numbers[i]); } printf("\n"); return 0; } Pointer ✓ A pointer is a derived data type that keeps track of another data type's memory address. ✓ When a pointer is declared, the data type it refers to is stated first, and then the variable name is preceded by an asterisk (*). 7/31/2024 50 Pointer ✓ You can have incorrect access and change the value of variable using pointers by specifying the memory address of the variable. ✓ Pointers are commonly used in tasks such as function pointers, data structures, and dynamic memory allocation. 7/31/2024 51 #include int main() { int num = 42; // An integer variable int *ptr; // Declares a pointer to an integer ptr = # // Assigns the address of 'num' to the pointer // Accessing the value of 'num' using the pointer printf("Value of num: %d\n", *ptr); return 0; } Structure ✓ A structure is a derived data type that enables the creation of composite data types by allowing the grouping of many data types under a single name. ✓ It gives you the ability to create your own unique data structures by fusing together variables of various sorts. ✓ A structure's members or fields are used to refer to each variable within it. 7/31/2024 53 Structure ✓ Any data type, including different structures, can be a member of a structure. ✓ A structure's members can be accessed by using the dot (.) operator. ✓ A declaration and use of a structure is demonstrated here: 7/31/2024 54 #include #include // Define a structure representing a person struct Person { char name; int age; float height; }; int main() { // Declare a variable of type struct Person struct Person person1; // Assign values to the structure members strcpy(person1.name, "John Doe"); person1.age = 30; person1.height = 1.8; // Accessing the structure members printf("Name: %s\n", person1.name); printf("Age: %d\n", person1.age); printf("Height: %.2f\n", person1.height); return 0; } Union ✓ A derived data type called a union enables you to store various data types in the same memory address. ✓ In contrast to structures, where each member has a separate memory space, members of a union all share a single memory space. ✓ A value can only be held by one member of a union at any given moment. 7/31/2024 56 Union ✓ When you need to represent many data types interchangeably, unions come in handy. ✓ Like structures, you can access the members of a union by using the dot (.) operator. 7/31/2024 57 Enumeration Data Type ✓ A set of named constants or enumerators that represent a collection of connected values can be defined in C using the enumeration data type (enum). ✓ Enumerations give you the means to give names that make sense to a group of integral values, which makes your code easier to read and maintain. 7/31/2024 58 C Format Specifier ✓ The Format specifier is a string used in the formatted input and output functions. ✓ The format string determines the format of the input and output. ✓ The format string always starts with a '%' character. 7/31/2024 59 Format specifier Description %d or %i It is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values. %u It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value. %o It is used to print the octal unsigned integer where octal integer value always starts with a 0 value. %x It is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc. %X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc. %f It is used for printing the decimal floating-point values. By default, it prints the 6 values after '.'. %e/%E It is used for scientific notation. It is also known as Mantissa or Exponent. %g It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output. %p It is used to print the address in a hexadecimal form. %c It is used to print the unsigned character. %s It is used to print the strings. %ld It is used to print the long-signed integer value. Any Queries or Doubts…??? Ask your queries on: Scan the QR Code to connect [email protected] Or +91 9987389441 Or Connect on LinkedIn by scanning the QR Code

Use Quizgecko on...
Browser
Browser