Docsity

C notes with all the topics, Lecture notes of Engineering

Toggle sidebar
bg1
History of C Language
History of C language is interesting to know. Here we are going to discuss a brief history of the
c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of
AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K & R C 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
Features of C Language
C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download C notes with all the topics and more Lecture notes Engineering in PDF only on Docsity!

History of C Language History of C language is interesting to know. Here we are going to discuss a brief history of the c language. C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the c language. It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL. Let's see the programming languages that were developed before C language. Language Year Developed By Algol 1960 International Group BCPL 1967 Martin Richard B 1970 Ken Thompson Traditional C 1972 Dennis Ritchie K & R C 1978 Kernighan & Dennis Ritchie ANSI C 1989 ANSI Committee ANSI/ISO C 1990 ISO Committee C99 1999 Standardization Committee Features of C Language C is the widely used language. It provides many features that are given below.

  1. Simple
  2. Machine Independent or Portable
  3. Mid-level programming language
  1. structured programming language
  2. Rich Library
  3. Memory Management
  4. Fast Speed
  5. Pointers
  6. Recursion
  7. Extensible 1) Simple C is a simple language in the sense that it provides a structured approach (to break the problem into parts), the rich set of library functions , data types , etc. 2) Machine Independent or Portable Unlike assembly language, c programs can be executed on different machines with some machine specific changes. Therefore, C is a machine independent language. 3) Mid-level programming language Although, C is intended to do low-level programming. It is used to develop system applications such as kernel, driver, etc. It also supports the features of a high-level language. That is why it is known as mid-level language. 4) Structured programming language C is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify. Functions also provide code reusability. 5) Rich Library C provides a lot of inbuilt functions that make the development fast. 6) Memory Management It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function. 7) Speed The compilation and execution time of C language is fast since there are lesser inbuilt functions and hence the lesser overhead.

By menu Now click on the compile menu then compile sub menu to compile the c program. Then click on the run menu then run sub menu to run the c program. By shortcut Or, press ctrl+f9 keys compile and run the program directly. You will see the following output on user screen. You can view the user screen any time by pressing the alt+f5 keys. Flow of C Program The C program follows many steps in execution. To understand the flow of C program well, let us see a simple program first. File: simple.c #include <stdio.h> int main(){ printf("Hello C Language"); return 0; }

Execution Flow

int number; printf("enter a number:"); scanf("%d",&number); printf("cube of number is:%d ",numbernumbernumber); return 0; } Output enter a number: cube of number is: The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable. The printf("cube of number is:%d ",numbernumbernumber)** statement prints the cube of number on the console. Program to print sum of 2 numbers Let's see a simple example of input and output in C language that prints addition of 2 numbers. #include<stdio.h> int main(){ int x=0,y=0,result=0; printf("enter first number:"); scanf("%d",&x); printf("enter second number:"); scanf("%d",&y); result=x+y; printf("sum of 2 numbers:%d ",result); return 0; } Output enter first number: enter second number: sum of 2 numbers: Variables in C A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified. Let's see the syntax to declare a variable: type variable_list; The example of declaring the variable is given below: int a; float b; char c; Here, a, b, c are variables. The int, float, char are the data types. We can also provide values while declaring the variables as given below: int a=10,b=20;//declaring 2 variable of integer type float f=20.8; char c='A'; Rules for defining variables o A variable can have alphabets, digits, and underscore. o A variable name can start with the alphabet, and underscore only. It can't start with a digit. o No whitespace is allowed within the variable name. o A variable name must not be any reserved word or keyword, e.g. int, float, etc. Valid variable names: int a; int _ab; int a30; Invalid variable names: int 2; int a b; int long ; Data Types in C A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

