IBPS SO IT Officer Programming Languages Notes Part II
IBPS SO IT Officer Programming Languages Notes Part II
Misc Operators ↦ sizeof & ternary
Operator | Description | Example |
sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 4. |
& | Returns the address of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; |
? : | Conditional Expression. | If Condition is true ? then value X : otherwise value Y |
Operators Precedence in C
Category | Operator | Associativity |
Postfix | () [] -> . ++ – – | Left to right |
Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
Join Best Exam Preparation group on Telegram
Decision making structures
IF statement
It is a powerful decision making statement and use to control the flow of execution of statement. Program has two paths to follow, one for “true” condition and other for “false” condition.
IF-ELSE statement
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.
SWITCH statement
- switch statement must have an integral or enumerated type,in which the class has a single conversion function to an integral or enumerated type.
- Each case is followed by the value to be compared to and a colon.
- The constant-expression for a case must be the same data type as the variable in the switch.
- When the variable being switched on to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line of the switch statement.
- every case needs not to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case performing a task when none of the cases is true. No break is needed in the default case.
The ? : Operator
Exp1, Exp2, and Exp3 are expressions.
- Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.
- If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
C – Loops
IBPS SO IT Officer Programming Languages Notes Part II
While loop
Repeats a statement until a given condition is true. It tests the condition before executing the loop body.
Example
#include <stdio.h>
int main () {
int a = 10;
while( a < 20 ) {
printf(“value of a: %d\n”, a);
a++;
}
return 0;
}
For loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
for ( init; condition; increment ) {
statement(s);
Do -while loop
It is like a while statement, except that it tests the condition at the end of the loop body.
do {
statement(s);
} while( condition );
Nested loop
You can use one or more loops inside any other while, for, or do..while loop.
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
Control Statements
Break statement
Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
Continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Goto statement
Transfers control to the labeled statement.
Functions
A function is a group of statements that together perform a task. C program has at least one function, which is main(). A function definition provides the actual body of the function.
strcat() to concatenate two strings, memcpy() to copy one memory location to another location.
C programming consists of a function header and a function body. Here are all the parts of a function −
- Return Type − A function may return a value. The return_type is the datatype of the value the function returns. Some functions perform the desired operations without returning a value.The return_type is the keyword void.
- Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function.
- Function Body − The function body contains a collection of statements that define what the function does.
Type of function call
Call by value
This method copies the actual value of an argument into the formal parameter of the function.Changes made to the parameter inside the function have no effect on the argument.
Call by reference
This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
Scope Rules
Local Variables
- Variables that are declared inside a function or block are called local variables.
- They can be used only by statements that are inside that function or block of code.
- Local variables are not known to functions outside their own.
Global Variables
- Global variables are defined outside a function,
- On top of the program.
- Global variables hold their values throughout the lifetime of your program and accessed inside any of the functions defined for the program.
- A global variable can be accessed by any function.
- Global variable is available for use throughout your entire program after its declaration.
Arrays
Arrays is collection of same data type elements.. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
type arrayName [ arraySize ];
This is called a single-dimensional array.
Multidimensional array :Multidimensional arrays is the form of two-dimensional array.
Pointers
A pointer is a variable which contain address of another variable.
type *var-name;
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Important facts about pointers
- Variable stores the value whereas pointer variable stores the address of the variable.
- The address of the C pointer always be a whole number.
- C pointer is initialized to null.
- The value of null pointer is 0.
- “& “ is used to get the address of the variable.
- * symbol is used to get the value of the variable that the pointer is pointing to.
- When a pointer is assigned to NULL, it means it is pointing to nothing.
- Two pointers can be subtracted to know how many elements are available between these two pointers.
- Pointer addition, multiplication, division are not allowed.
- The size of any pointer is 2 byte.
Strings
Strings are one-dimensional array of characters terminated by a null character ‘\0’.
strcpy(s1, s2):Copies string s2 into string s1.
strcat(s1, s2) : Concatenates string s2 onto the end of string s1.
strlen(s1) : Returns the length of string s1.
strcmp(s1, s2) : Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch) : Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2) :Returns a pointer to the first occurrence of string s2 in string s1.
Structures : The struct statement defines a new data type, with more than one member.
struct [structure tag] {
member definition;
member definition;
member definition;
} [one or more structure variables];
Unions
A union is a special data type available in C that allows to store different data types in the same memory location.
union [union tag] {
member definition;
member definition;
…
member definition;
} [one or more union variables];
typedef
typedef, which you can use to give a type, a new name.
typedef unsigned char BYTE;
Input / output
Getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only single character at a time.
Putchar(int c) : function puts the passed character on the screen and returns the same character. This function puts only single character at a time.
char *gets(char *s) :function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File).
int puts(const char *s) : function writes the string ‘s’ and ‘a’ trailing newline to stdout.
scanf(const char *format, …) function reads the input from the standard input stream stdin.
printf(const char *format, …) function writes the output to the standard output stream stdout and produces the output.
Preprocessors
Directive Description
#define | Substitutes a preprocessor macro. |
#include | Inserts a particular header from another file. |
#undef | Undefines a preprocessor macro. |
#ifdef | Returns true if this macro is defined. |
#ifndef | Returns true if this macro is not defined. |
#if | Tests if a compile time condition is true. |
#else | The alternative for #if. |
#elif | #else and #if in one statement. |
#endif | Ends preprocessor conditional. |
#error | Prints error message on stderr. |
#pragma | Issues special commands to the compiler, using a standardized method. |
Related Posts
« IBPS SO IT Officer Programming Languages Notes IBPS SO IT Officer Notes for Operating System »