Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Basic Concepts of C language as well as c programs, Lecture notes of Computer Programming

Benefits of all c programming students.

Typology: Lecture notes

2018/2019

Uploaded on 07/22/2019

Vivek_Jain
Vivek_Jain 🇮🇳

1 document

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
NOTES FOR C LANGUAGE PART 1
Back to notes section :
What is Complier?
Compiler is a program that converts the source code (program written in a
language other than machine language ) in to machine code. Computer
can understand machine code and execute machine code. Therefore we
can state that compiling is a process of converting source code to machine code
if source code is error free.
What is the basic structure of c program?
Consider a simple program
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
printf(“\n enter two values”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(“\n addition=%d”,c);
getch();
}
We can note following facts:
1. Program starts with #include
#include is known as preprocessor directive. This preprocessor directive gives
instruction to preprocessor program to include contents of file mentioned in
angle bracket (or in double quotes) to our source file. The file name in angle
bracket is known as include file (because this is used always with include
preprocessor) or header file (appears in head/top position of file). This header
files contents macro, constant and function declaration.
2. void main()
void specifies function does not return value.
‘main’ is a function which acts as entry point from which our program statement
starts running. Declaration statement is followed by executable statements.
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

Partial preview of the text

Download Basic Concepts of C language as well as c programs and more Lecture notes Computer Programming in PDF only on Docsity!

NOTES FOR C LANGUAGE PART 1

Back to notes section : What is Complier? Compiler is a program that converts the source code (program written in a language other than machine language ) in to machine code. Computer can understand machine code and execute machine code. Therefore we can state that compiling is a process of converting source code to machine code if source code is error free. What is the basic structure of c program? Consider a simple program #include <stdio.h> #include <conio.h> void main() { int a,b; printf(“\n enter two values”); scanf(“%d %d”,&a,&b); c=a+b; printf(“\n addition=%d”,c); getch(); } We can note following facts:

  1. Program starts with #include #include is known as preprocessor directive. This preprocessor directive gives instruction to preprocessor program to include contents of file mentioned in angle bracket (or in double quotes) to our source file. The file name in angle bracket is known as include file (because this is used always with include preprocessor) or header file (appears in head/top position of file). This header files contents macro, constant and function declaration.
  2. void main() void specifies function does not return value. ‘main’ is a function which acts as entry point from which our program statement starts running. Declaration statement is followed by executable statements.
  1. Blank spaces may be inserted to increase readability.
  2. Usually all statements are entered in lowercase
  3. We can start writing statement from any column position.
  4. Any ‘C’ statement terminates by semicolon; Write program to swap two values. (a) using a third variable. #include<stdio.h> #include<conio.h> void main() { int a=3,b=4,c; c=a; a=b; b=c; printf(“\n a=%d,b=%d”,a,b); getch(); } (b)without using a third variable. #include<stdio.h> #include<conio.h> void main() { int a=3,b=4; a=a+b; b=a-b; a=a-b; printf(“\n a=%d,b=%d”,a,b); getch(); } What does void means in front of main()? void means function is not going to return a value. If we remove void we have to

source file (program written in high level language). Some preprocessor are: #include, #define, #ifdef, #ifndef, #undef, #else, #endif, #pragma etc. What are the popular features of ‘C’ language? The features are:

  1. Portability: This refers to the ability of a program to run in different environments. Different environments could refer to different computers, operating systems, or different compilers. Since ‘C’ language is mid level language and its compiler is available for variety of environments we can say it is portable.
  2. Flexibility: ‘c’ language is flexible because: (a) It provides facility of creating user defined data type. (b) It is not necessary to write statement in ‘c’ in a particular format which means we can start writing statement in ‘c’ from any column.
  3. Mid level language: C is a mid level language combines feature of high level language (faster code development) and low level language (efficient program).
  4. Wide acceptability: Majority of people know ‘C’ language.
  5. System Programming: system programming and application programming using ‘C’ language are possible. What is an IDE? An IDE is nothing else but it is a program which includes all facilities to develop and run program, such as editor, compiler, debugger and so on. What is C? C is a programming language developed at AT & B bell lab. of USA in 1972. It was developed by Dennis Ritchie. It is user friendly, general purpose and reliable language. How C originated? Or Write short notes on history of C language. By 1960 a hoard of computer language had come into existence almost each for a specific purpose. An International committee was setup to develop a general purpose language using which any type of application can be developed easily and the outcome of the effort was Alogol-60. Algol-60 did not become popular because of its abstract nature and many complicated features. Combined