short 2 byte āˆ’32,768 to 32, signed short 2 byte āˆ’32,768 to 32, unsigned short 2 byte 0 to 65, int 2 byte āˆ’32,768 to 32, signed int 2 byte āˆ’32,768 to 32, unsigned int 2 byte 0 to 65, short int 2 byte āˆ’32,768 to 32, signed short int 2 byte āˆ’32,768 to 32, unsigned short int 2 byte 0 to 65, long int 4 byte -2,147,483,648 to 2,147,483, signed long int 4 byte -2,147,483,648 to 2,147,483, unsigned long int 4 byte 0 to 4,294,967, float 4 byte double 8 byte long double 10 byte Keywords in C

A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There are only 32 reserved words (keywords) in the C language. A list of 32 keywords in the c language is given below: 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 C Operators An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc. There are following types of operators to perform different types of operations in C language. o Arithmetic Operators o Relational Operators o Shift Operators o Logical Operators o Bitwise Operators o Ternary or Conditional Operators o Assignment Operator o Misc Operator Precedence of Operators in C The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator direction to be evaluated; it may be left to right or right to left. Let's understand the precedence by the example given below: int value=10+20*10; The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator). Category Operator

Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in the C language.

  1. Single Line Comments
  2. Multi-Line Comments Single Line Comments Single line comments are represented by double slash \. Let's see an example of a single line comment in C. #include<stdio.h> int main(){ //printing information printf("Hello C"); return 0; } Output: Hello C Even you can place the comment after the statement. For example: printf("Hello C");//printing information Mult Line Comments Multi-Line comments are represented by slash asterisk * ... . It can occupy many lines of code, but it can't be nested. Syntax: / code to be commented / Let's see an example of a multi-Line comment in C. #include<stdio.h> int main(){ /printing information Multi-Line Comment*/ printf("Hello C"); return 0;

Output: Hello C Escape Sequence in C An escape sequence in C language is a sequence of characters that doesn't represent itself when used inside string literal or character. It is composed of two or more characters starting with backslash . For example: \n represents new line. List of Escape Sequences in C Escape Sequence Meaning \a Alarm or Beep \b Backspace \f Form Feed \n New Line \r Carriage Return \t Tab (Horizontal) \v Vertical Tab

Constant Example Decimal Constant 10, 20, 450 etc. Real or Floating-point Constant 10.3, 20.2, 450.6 etc. Octal Constant 021, 033, 046 etc. Hexadecimal Constant 0x2a, 0x7b, 0xaa etc. Character Constant 'a', 'b', 'x' etc. String Constant "c", "c program", "c in javatpoint" etc. C if else Statement The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true. There are the following variants of if statement in C language. o If statement o If-else statement o If else-if ladder o Nested if If Statement The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below. if (expression){ //code to be executed }

Flowchart of if statement in C Let's see a simple example of C language if statement. #include<stdio.h> int main(){ int number=0; printf("Enter a number:"); scanf("%d",&number); if (number%2==0){ printf("%d is even number",number); } return 0; } Output Enter a number: 4 is even number enter a number: Program to find the largest number of the three. #include <stdio.h> int main() { int a, b, c; printf("Enter three numbers?"); scanf("%d %d %d",&a,&b,&c); if (a>b && a>c) { printf("%d is largest",a);

Flowchart of the if-else statement in C Let's see the simple example to check whether a number is even or odd using if-else statement in C language. #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); if (number%2==0){ printf("%d is even number",number); } else { printf("%d is odd number",number); } return 0; } Output enter a number: 4 is even number enter a number: 5 is odd number Program to check whether a person is eligible to vote or not. #include <stdio.h> int main() { int age; printf("Enter your age?"); scanf("%d",&age);

if (age>=18) { printf("You are eligible to vote..."); } else { printf("Sorry ... you can't vote"); } } Output Enter your age? You are eligible to vote... Enter your age? Sorry ... you can't vote If else-if ladder Statement The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched. if (condition1){ //code to be executed if condition1 is true } else if (condition2){ //code to be executed if condition2 is true } else if (condition3){ //code to be executed if condition3 is true } ... else { //code to be executed if all the conditions are false }