C Faqs
C Faqs
Recent version of C?
Like all other programming languages C has also got its own
versions.
C18 and C17 are informal names for ISO/IEC 9899:2018.
( International Organization for Standardization/International
Electro technical Commission)
The most recent standard for the C programming language,
published in June 2018.
It replaced C11 (standard ISO/IEC 9899:2011).
C11 (formerly C1X) is an informal name for ISO/IEC
9899:2011.
what are the new features added in C18 that was not there in the
previous release?
Some of the noteworthy newly added features are:
• Type Generic Macros
• Anonymous Structures
• Improved Unicode Support
• Atomic Operations
• Multi-threading
• Bounds-checked Functions
• Improved Compatibility with C++
• Repetition structure
– Programmer specifies an action to be repeated while
some condition remains true
– while loop repeated until condition becomes false.
• Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
The while Repetition Structure
• Flowchart of while loop
true
condition statement
false
int x = 2;
while (x >= 0){
if ( x == 2){
printf(“%d Value of x is : “, x);
}
x = x – 1;
}
• Common errors:
– infinite loop
– uninitialized variables
• Counter-controlled repetition
– Loop repeated until counter reaches a certain value.
• Definite repetition
– Number of repetitions is known
• Example
A class of ten students took a quiz. The Marks (integers
in the range 0 to 100) for this quiz are available to you.
Determine the class average on the quiz.
Formulating Algorithms (Counter-
Controlled Repetition)
• Pseudo code for example:
Set total and marks counter to zero
While (marks counter < 10)
Input the next mark
Add the marks into the total
mark counter++
average = total divided / 10
Print the average
How many cases a switch statement can have?
Microsoft C does not limit the number of case values in a
switch statement. The number is limited only by the available
memory. ANSI C requires at least 257 case labels be allowed in
a switch statement.
Types of functions:
Calling a function in C
Syntax:
Variable = function_name (arguments...);
What is the difference between actual and formal
arguments in C?
Dennis M Ritchie in his classic book on C language mentioned, "We
will generally use ”parameter” for a variable named in the
parenthesized list in a function definition, and ”argument” for the
value used in a call of the function. The terms formal
argument and actual argument are sometimes used for the same
distinction."
The major difference between actual and formal arguments
is that
actual arguments are the source of information; calling
programs pass actual arguments to called functions.
The called functions access the information using
corresponding formal arguments.
Recursion in C
When function is called within the same function, it is known
as recursion in C.
The function which calls the same function, is known as recursive
function.
Storage Classes in C
Storage classes are used to define scope and life time of a variable.
There are four storage classes in C programming.
Assignment Operators
• Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the
addition assignment operator
• Statements of the form
variable = variable operator expression;
C = c + 3;
can be rewritten as
variable operator= expression;
c + = 3;
• Examples of other assignment operators include:
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Increment and Decrement Operators
• Increment operator (c++) - can be used instead of
c += 1
• Decrement operator (c--) - can be used instead of
c -= 1
• Preincrement
• When the operator is used before the variable (++c or –c)
• Variable is changed, then the expression it is in is evaluated.
• Posincrement
• When the operator is used after the variable (c++ or c--)
• Expression the variable is in executes, then the variable is changed.
• If c = 5, then
– cout << ++c; prints out 6 (c is changed
before cout is executed)
– cout << c++; prints out 5 (cout is
executed before the increment. c now has the
value of 6)
• When Variable is not in an expression
– Preincrementing and postincrementing have the
same effect.
c=5;
++c;
cout << c;
and
c=5;
c++;
cout << c;
have the same effect.
Predict the output:
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ;
j = a[1]++ ;
m = a[i++] ;
printf ( "\n%d %d %d", i, j, m ) ; }
The for Repetition Structure
Initialize variable
false
• Program to sum the even numbers from 2 to 100
1 // Fig. 2.20: fig02_20.cpp
2 // Summation with for
3 #include <iostream.h>
4
5
6
7
8 int main()
9 {
10 int sum = 0;
11
12 for ( int number = 2; number <= 100; number += 2 )
13 sum += number;
14
15 cout << "Sum is " << sum << endl;
16
17 return 0;
18 }
Sum is 2550
Do it now:
Program to find the sum of ODD numbers till 99
The switch Multiple-Selection Structure
• switch
– Useful when variable or expression is tested for
multiple values
– Consists of a series of case labels and an optional
default case
– break is (almost always) necessary
switch (expression) {
case val1:
statement if (expression == val1)
break; statement
case val2: else if (expression==val2)
statement statement
break; ….
….
else if (expression== valn)
case valn: statement
statement else
break; statement
default:
statement
break;
}
flowchart
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
The do/while Repetition Structure
• The do/while repetition structure is similar to the
while structure,
– Condition for repetition tested after the body of the loop
is executed
• Format:
do {
statement statement
} while ( condition );
• Example (letting counter = 1):
true
do {
condition
cout << counter << " ";
} while (++counter <= 10); false
– This prints the integers from 1 to 10
• All actions are performed at least once.
The break and continue Statements
• Break
– Causes immediate exit from a while, for,
do/while or switch structure
– Program execution continues with the first
statement after the structure
– Common uses of the break statement:
• Escape early from a loop
• Skip the remainder of a switch structure
• Continue
– Skips the remaining statements in the body of a
while, for or do/while structure and proceeds
with the next iteration of the loop
– In while and do/while, the loop-continuation test
is evaluated immediately after the continue
statement is executed
– In the for structure, the increment expression is
executed, then the loop-continuation test is evaluated
The continue Statement
• Causes an immediate jump to the loop test
int next = 0;
while (true){
cin >> next;
if (next < 0)
break;
if (next % 2) //odd number, don’t print
continue;
cout << next << endl;
}
cout << “negative num so here we are!” << endl;
Nested control structures
• Problem:
A college has a list of test results (1 = pass, 2 = fail)
for 10 students. Write a program that analyzes the
results. If more than 8 students fail, print "Raise
Tuition".
• We can see that
– The program must process 10 test results. A counter-
controlled loop will be used.
– Two counters can be used—one to count the number of
students who passed the exam and one to count the
number of students who failed the exam.
– Each test result is a number—either a 1 or a 2. If the
number is not a 1, we assume that it is a 2.
C Array
Array in C language is a collection or group of elements (data).
All the elements of c array are homogeneous (similar).
It has contiguous memory location.
Declaration of C Array
int marks[5];
Initialization of C Array
marks[0]=80;//initialization of arra
ymarks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
C Array: Declaration with Initialization
int marks[5]={20,30,40,50,60};
Or
int marks[]={20,30,40,50,60};
Initialization of 2D Array in C
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j Do it now:
}//end of i Matrix addition
Passing Array to Function in C
To pass array in function, we need to write the array name
only in the function call.
return_type functionname(arrayname);//passing array
What is the difference between call by value and call by reference in
C?
We can pass value to function by one of the two ways:
call by value or call by reference.
In case of call by value, a copy of value is passed to the function, so
original value is not modified.
But in case of call by reference, an address of value is passed to the
function, so original value is modified
What is pointer in C?
A pointer is a variable that refers to the address of a value. It makes
the code optimized and makes the performance fast.