Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

Complementary Lecture Material for Problem Solving Through Programming In C | NPTEL | Swayam | Jan 2022

Week 1 : Complementary Study Material July 2022


Q.1 : the Extended Binary Coded Decimal Interchange (EBCDIC) character set. This is a character set that was developed before ASCII (American Standard Code for Information Interchange) became commonly used.

Most systems that you are familiar with use ASCII. In addition, z/OS UNIX® files are encoded in ASCII.




Q.8 : What is language processor

Featured snippet from the web

A language processor is a software program designed or used to perform tasks such as processing program code to machine code. programming language processor is a generic term that can refer to anything that converts code from one computer language into another.









Week 6 : Complementary Study Material



Q. What will be output of the following program?
int main() {
 int i; int arr[3] = {3}; 
 for (i = 0; i < 3; i++) 
 printf("%d ", arr[i]); 
 return 0;
 } 
a) 3 followed by garbage values 
b) 3 0 0 
c) 3 1 1 
d) Syntax error 

Solution: (b) If an array is initialized with few elements, remaining elements will be initialized to 0. Therefore, 3 followed by 0, 0, will be printed. 

The initializer for an array is a comma-separated list of constant expressions enclosed in braces ({ }). The initializer is preceded by an equal sign (=). You do not need to initialize all elements in an array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. 




For C, if an initializer has fewer elements than the array begin initialized, the remaining elements are initialized to zero. The meaning of “zero” in this context is recursive: numbers are initialized to 0, pointers to a null pointer value, and so on. If the element type is an array, structure, or union, its elements are recursively set to 0. If there are more elements in the initializer, that’s a constraint error, which basically means that it’s illegal. (Some compilers might issue a non-fatal warning and do something compiler-specific. You absolutely should not depend on that.)



Q. What will happen if in a C program you assign a value to an array element whose index
exceeds the size of the array?

a) The element will be set to 0
b) The compiler will not give any error but the program may crash if some
important data gets overwritten.
c) The compiler would report an error.
d) The array size would appropriately grow.

Solution: (b) The compiler will not give any error but the program may crash if some important
data gets overwritten. The programmer has to be cautious in this case.








Week 5 : Complementary Study Material


This content right here will help you to solve questions using concepts which are not discussed in the video lectures of the course. As I have mentioned earlier, I have remained a topper in this course ' Problem Solving Through Programming In C ' , and I very well know that the video lectures alone won't be sufficient for cracking the Proctored exam. I was lucky, I got enough time to finish the book suggested by the instructor. Hence, I was able to clear the Proctored exam without any trouble.
But I understand that you might be running out of time. And that's why I decided to start this Complementary Study Material blog. This will provide you enough content which will help you solve assignment as well as proctored exam questions easily.
If You don't get something written here, don't worry I will soon upload explanatory videos on our YouTube channel, explaining all this with hand.

So, don't forget to subscribe to our YouTube Channel :  Swayam Solver


Comma operator

Jump to navigationJump to search

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations. // Don't worry you don't need to understand sequence point right now.

The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.

Syntax[edit]

The comma operator separates expressions (which have value) in a way analogous to how the semicolon terminates statements, and sequences of expressions are enclosed in parentheses analogously to how sequences of statements are enclosed in braces:[1] (a, b, c) is a sequence of expressions, separated by commas, which evaluates to the last expression c while {a; b; c;} is a sequence of statements, and does not evaluate to any value. A comma can only occur between two expressions – commas separate expressions – unlike the semicolon, which occurs at the end of a (non-block) statement – semicolons terminate statements.

The comma operator has the lowest precedence of any C operator, and acts as a sequence point. In a combination of commas and semicolons, semicolons have lower precedence than commas, as semicolons separate statements but commas occur within statements, which accords with their use as ordinary punctuation: a, b; c, d is grouped as (a, b); (c, d) because these are two separate statements.

Examples

In this example, the differing behavior between the second and third lines is due to the comma operator having lower precedence than assignment. The last example differs as well since the return expression must be fully evaluated before the function can return.

/**
 *  Commas act as separators in this line, not as an operator.
 *  Results: a=1, b=2, c=3, i=0
 */
int a=1, b=2, c=3, i=0;

/**
 *  Assigns value of b into i.
 *  Commas act as separators in the first line and as an operator in the second line.
 *  Results: a=1, b=2, c=3, i=2
 */
int a=1, b=2, c=3;              
int i = (a, b);           
                      
/**
 *  Assigns value of a into i.
 *  Equivalent to: int i = a; int b;
 *  Commas act as separators in both lines.
 *  The braces on the second line avoid variable redeclaration in the same block,
 *  which would cause a compilation error.
 *  The second b declared is given no initial value.
 *  Results: a=1, b=2, c=3, i=1
 */
int a=1, b=2, c=3;                                
{ int i = a, b; }

/**
 *  Increases value of a by 2, then assigns value of resulting operation a + b into i.
 *  Commas act as separators in the first line and as an operator in the second line.
 *  Results: a=3, b=2, c=3, i=5
 */
int a=1, b=2, c=3;
int i = (a += 2, a + b);
          
/**
 *  Increases value of a by 2, then stores value of a to i, and discards unused
 *  values of resulting operation a + b.
 *  Equivalent to: (i = (a += 2)), a + b;
 *  Commas act as separators in the first line and as an operator in the third line.
 *  Results: a=3, b=2, c=3, i=3
 */
int a=1, b=2, c=3;
int i;
i = a += 2, a + b;

/**
 *  Assigns value of a into i.
 *  Commas act as separators in both lines.
 *  The braces on the second line avoid variable redeclaration in the same block,
 *  which would cause a compilation error.
 *  The second b and c declared are given no initial value.
 *  Results: a=1, b=2, c=3, i=1
 */
int a=1, b=2, c=3;
{ int i = a, b, c; }

/**
 *  Commas act as separators in the first line and as an operator in the second line.
 *  Assigns value of c into i, discarding the unused a and b values.
 *  Results: a=1, b=2, c=3, i=3
 */
int a=1, b=2, c=3;
int i = (a, b, c);

/**
 *  Returns 6, not 4, since comma operator sequence points following the keyword 
 *  return are considered a single expression evaluating to rvalue of final 
 *  sub-expression c=6.
 *  Commas act as operators in this line.
 */
return a=4, b=5, c=6;

/**
 *  Returns 3, not 1, for same reason as previous example.
 *  Commas act as operators in this line.
 */
return 1, 2, 3;

/**
 *  Returns 3, not 1, still for same reason as above. This example works as it does
 *  because return is a keyword, not a function call. Even though compilers will 
 *  allow for the construct return(value), the parentheses are only relative to "value"
 *  and have no special effect on the return keyword.
 *  Return simply gets an expression and here the expression is "(1), 2, 3".
 *  Commas act as operators in this line.
 */
return(1), 2, 3;

Uses

The comma operator has relatively limited use cases. Because it discards its first operand, it is generally only useful where the first operand has desirable side effects that must be sequenced before the second operand. Further, because it is rarely used outside of specific idioms, and easily mistaken with other commas or the semicolon, it is potentially confusing and error-prone. 

For loops

The most common use is to allow multiple assignment statements without using a block statement, primarily in the initialization and the increment expressions of a for loop. This is the only idiomatic use in elementary C programming. 


This blog will keep updating throughout the course. Make sure you bookmark this webpage.

This is just the begnning. More quality content is on the way.


Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making qualilty content...

Have a great day ! 


No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.