programming language was developed with reduced feature of Alogol-60 but it was still vague and did not become popular. Later on basic combined programming language was developed with reduced feature when compared to combined programming language. Basic Combined Programming Language had very limited feature and did not become popular. B language was developed by Ken Thompson as further enhancement to Basic Combined Programming Language. Dennis Ritchie developed some new feature and used features of language ‘B’ and Basic Combined Programming Language and developed ‘c’ and ‘c’ became popular. Why c is called a middle level language? All the programming languages can be divided into two categories: (a) Problem oriented languages or high level languages: These language have been designed to give a better programming efficiency. i.e. faster program development E.g. Fortran, Basic. (b) machine oriented languages or low level languages: These language have been designed to give a better machine efficiency means faster program execution. ‘c’ stands in between these two categories. That is why it is called a middle level language it has got relatively good programming efficiency (as compared to machine oriented languages) and relatively good machine efficiency (as compared to high level languages). What do you mean by character set of C? A character denotes any alphabet, digit or special symbol used to represent information. All those characters which can be part of a ‘c’ program and program still remains valid is knowns as ‘c’ character set. Few e.g. as alphabets a-z, digits -9, special symbols ,,{,} etc. What is Syntax? Syntax is correct way of writing statement according to rules suggested by language. What is constant?/What are the several type of constant that c language supports?. what is the difference between variable and constant? how can variable be initialized? A constant is a quantity that doesn’t change during program run time. This quantity can be stored at a location in memory. A variable is that whose value can change during run time of program.

‘y’ invalid is ‘ab’ /* only single character can be used / invalid is “a” / single quote must be used */ (B) Secondary constants Array, pointers, structure, union, enum etc. are known as secondary constant. What are the different data types available in c? ans: char,unsingned char, int,unsigned int,long ,unsigned long,float,double, long double. What is variable? Variable is name given to a location of computer memory where we can store value and retrieve the stored value by variable name. Value of variable can change during program run time and it is fundament requirement of any programming language. Rules for constructing identifier (variable/pointer/function name):

  1. A variable name is any combination of alphabets, digits and underscore. Some compiler supports 40 character lengthy variable names.
  2. The first character in variable name must be alphabet.
  3. No commas or blanks or special character can appear in variable name.
  4. Variable name is case sensitive therefore variable Area and variable area are different.
  5. Variable name must not match with keywords. e.g. few valid variable names are: si_int pop_pe_ few invalid variable names are a b /* we can not use space / 1b / must start with alphabet / char / char is keyword */

What is keyword? Keyword or reserved word is word whose meaning is well defined for compiler we can use it in our program but we can not alter its meaning. e.g. int, float, do, while etc. There are such 32 keywords. What is statement or instruction? An instruction requests computer to perform some work. These are following types:

  1. Type declaration statement (non executable statement) This is useful to declare variables and functions used in c program.
  2. Input/output statement (executable statement) This is useful to supply data into program(input statement) or to take output from program(output statement).
  3. Arithmetic assignment statement This is useful to perform arithmetic operations between constant and variables. Arithmetic statement may be of three types: a. Integer mode statement in which integer variables and constant appear in statement. b. Real mode statement in which real variables and constant appear in statement. c. Mixed mode statement in which integer and real variables and constant appear in statement.
  4. Control statement This is useful to control the sequence of execution of various statements. Control statements include looping and branching. Branching includes if..else, switch where as looping include while, do..while, for loop. 5.Conditional statement conditional statement is made of variable, constant, relational operators and logical operators. Conditional statement returns either true or false. In c’s point of view, 0 is false and non zero is true. e.g. int c,a=3,b=4; c= a = = 3|| b>4; /* this is conditional statement */ What is Expression?

(a)Values of variable or constant can be read by: scanf function whose syntax is scanf(“format strings”,&var1,&var2…); format string can be constructed using format specifier character e.g. %c, %d, %u, %f. Which format specifier to use? depends on data type of variable to read value in. Suppose variables are declared as int a,b; float e; char c; then we need statement scanf(“%d %d %f %c”,&a,&b,&e,&c); Since a and b are int types we used corresponding format specifier %d two times because there are two int type variables to read values in. Since f and c are float and char types respectively we used corresponding format specifier %f and %c. It means that no. of variables required read values in, must match with no. of format specifier. Each data type uses a predefined format specifier and we have to use the predefined one. (b) writing of values of variable or constant: it can be done using printf statement whose syntax is as follows printf(“message to print”); e.g. printf(“enter an integer”); or printf(“format string”,var1,var2); if we have declared variables as: int a=10; float b=20; we can give statement printf(“%d %f”,a,b); Since a is int we used %d format specifier. Since b is float we used %f format specifier The values of variable or constant to print replace format specifier at print time.

Therefore output will be 10 20. if we give statement printf(“a=%d,b=%f”,a,b); output will be a=10 b=20. How can we read character constant in character variable? (Single character input-output) Character variable is treated internally as integer therefore we have two ways to read a character constant in character variable. (1)Character constant can be read using %c format specifier when to read a single character. Consider following program: #include<stdio.h> #include<conio.h> void main() { char p; printf(“\n enter a character”); scanf(“%c”,&p); /* or we can use c=getchar(); c=getche(); c=getch(); */ printf(“\n the character read is %c and ascii value is %d”,p,p); getch(); } If we enter a character ‘a’ during run time of program then output will be: The character read is a and ascii value is 97 (2)Character constant can be read using %d format specifier when to read an integer value corresponding to ascii value of character to read #include<stdio.h> #include

