Computer Programming – I (24CP101T) Lecture Notes PDF

Document Details

UndauntedMagenta279

Uploaded by UndauntedMagenta279

Pandit Deendayal Energy University

Dr. Rajeev Kumar Gupta

Tags

C programming computer programming programming languages computer science

Summary

These lecture notes cover introductory concepts in Computer Programming – I (24CP101T). The document details basic concepts and examples in C Programming. Topics covered include C language introduction, disadvantages, applications, and basic syntax.

Full Transcript

Computer Programming – I (24CP101T) Dr. Rajeev Kumar Gupta Assistant Professor Pandit Deendayal Energy University Gandhinagar, Gujarat 1 ...

Computer Programming – I (24CP101T) Dr. Rajeev Kumar Gupta Assistant Professor Pandit Deendayal Energy University Gandhinagar, Gujarat 1 C Language Introduction  C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs.  It was mainly developed as a system programming language to write the UNIX operating system.  Why Learn C?  C is called the mother of all modern programming languages so learning C will help you to learn other languages easily like Java, C++, C#, Python, etc.  C language is faster than other programming languages like Java and Python.  It can handle low-level programming and we can compile the C code in a variety of computer platforms. 17 Disadvantages 1) Steep learning curve: C can be difficult to learn, especially for beginners, due to its complex syntax and low-level access to system resources. 2) Lack of memory management: C does not provide automatic memory management, which can lead to memory leaks and other memory-related bugs if not handled properly. 3) No built-in support for object-oriented programming: C does not provide built-in support for object-oriented programming, making it more difficult to write object-oriented code compared to languages like Java or Python. 4) No built-in support for concurrency: C does not provide built-in support for concurrency, making it more difficult to write multithreaded applications compared to languages like Java or Go. 5) Security vulnerabilities: C programs are prone to security vulnerabilities, such as buffer overflows, if not written carefully. 18 Applications of C Language  C was known as a system development language because the code written in C runs as fast as the code written in assembly language.  Operating Systems  Language Compilers  Assemblers  Text Editors  Network Drivers  Modern Programs  Databases  Language Interpreters  Utilities 19 A First Program with Basic Syntax Header files for necessary functions. #include Stdio.h provides input and output facility Main function declaration void main() As of now, entry point of our code { printf(“Hello World.\n"); } Instruction to print something on output window Give your attention to semicolon open and close braces mark the beginning and end C Program Structure  Program defined by: Preprocessor Directives  global declarations  function Global Declarations definitions Function Definitions  May contain int main () { preprocessor Local Declarations directives Statements  Always has one function named } main, may contain others Parts of a Program scanf(“%d%d”, &x,&y); Common printf( ) Conversions  %d -- the int argument is printed as a decimal number  %u -- the int argument is printed as an unsigned number  %s -- prints characters from the string until ‘\0’ is seen or the number of characters in the (optional) precision have been printed (more on this later)  %f -- the double argument is printed as a floating point number  %x, %X -- the int argument is printed as a hexadecimal number (without the usual leading “0x”)  %c - the int argument is printed as a single character  %p - the pointer argument is printed (implementation dependent) printf( ) Examples int anInt = 5678; double aDouble = 4.123; #define NAME “Bob” printf (“%d is a large number\n”, anInt); printf (“%8d is a large number\n”, anInt); printf (“%-8d is a large number\n”, anInt); printf (“%10.2f is a double\n”, aDouble); printf( “The sum of %d and %8.4f is %12.2f\n”, anInt, aDouble, anInt + aDouble); printf (“Hello %s\n”, NAME); printf("%p",&anInt); //address Comments Data Types  To store data the program must reserve space which is done using datatype. A datatype is a keyword/predefined instruction used for allocating memory for data. 25 The derived data types are basically derived out of the fundamental data types. A derived data type won’t typically create a new data type – but would add various new functionalities to the existing ones instead. 26  The memory size of basic data types may change according to 32 or 64 bit operating system. Let‟s see the basic data types. Its size is given according to 32 bit architecture. 27 Variables  A variable is a name of memory location. It is used to store data. Variables are changeable, we can change value of a variable during the execution of a program.. It can be reused many times.  Note: Variable are nothing but identifiers. All rules that are used to define identifiers are also applicable to define variable.  Declaration of Variables :  Assigning values to variables 28 Memory Map 0000 Every variable is 0001 mapped to a particular 8000 memory address 800 800 1 32 2 C Variables in Memory Instruction executed Memory location allocated to a variable X X = 10 T i X = 20 10 m e X = X +1 X = X*5 Variables in Memory Memory location allocated Instruction executed to a variable X X = 10 T i X = 20 20 m e X = X +1 X = X*5 Variables in Memory Memory location allocated Instruction executed to a variable X X = 10 T i X = 20 21 m e X = X +1 X = X*5 Variables in Memory Memory location allocated Instruction executed to a variable X X = 10 T i X = 20 105 m e X = X +1 X = X*5 Variables (contd.) X = 20 20 X Y=15 X = Y+3 ? Y Y=x/6 Variables (contd.) X = 20 20 X Y=15 X = Y+3 15 Y Y=x/6 Variables (contd.) X = 20 18 X Y=15 X = Y+3 15 Y Y=x/6 Variables (contd.) X = 20 18 X Y=15 X = Y+3 3 Y Y=X/6

Use Quizgecko on...
Browser
Browser