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⌗

  1. Keywords
  2. Operators
  3. Identifiers
  4. Constants
  5. Special Symbols
  6. String

Keywords⌗

  • auto, double, int, struct, switch ……. (total 32)

Identifier⌗

  • Name of a variable || Name of a function.
constraint for identifier⌗
  1. Can’t be start with digit, only letter || underscore followed by digit
  2. 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⌗
  1. The value it takes
  2. Amount of memory required
  3. 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.

  1. Function Declaration
    1. Announcement that this function we are going to use.
    2. It has function name, input parameters, number of parameters, types of output.
  2. Function Definition
    1. Has code of function.
  3. Function Call
    1. 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⌗

  1. Call by Value
  2. Call by reference
  3. Call by address

Call by Value⌗