could also be written as:- signed int i; qualifier can be two types: a)Size qualifier size qualifier modifies the range of value, a variable can store. short: applies to int long: applies to int and double b)Sign qualifier signed: applies to int, char unsigned: applies to int, char What do you mean by typedef statement?/define type declaration in c. The typedef (keyword) statement allows creating new name for existing data type. For example if we write a statement typedef unsigned long ulong; Then we can use statement :- ulong a; This means that: unsigned long u; What is the difference between ‘a’ and “a”? ‘a’ “a” ‘a’ is a character constant. “a” is a string constant Character constant can have a single character enclosed inside single quote. String constant can have one or more character enclosed inside double quotes. a character constant takes just one byte of memory. a string constant takes no. memory that is sum of no. of characters inside double quotes plus 1 byte taken by null character. %c is format specifier for formatted%s is format specifier for formatted

input/output. input/output. getch(),getchar(),getche() are unformatted input library functions. gets() is a unformatted input library function. What are the different types of operators available in c? Different types of operator are available in ‘C’ are: Arithmetic operators +,-,,/,% Relational operators >,<,>=,<=,= = ,!= Logical operators &&,||,! Assignment operators = compound assignment operators are: +=,-=,%=,/= etc. Increment/decrement operators ++,-- Conditional operators ( condition ) ?true statement :false statement Bitwise operators &,|,^,<<,>>,~ Comma operators , Other operators Sizeof Write short notes on shorthand operators. short hand operator are ++,- -,+=,-=,= etc. suppose int a=1; a++; value of a is now 2

<< left shift operator

right shift operator ~ complement operator suppose int a=13 ,b=7,c; c=a&b; first row is a’s bit pattern ,second row is b’s bit pattern ,third row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 c=a|b; first row is a’s bit pattern ,second row is b’s bit pattern ,third row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 c=a^b; first row is a’s bit pattern ,second row is b’s bit pattern ,third row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 c=a<<3; first row is a’s bit pattern ,second row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 clearly c= a*(2^3 ) c=a>>3; first row is a’s bit pattern ,second row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1

clearly c= a/(2^3 ) c=~a; first row is a’s bit pattern ,second row is c’s bit pattern 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 Explain the behavior of right shift operator for positive and negative integer. Zeros are always inserted in the left if integer is positive otherwise 1 is inserted in the left, if integer is negative when using right shift operator. if we assign -3 to a variable ‘a’ int a=-3; then binary representation of ‘a’ in two’s complementary system is computed as follows: binary representation of positive 3 is 0000 0000 0000 0011 taking complementary 1111 1111 1111 1100 by adding 1 to get two’s complementary 1111 1111 1111 1101 which is binary pattern of - now c=a>>2; will shift the value of a by 2 bit positions to the right and will insert two 1’s in the left. so value of c will be 1111 1111 1111 1111 once again taking complementary 0000 0000 0000 0000 adding 1 to get two’s complementary 0000 0000 0000 0001 2’s complement of a 2’s complement is the original number itself result indicates it is -1 in decimal. What do you mean by mixed mode arithmetic?

Associativity of operator can be left to right or right to left. For unary, compound assignment and ternary operator associativity is right to left for others it is left to right. Consider c=a+b+d; If two operators having same operator precedence occurs in a mathematical assignment statement then associativity starts playing its role. Since + operator has associativity from left to right therefore a+b will be evaluated first then result of a+b will be added to d and finally result of evaluation will be assigned to c. What is Control Structure? Control structure specifies the order in which the various instructions in a program are to be executed by the computer. There are four type of control structure or control statement:

  • Sequence control instruction –ensures statement are executed in same order in which they appear in program.
  • Selection or decision control instruction-if given condition becomes true executes one set of statement if given condition becomes false executes different set of statement.
  • Repetition or loop control instruction- given set of statements are executed repeatedly till given condition is true.
  • Case control instruction – checks value of a single variable/constant and performs different action depending on case defined for value of single variable/constant. Why do we need control structure? We all need to alter our action depending on changing circumstances. For example If weather is fine I will go Dongargarh on bike otherwise I will go by train. In the same way ‘C’ language too must be able to perform different action under one condition and different action under another condition and this is made possible by control structure. Sometimes a given set of statements are needed to run repeatedly this is done using loop control structure. Write notes on if control structure. The general form of if statement is: (a)if the general format of if statement is if (condition is true)

execute this statement; The keyword if tells the compiler that what follows, is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. (b) if -else if statement by itself can execute only one statement if condition is true. If it required running a group of statements when condition is true we have to enclose those statements inside curly brace known as compound statement. The above form of if statement mentioned in (a) will not do anything when condition is false. If we want to run statement when condition is false we need if-else construct. General format of if-else construct is If (condition is true) Execute this statement; Else Execute this statement if condition is false; Condition is specified using relational operator x= =y means x is equal to y. x!= y means x is not equal to y. x< y means x is less than y. x>y means x is greater than y. x<=y means x is less than or equal to y. x>=y means x is greater than or equal to y What is the difference between = and = =? = operator is useful to assign value to some variable. = = operator is useful to compare value of variables or constant and can be used to build conditional expression.