answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Why do you need data types in c language?

All programming languages require types. Some are dynamically-typed while others are statically-typed. C is a statically-typed language. Static-typing means values are bound to types at compile time, thus optimising storage requirements and rejecting invalid operations at compile time. Dynamic-typing means values are bound to types at runtime. While dynamic-typing is more flexible and generally easier to work with, that flexibility comes at a cost in terms of performance and maintenance. Invalid operations will result in runtime errors but, even if no errors occur, the runtime must still test for them, resulting in decreased performance and increased code size. Since C can eliminate type-based errors at compile time, most runtime-checks become redundant, code is more compact and performance is optimal.

How to write a c program to convert analog to digital signals using a sound port?

You cannot. Before we explain why, let's first examine how we actually record audio, then we'll look at how we convert it to the digital domain.

In order to record a sound wave, we first have to capture the vibrations made by the sound. Before the advent of electrical recording, this was achieved using a horn. This transmitted the sound to a moving stylus which vibrated in sympathy with the sound. When the vibrating stylus was brought into contact with a rotating cylinder, it etched a groove in the surface. As well as rotating, the cylinder also moved horizontally, thus the groove spiralled across the outer face of the cylinder. Once recorded, the same device could be used for playback; the rotating cylinder vibrated the stylus and those vibrations were amplified by the horn, reproducing the original sound.

With electrical recording we use a microphone instead of a horn. A microphone is simply a loudspeaker working in reverse. The microphone diaphragm vibrates in sympathy with the sound wave, which in turn moves a coil back and forth within a magnetic field, thus creating an electrical signal which is analogous to the sound wave. If we pass that same signal to a loudspeaker, we can recreate the original sound, just as we did with the horn and stylus.

The moving coil has a limited range of movement within the magnetic field. If we say that -1 represents the full extent inwards and +1 the full extent outwards, it follows that 0 is the transition point between the positive and negative phases of the sound wave. 0 is also the resting point of the coil when there is no sound at all. Although we are imagining numbers at this point, they are completely imaginary; we are still in the analog realm not the digital domain.

In order to convert the electrical signal for the digital domain, we need to take a snapshot of the coil's position at a given moment in time. For accurate reproduction we'd need an infinite number of snapshots, however the human ear is quite forgiving and we find that 44,100 snapshots per second (44.1 KHz) is sufficient to reproduce sound with high-fidelity. This is because the human ear has a frequency range of 20 Hz to 20 KHz, and we need at least twice as many samples per second to capture both the positive and negative phases of 20 KHz sound. CD-quality audio is 44.1 KHz for that reason. Once recorded we cannot improve the fidelity -- we can only lose fidelity -- however by using a higher frequency sampling rate, such as 96 KHz, we have sufficient fidelity to cater for all forms of mainstream digital media (such as BluRay). Fidelity is the physical difference between the original sound and the recorded playback of that same sound. At 44.1 KHz, most people won't notice any difference at all even though the physical difference is quite substantially different.

As well as the sample rate we need to consider how we store each sample. Given that the coil moves within a working range of -1 and +1, the position of the coil at any given moment can be converted to a real number within that range. However, although the range is relatively small, the set of real numbers within that range is as infinite as the set of all numbers; it's what we call a "small infinity" because it's an infinite set within a much larger set of infinities.

The only way to digitally represent an infinite set of values is with an infinite number of digits or bits. That's clearly impossible, so we need to limit our range in some way. With 16-bit recording we have 65,536 unique values available (all integers in the range -32,768 to +32, 767), thus we must scale our set of real numbers to fit within this range, rounding up or down to the nearest integer. Each additional bit doubles the range of integers thus the fidelity of 24-bit is 2^8 times that of 16-bit.

Now -- to answer the actual question -- converting analog audio to digital audio cannot be achieved with software of any kind, it can only be achieved with hardware. This stands to reason because software can only operate upon digital information stored in the computer's memory which means the conversion has to happen before the software "sees" that information; it cannot process what it cannot "see". But hardware can easily convert an analog electrical signal into a digital one, it's simply a matter of designing the appropriate circuitry.

To convert audio signals from analogue to digital we need a device known as an analogue to digital converter or ADC. The ADC takes a bandwidth-limited analog input (anti-aliased to eliminate all frequencies greater than half the sample rate) and produces a digital output. Once converted to a digital form, we can easily store it in a computer's memory. The conversion itself is trivially simplistic and is really no different to the way we would manually scale a real number in the range -1 and +1 to an integer in the range -32,768 and +32,768.

