Keep Calm and Code in C

Keep Calm and Code in C

·

10 min read

Table of contents

description:→

Embark on a comprehensive journey into the world of C programming with "Keep Calm and Code in C." This course is meticulously designed for beginners eager to grasp the foundational concepts of the C language, as well as for seasoned programmers aiming to reinforce their skills.


1>Why C is called Middle Level Language program?

C is considered a middle-level programming language because it combines features of both low-level and high-level languages:

  • Low-level features: C supports pointer arithmetic, which is a low-level feature.

  • High-level features: C is machine independent, which is a high-level feature.

  • Balance: C provides a balance between high-level and low-level languages.

if you want to learn more about it try to go though this :→

Click Here.


2>What is differences between Interpreter and Compiler?

i>The Compilers generate intermediate machine codes. Interpreter never made machine code.

ii> A Compiler takes a whole program. Interpreter take code line by line.

iii>compiler run code faster than Interpreter. Interpreter run code slower than compiler .

exaample:→

compliled: c,c++,c#

interpreted: js,python etc.


3>What are the different data types in C?

data type show what a variable can store like int float etc.

so here we define all the data type at once.

int :→for whole number

char:→ for “ascii” values and characters too.

float:→ real and decimal numbers

double :→ the number we can’t represent with int and float

void:→ that doesnot have anything.

enum:→special data type that show unchangeable constant.


4>What is the format specifier of unsigned int?

format specifier tells the compiler what data to be printed.

for unsigned int it is :→ %u ;


5>What is the range of unsigned char?

0 to 255

size :→ 1 byte.


6>Define Array.

array is fundamental data structure that used to store imformation efficiently.

so it’s a collection of same type of data type that’s stored in a memory loaction.

access by indexes. it follow 0 based indexing and go from 0 to n-1.

array size is constant not dynamic.

syntax:→ datatype array_name[array_size];

example:→


7>What is the differences between array of pointers and pointer to an array?

1>

array of pointer the name it self says it all :→

it’s an array of pointer. means it’s an array whose all elements are “pointer”.

pointer to an array :→

refer to a single “pointer” , that point to a single array.

2>

decleration :→



3>

memory alocation :->

array of pointer has multiple pointers so it can point to different locations.

pointer to an array has single pointer to it will point to single location.


8>What is function declaration?

when we write the ruturn type, function name and paramets it’s named as function declaration. here int is return type ayush is function name and int a and b is parameter.

int ayush(int a, int b);


9>What is the base address of an array?

The address of the first element of an array is called as base address of that array.

the memory adress is the adress.


10>Illustrate the advantages of recursion?

1>

readabilty:→

recursion increase the read-ablity of the code.

2>

help in problem solving:→

this help us to see the problem statement close that’s why…help in ps.

example:→

we can do the same with for loop but this increse the readablity and help in ps.


11>Explain strlen() with an example.

it show the string length. it’s in <string.h> header file,

#include <stdio.h>
#include <string.h>

int main(){
  char ayush[] = "hello batman";
  int length = strlen(ayush);
  printf("length of string is %d", length);
}



12>Explain strcmp() with an example.

it’s also in <string.h> header file.

this function two string as argument and it compare their “ascii” value and give result in +ve,-ve and in 0 from.

if two string are same it will give 0.

ptr: cpm always ask that “ascii”.



13>Define structure.

a structure (or struct) is a user-defined data type, that groups together variables of different data types under a single name.


14>Define pointer.

pointer is variable that store the memory address of the another variable.

syntax:→

int batman = 69;
  int *catwoman;
  catwoman = &batman;



15>what is function signature?

it refers to the combination of a function's name, data type of the parameters and doesnot care about return type.

example:→

with function decleartion :→

int ayush(int a, int b);// function decleartion

the function signature is :→

ayush(int,int);// here data type order is imp

16>all about recursion:→

what recursion do ?

when a function call itself is called recusion

mainly used for ps and stroing data more optimisely and increase the raed ablity of the code.

then we can give the example.


usage of recursion stack :→

i>recursion stack is just a implentation of call stack.

it can do 4 task.

1>track fxn call :→

the stack take care of it that every time the fxn is calling itself over and over untill reaching base case.

2>handle base case:→

it take care of it after the all function is called base is reach the program will be stoped is done by it.

3>execution:→

after function calling and handling the base case the stack load to show the output.

4>the main feature of it preseving the loacl var in the program that are used in recursion.


all stages of recursion:→

so let’s disscuss it

1>base case :→

it’s the condition that stop the function otherwise will be a never ending loop

2>function call:→

