C-Programming (Full Notes For GATE)
Table of Contents
Syllabus For Fundamental Of C-Programming⌗
- Token in C-programming
- Keywords/Operators/String/Constant/Identifier
- Special Character
- int, char, float (signed and unsigned), short and long
Tokens in C⌗
- Keywords
- Operators
- Identifiers
- Constants
- Special Symbols
- String
Keywords⌗
- auto, double, int, struct, switch ……. (total 32)
Identifier⌗
- Name of a variable || Name of a function.
constraint for identifier⌗
- Can’t be start with digit, only letter || underscore followed by digit
- No special keyword should be used.
C is a Case sensitive language a and A are treaated different.
Constants/Literals⌗
int a = 08;
In above example ‘08’ is a Constant.
Q Solve
#include <stdio.h>
int main() {
int a ;
a = 60+0b1111+014+0x10;
printf("%d", a); // 103
return 0;
}
Explanation 60 <– Decimal Constant; 0b <– Binary Constant 0 <– Octal Constant 0x <– Hexadecimal Constant
๐ข Number System Recap⌗
If you don’t know Number Systems, here’s a quick recap.
Decimal Number⌗
In C, an integer constant without a prefix is treated as a decimal constant.
printf("%d", 40);
Binary Number Calculation⌗
Binary Number -> 0b1111
1 1 1 1
โ โ โ โ
2ยณ 2ยฒ 2ยน 2โฐ
= 1ร8 + 1ร4 + 1ร2 + 1ร1
= 8 + 4 + 2 + 1
= 15
Octal Number Calculation⌗
Octal Number-> 014
014โ
= 1 ร 8ยน + 4 ร 8โฐ
= 1 ร 8 + 4 ร 1
= 8 + 4
= 12โโ
Hexadecimal Number Calculation⌗
Hexadecimal -> 0x
0x10โโ
= 1 ร 16ยน + 0 ร 16โฐ
= 1 ร 16 + 0 ร 1
= 16
Q Solve
#include <stdio.h>
int main() {
int a ;
a = 0101/011+0110+0x110;
printf("%d", a); // 351
return 0;
}
Format Specifier⌗
| Format Specifier | Data Type | Example |
|---|---|---|
%d |
Signed integer | printf("%d", 10); |
%i |
Signed integer | printf("%i", 10); |
%u |
Unsigned integer | printf("%u", 20); |
%f |
Float | printf("%f", 3.14); |
%lf |
Double (scanf) |
scanf("%lf", &d); |
%c |
Character | printf("%c", 'A'); |
%s |
String | printf("%s", "Hello"); |
%x |
Hexadecimal (lowercase) | printf("%x", 255); โ ff |
%X |
Hexadecimal (uppercase) | printf("%X", 255); โ FF |
%o |
Octal | printf("%o", 8); โ 10 |
%p |
Pointer address | printf("%p", ptr); |
%% |
Prints % symbol |
printf("100%%"); |
DATA TYPE⌗
- The value it takes
- Amount of memory required
- Interpretation of value
Signed and Unsigned Number⌗
example: 2s compliment Integer Interpretation 1111 (Signed) -1x2^3 + 1x2^2 + 1x2^1
11111 (UnSigned) 1x2^3 + 1x2^2 + 1x2^1
2’s Complement โ Signed Integer⌗
- MSB (Most Significant Bit) = 0 โ Positive
- MSB = 1 โ Negative
- MSB weight =
-2โฟโปยน - Other bits have normal positive weights.
Example: 4-bit 1111⌗
Weights: -8 4 2 1
Bits: 1 1 1 1
= -8 + 4 + 2 + 1
= -1
Range of a Integer –> -2^n-1 to 2^n-1
Operators & Arithmetic Expression⌗
`+, -, *, /, % "
/ -> Quotient % -> Remainder
Dividend = Quotient Divisor + Remainder*
Solve–> 7/3, -7/3, 7/-3. -7/-3
If dividend and divisor same sign +ve otherwise -ve Remainder: Sign of dividend is sign of Remainder
Solve
#include<stdio.h>
int main(){
int x;
x= -2 + 11 - 7 * 9 % 6 / 12; //9
printf("%d",x);
return 0;
}
C Operator Precedence โ Short⌗
| Priority | Operators | Associativity | ||
|---|---|---|---|---|
| 1 | () |
Left โ Right | ||
| 2 | ++ -- ! ~ |
Right โ Left | ||
| 3 | * / % |
Left โ Right | ||
| 4 | + - |
Left โ Right | ||
| 5 | < <= > >= |
Left โ Right | ||
| 6 | == != |
Left โ Right | ||
| 7 | && |
Left โ Right | ||
| 8 | ` | ` | Left โ Right | |
| 9 | = += -= *= /= |
Right โ Left |
Higher priority = evaluated first.
* / %have same precedence โ evaluate left to right.
Functions In C⌗
`A function is a block of code which only runs when it is called.
*Input (function parameters) and output (return value) of function.
- Function Declaration
- Announcement that this function we are going to use.
- It has function name, input parameters, number of parameters, types of output.
- Function Definition
- Has code of function.
- Function Call
- Function calling (execute)
Declaration Of Function⌗
return_type name(parameter_type1, parameter_type2......);
example:-
float func_name(int a_argument, int b_argument, float c_argument){
};
int * func1 (int *){};
int func (void){}; // no parameters
int func(){};
void func(){}; // if output is not present then use void before func declaration
Function Definition⌗
Actually piece of code.
#include<stdio.h>
float areaOfCircle(float radius){
float area = 3.14*(radius*radius);
return area;
}
void main(){
float r;
printf("Enter the radius: ");
scanf("%f",&r);
printf("Area of Circle: %f",areaOfCircle(r));
}
Type of Parameter Passing⌗
- Call by Value
- Call by reference
- Call by address