0% found this document useful (0 votes)
58 views60 pages

C Faqs

The document discusses the C programming language. It provides details about the latest C standard C18, including some new features added in C18 like type generic macros and atomic operations. It also discusses basics of C like data types, operators, control structures, functions and arrays.

Uploaded by

Makesh MäKz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views60 pages

C Faqs

The document discusses the C programming language. It provides details about the latest C standard C18, including some new features added in C18 like type generic macros and atomic operations. It also discusses basics of C like data types, operators, control structures, functions and arrays.

Uploaded by

Makesh MäKz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

Basics of C

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++

TIOBE Index for May 2019


(The software quality company)
The TIOBE Programming Community index is an indicator of the
popularity of programming languages.  It is important to note that the
TIOBE index is not about the best programming language or the language in
which most lines of code have been written.
How a C programs gets executed?
What is an 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.
Following are some of them:
Control Structures in C

while, do/while, for


switch, break, continue
The while Repetition Structure

• 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

There are functions that return True or False :


So..

while (1) // always True


{
printf(“INFINITE LOOP”);
}
Counter-Controlled Repetition

• 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.

Differentiate Source Codes from Object Codes.


Source codes are codes that were written by the
programmer. It is made up of English-like keywords.
Computers would not be able to understand source codes.
Therefore, source codes are compiled using a compiler. The
resulting outputs are object codes, which are in a format that
can be understood by the computer processor.
In C programming, source codes are saved with the file
extension .C, while object codes are saved with the file
extension .OBJ
Compare and contrast compilers from interpreters.
Interpreters execute program codes one line at a time,
while compilers take the program as a whole and convert it into object
code, before executing it.
The key difference here is, in the case of interpreters,
a program may encounter syntax errors in the middle of execution,
and will stop from there.
On the other hand, compilers check the syntax of the entire program
and will only proceed to execution when no syntax errors are found.

Is C a mid-level programming language?


Yes. because it supports the feature of both low-level and
high level language. C language program is converted into assembly
code, supports pointer arithmetic (low level), but it is
machine independent (feature of high level).
What is type conversion in C?
A type cast is basically a conversion from one type to another.
There are two types of type conversion:
Implicit Type Conversion Also known as ‘automatic type
conversion’.
• Generally takes place when in an expression more than one data
type is present. In such condition type conversion (type promotion)
takes place to avoid loss of data.
• All the data types of the variables are upgraded to the data type of
the variable with largest data type.
Functions in C
The function in C language is also known as procedure or subroutine
in other programming languages.
Advantages:
Code Reusability
Code optimization

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

• The general format when using for loops is


 
for ( initialization; LoopContinuationTest;
increment/decrement )
statements
• Example:
for( int counter = 1; counter <= 10;
counter++ )
cout << counter << endl;
– Prints the integers from one to ten
• For loops can usually be rewritten as while
loops:
initialization;
while ( loopContinuationTest){
statement
increment;
}
• Initialization and increment as comma-
separated lists
for (int i = 0, j = 0; j + i <= 10;
j++, i++)
cout << j + i << endl;
Flowchart for for

Initialize variable

Condition true statement


Test the variable Incre/decre 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};

Two Dimensional Array in C

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.

What is NULL pointer in C?


A pointer that doesn't refer to any address of a value but NULL, is
known as NULL pointer. For example:
int *p=NULL;
What is far pointer in C?
A pointer which can access all the 16 segments (whole residence
memory) of RAM is known as far pointer.

What is pointer to pointer in C?


one pointer refers to the address of another pointer.
Pointer Arithmetic
C Pointer Addition
Accessing arrays using Pointers
What is NULL pointer? 
Ans: NULL is used to indicate that the pointer doesn’t point to a valid
location. Ideally, we should initialize pointers as NULL if we don’t
know their value at the time of declaration. Also, we should make a
pointer NULL when memory pointed by it is de-allocated in the middle
of a program.
What is Dangling pointer?
Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory
location. Dangling pointers arise when an object is deleted or de-
allocated, without modifying the value of the pointer, so that the pointer
still points to the memory location of the de-allocated memory. 
// EXAMPLE 1
int* ptr = (int*)malloc(sizeof(int));
free(ptr);
// ptr is a dangling pointer now and operations like following are
invalid
*ptr = 10; // or printf("%d", *ptr);

You might also like