it’s the recursive call and explain the actuall crux of the code and case logic by which the function will call it again and again .

3>progeress to base case :→

after the function call in this stage function call itself again and again untill it reaches to base case and after that after that recursion stack will refresh itself and output will be shown.


eg. factorial code prv.


17>Explain how reusability can be achieved usign function

Reusability using functions is achieved by encapsulating logic into modular units. That can be called multiple times, By allowing repeated use with different inputs through parameters.it make the code flexible and maintainable. For example, a calculateArea(radius) function can be reused for any circle's area calculation without rewriting the logic.


18>State the differences between a++ and ++a in C


19>Write the difference between ‘A’ and “A”.


20>How many types of constants possible in C? [4]

4 type of constants.

a>integer constant:→

These represent whole numbers (both positive and negative). They can be written in:

a>decimal(123)

b>octal(127)

c>hexa-dec(12a)


b>character constant:→

show the single alphabet can be shown in single quotes.’a


c>floating constant :→

show the num that’s fractional.

a>decimal b>scientific


d>string constant:→

sequnce of char enclosed in [] breacter.

eg. “hello“


21>Define compiler

A compiler is a program that translates source code written in a high-level programming language (like C, C++, or Java) into machine code.

ensure program is optimized and structured and error free.


22>What is main()?

The main() function in C is the starting point of program execution. It is a special function where the program begins running. nad after succfull run it return 0 Every C program must contain a main() function. Its typical syntax is:

#include<stdio.h>

int main(){
//code
return 0;

}

23>What is printf()? Why it is used?

The printf() function in C is used to output data to the console. it’s used to display the user output or promt given by the user with hel[ of format specifeirs like :→

%s,%d,%f…it;s used to shoe number,s tring cahr etc..


24>Explain extern type of variable in C. :→

this is used when we need to use to use variable that is in another file.

syntax:→

extern data_type name;

usage:→

//file1.c
int ayush=14;
//file2.c
extern int ayush;

25>Explain static type of variable in C.

static is a keyword that limit the scope of var or make it call when the fxn call.

so there are 2 type of variable:→

a>static local var:→

that preserve the variable and call it when the fxn call or need.

static int ayush=18;

a>static global var:→

it limit the var scope and make it accessable for the file only.

static int ayush;

26>Explain working principle of the directive: #define N 10 in a C code.

so define is used when we are define the constants.

if we define that this is: 10 then when we use n that means it’s always denote 10. like see the ex.


27> all about all loop with flow chart.

there are three loops in c

they are

a>for loop

b>while loop

c>do-while loop.

1> flowchart

while:→

do-while :→

for :→


2>entry and exit controled loop :→

exit-controlled loop :→

1>means how the code will exit depend on the loop.

2> first the body of the code will execute then the loop condition will be checked.

3> at least on time the code will run.

ex:→ do-while


entry controlled loop:→

1>when entry(block of code) in the loop depend on the loop .

2>fist the condition will be checked if true the body of the code will fun.

3>if cond is false the whole loop and body of the loop will nit work.

ex:→ while for


3>syntax:→

while:→

//intial codn.
while(codn){
//code we have to run
}


for :→

for(intialization;condn;i++/d--){
//code we have to run
}


do-while:→

do{
//executioon code
}while(condn.);


usage:→

(when we will use chat code)

while:→

when we know the initial codn.

for:→

when we know how many time the code will run.

do-while:→

when at least on time we have to write the code.


28>How many ways can one 1D array be initialized. Explain with example

there are 3 type of initialization :→

1>all at once:→

2>one by one dec

3>partial dec


29>What do you mean by Strings? Explain with example

string in c must be null terminated.

%s format speacifeir is used to print the string.

as represented by array of characters.


output :→


30>strcpy(d,s) :→

use to copy string



31>What will happen if you try to put so many values into an array when you initialize it that the size of the array is exceeded?

If you try to initialize an array with more values than its defined size in C, it will result in a compile-time error. The C compiler checks the size of the array and the number of initializers provided, and if they exceed the declared size, the program will not compile.


32>basic structure of c and stdio.h :→

1> preprosesor directive(stdio.h) :→

1>it refer to strandard input and output header file.

2>it has many pre-built functions like :→ printf(),scanf(),getchar,putchar etc.

printf() => used to print on the screen

scanf() => take input from user.


see q 22 for main fxn and fro return type after program is suceessfull it return 0


33>What do you understand by identifiers and keywords ?

  • Identifiers are names given to user-defined elements (variables, functions) in C, following specific rules.

Keywords are reserved words predefined in C, each having a special meaning or function in the language.

int age;
//int is keyword
//age is indentifier