We can adjust the conversion insofar as we can amplify or attenuate the ADC input signal (the input gain), the sampling frequency and the bit-depth. But while we can perform these adjustments with software (communicating via the hardware's device driver), the conversion process itself is handled entirely by the hardware.

The reverse process is achieved with another piece of hardware known as a digital to analog converter or DAC, which converts the digital values back to electrical signals of the appropriate amplitude.

Note that not all sound originates in the analog world but must be converted to the analog world in order for us to hear it. For instance, computer-generated sound is entirely digital but until we pass it through a DAC (and subsequently through an amplifier and speaker) we cannot hear what it sounds like.

How c strings are terminated?

c strings are terminated by \0 character

How can you search an array element in a file?

Logic to search element in array

Input size and elements in array from user. ...

Input number to search from user in some variable say toSearch .

Define a flag variable as found = 0 . ...

Run loop from 0 to size . ...

Inside loop check if current array element is equal to searched number or not.

To learn more about data science please visit- Learnbay.co

Write a shell script to print given number in reverse order in Unix?

# Algo:

# 1) Input number n

# 2) Set rev=0, sd=0

# 3) Find single digit in sd as n % 10 it will give (left most digit)

# 4) Construct revrse no as rev * 10 + sd

# 5) Decrment n by 1

# 6) Is n is greater than zero, if yes goto step 3, otherwise next step

# 7) Print rev

#

if [ $# -ne 1 ]

then

echo "Usage: $0 number"

echo " I will find reverse of given number"

echo " For eg. $0 123, I will print 321"

exit 1

fi

n=$1

rev=0

sd=0

while [ $n -gt 0 ]

do

sd=`expr $n % 10`

rev=`expr $rev \* 10 + $sd`

n=`expr $n / 10`

done

echo "Reverse number is $rev"

What is difference between for loop and do-while loop?

The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action:

do {

System.out.println("Inside do while loop");

} while(false);

The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.

How can you make a splash screen in c or c plus plus?

Many people use a modified version of the program's About dialogue window as a splash screen, but you can also create a dedicated splash screen window that contains an image of the company logo along with some program information. The choice is yours but ultimately a splash screen is nothing more than a window that is shown while a program loads in the background. When the program has loaded, the splash screen is closed automatically.

The simplest method of displaying your splash screen is to use a program loader function. Typically this will be your program's main function or a function called from your main function. The program loader first loads and displays the splash screen, then loads the rest of your program in the background before closing the splash screen window and finally showing the main window.

Some splash screens display progress information during the load sequence, thus your program loader should send messages to the splash screen and give time to the system to allow messages to be processed. This will increase the time it takes to load your program, but if the load process is lengthy to begin with, it is better to give some indication of the progress.

What is the need for indentation in while writing a c plus plus program?

Indenting is a good habit to inculcate while writing any type of code

Although indentation is not mandatory and will never affect the working of your programme in C++ it makes the code more easy to read and debug especially for larger programmes. Most IDE's eg eclipse automatically indent the code as you type.

Write a C program to find the area and perimeter of you square ii rectangle?

#include<stdio.h>

void main()

{

float length, breadth, area, perimeter;

printf("Enter the length & breadth of a Rectangle\n(length breadth): ");

scanf("%f %f",&length,&breadth);

area=length*breadth;

perimeter=2*(length+breadth);

printf(" Area of the Rectangle=%0.3f\n",area);

printf("Perimeter of the Rectangle=%0.3f\n",perimeter);

printf("(Press ENTER to exit.)");

getch();

printf("\n");

}

Insert an element after an element in an array?

  1. To begin, obtain the element to be added, such as x

  2. Then, say pos, get the position where this element will be put.

  3. Then shift the array items one position ahead, then do the same for all the other elements next to pos.

  4. Because the location pos is now empty, insert the element x there.

To learn more about data science please visit- Learnbay.co

When you delete an item from the empty stack will the program execute?

If the program correctly checks the error-conditions, it will terminate -- otherwise it will do... something, e.g. using memory-garbage as data.

How do you call a function for expressing thanks?

void expressing_thanks (void)

{

puts ("thanks");

}

int main (void)

{

expressing_thanks ();

return 0;

}

Why the return type of all the function by default is int in c?

Because int is the most common data-type in C. Don't rely on this though, always specify the return type explicitly.

Scope of static variables?

Scope of static variable is with in the file if it is static global.

Scope of static variable is with in the function if variable is declared local to a function.

But the life time is throughout the program

What are C reserved words?

C reserved words are words which cannot be used as names. This includes all the built-in type names, including int, char and double and their aliases, the modifiers long, short, signed and unsigned, and all language keywords such as if, else, for, while, return and so on.

What does camelcase mean in programming?

CamelCase is something I've already used in this answer.

CamelCase is where two or more words are joined together without spaces. To avoid confusion, the first letter of each word is capitalized.

The name came about because the capitals make "bumps" said to resemble a camel...