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

What is the Use of main function in c language?

Any program source has to identify a memory location as the start of the program. The OS transfers control to this point of the program or module.

In case of C programs, the starting module is NOT your program, but another initialisation routine, which has name lis c0s.obj, c0l.obj etc. This routine after doing initial housekeeping work, like storing program name, arguments, environment variables etc. in the stack, calls a routine by name main.

How else are we going to tell the OS where to start ?

At the end of executing the main() function, the control goes back to the routine from which the main was called and then to OS, which terminates the program.

Why were high-level languages developed?

Because machine-language programming is extremely time-consuming, programmers began using English-like abbreviations to code the programs. High-level languages were developed to improve programming efficiency by requiring fewer coding statements.

How does a for loop start?

It starts with a boolean (true or false) expression. If the expression evaluates to true then the following statement or block of statements is run.

For example:

int num = 5;

while (num < 9) {

<code that does something goes here>

num++;

}

In this example the contents of the while loop will be executed 4 times

Write a program to generate the Fibonacci Series using array?

#include<stdlib.h>

#include<conio.h>

#include<stdio.h>

void main (void)

{

clrscr();

int i;

int a[10];

a[0]=0;

a[1]=1;

printf("First 10 Fibonacci numbers are :\n");

printf("%d\n%d\n",a[0],a[1]);

for(i=2;i<10;i++)

{

a[i]=a[i-1]+a[i-2];

printf("%d\n",a[i]);

}

getch();

}

Which data structure allows deleting data elements from front and inserting at rear?

A linked list is an example for a data structure that allows removal from one end and insertion of new items to the other. Many other data structures support the same operations. They differ in what else they can do, and how efficient the operations can be performed.

C program to check given number is a perfect square or not?

#include <stdio.h>

#include <math.h>

int main(void)

{

int number;

double result;

printf ("\n Introduce an integer: ");

scanf ("%i", &number);

result= sqrt (number);

if ((result * result)== number)

printf ("\n The integer HAS a perfect square \n\n");

else

printf ("\n The integer DOES NOT HAVE a perfect square \n\n");

getch ();

}

Shrikanth Ganure

The Oxford College of Engineering (MCA-2010 Batch)

Bangalore..

Distinguish between the concept assembler compiler and interpreter?

Compiler -- reads human-readable source code, produces machine-executable binary code. Examples are C, COBOL, Java, etc. Easiest for humans to program, but does not always produce the most efficient executables.

Interpreter -- Reads human-readable code, line at a time, and produces and executes machine instructions "on the fly". Example is good old BASIC. Good for testing, but is VERY slow.

Assembler -- Converts machine-manipulation coding directly into binary machine instructions. Produces the most efficient executables, but is the most difficult (for humans) to work with.

Best of all worlds (my opinion) is C on Unix, for speed, ease of use, program efficiency.

What language can a computer always understand?

I have only a vague understanding of your question, but I'll take it as "why computers can understand language". :P.
- they don't
-they only seem to cause it was written in their program.

if it meant "what language computers understand",
-programming language.
-go search up wikipedia on this one.

How do you use Graph in data structure?

Em depends on the points your plotting on the graph. If they are evenly distributed then it's probably a linear graph.

How will be the c program for reversing first n characters in a file?

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <process.h>

void main(int argc, char *argv[])

{

char a[15];

char s[20];

char n;

int k;

int j=0;

int i;

int len;

FILE *fp;

if(argc!=3)

{

puts("Improper number of arguments.");

exit(0);

}

fp = fopen(argv[1],"r");

if(fp == NULL)

{

puts("File cannot be opened.");

exit(0);

}

k=*argv[2]-48;

n = fread(a,1,k,fp);

a[n]='\0';

len=strlen(a);

for(i=len-1;i>=0;i--)

{

s[j]=a[i];

printf("%c",s[j]);

j=j+1;

}

s[j+1]='\0';

getch();

}

Give an example of pseudocode program?

Pseudocode is a non-specific programming language that is used to outline an algorithm's logic using plain-English terms and sentence structures, but often incorporating the grammar of well-known programming constructs such as loops and branches.

For example, the following pseudocode demonstrates a loop that prints the values from 1 to 10:

let number be 0

repeat while number is less than 10

increment number

print number

This is a simple example, however anyone familiar with at least one programming language can translate this pseudocode into their chosen language without too many problems. The point of pseudocode is to reduce complex algorithms to their simplest form, without being overly-specific with regards an actual programming language. That is, the focus is purely upon the algorithm itself, rather than the idiosyncrasies of specific coding methods.

Pseudocode is not a programming language, per se, so there are no grammar rules regarding syntax or sentence structure. However, each line of code should be short and to the point (not overly-wordy), clearly conveying the point of the code. Whitespace and indentation should be used wherever necessary to convey the separation of code blocks (as shown in the example), or to break down complex lines into bite-size chunks that clearly show the logic.

Write a program To print the reverse order of a string?

//code for reverse string

class ReverseString

{

public static void main(String arg[])

{

StringBuffer s=new StringBuffer("abcdef");

System.out.println("Before reversing :"+s);

s.reverse();

System.out.println("after reversing.."+s);

}

}

Which symbol is used as a statement terminator in C?

semicolon ';' (Not applicable for block-statements)

What is the difference between switch case and if else?

If else can have values based on constraints, where as switch case can have values based on user choice.In if else, first condition is verified, then it comes to else whereas in the switch case first it cheaks the case and then it switches to that particular case.

What do you mean by prototype of function?

Building a prototype allows the designer to explore unforeseen problem areas before building the finished product. Prototypes can also be used for testing to see how a device will operate in real-world conditions as opposed to computer projections, and to determine production cost and propose a budget.

Write a program to implement stack in openGL?

Module Module1

Sub Main()

myQuequeue()

myStack()

Console.ReadLine()

End Sub

Private Sub myQuequeue()

Dim theQ As New Queue

Dim dStr, frmstrArr() As String

Dim i As Integer

frmstrArr = FillArr()

For i = 0 To 25

theQ.Enqueue(frmstrArr(i) + " queue")

Next

For i = 0 To 25

dStr = theQ.Dequeue()

Console.WriteLine(dStr)

Next

End Sub

Private Sub myStack()

Dim theStack As New Stack

Dim dStr, fStr() As String

Dim i As Integer

fStr = FillArr()

For i = 0 To 25

theStack.Push(fStr(i) + " stack")

Next

For i = 0 To 25

dStr = theStack.Pop()

Console.WriteLine(dStr)

Next

End Sub

Private Function FillArr()

Dim strArr(25) As String

strArr = New String() {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

Return strArr

End Function

end module

What is the difference between int main and int void?

int main() refers that main function returns an integer value, where the integer value represent the program execution status. This status value is sent to the Operating System. If you follow strictly follow the rules, then it should be int main not void main()

How does syntax affect setting?

Syntax is essential to any form of language. Without syntax, there is no setting, there is no way to explain it and no language. Language and setting are both comprised with syntax.

Write a C program to display days in a week using single dimension array?

In any programming language, an array is a list of items ordered sequentially (one after another).

C arrays require that each item in an array is of the same type. To declare an array called intlist of ten integers, for instance, the following statement is used:

int intlist[10];

You can access the first item in intlist by using intlist[0], since all array indexes in C are zero-based. The last item is accessed using intlist[9] - 0 to 9 makes for 10 total integers.

A string, like a day of the week for instance, in C is an array of characters. C treats all strings as zero-terminated or NUL-terminated - the last index contains the value 0, which is called NUL (which is not to be confused with NULL, with two L's, which is used for pointers).

The following two string definitions result in the same data:

char hellostr[6]="Hello";

char hellostr[6]={'H', 'e', 'l', 'l', 'o', 0};

Alternately, and this is under C, you can also use:

char *hellostr="Hello";

Under C++, you would need to use:

const char *hellostr="Hello";

But that's outside the realm of this discussion.

What this does is forces the compiler to allocate memory for "Hello" (a string constant) followed by the value 0 (zero), and then point hellostr to the location of that string constant.

That takes care of strings. Now, for arrays of strings.

What the exercise is expecting you to do is store the name of each day in a week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday) in a string. This requires an array of 7 strings.

Because a string is an array of characters, you will need an array of an array of strings. Fortunately, string constants come to the rescue. If I wanted to store a list of colors, I could use the following definition:

char *colorlist[6]={"Red", "Orange", "Yellow", "Green", "Blue", "Purple"};

Then use the following code to access the colors in a loop:

for (count=0; count<6; count++)

printf("%s is a fine color!\n", colorlist[count]);

All you have to do now is code the main() function, replace some strings, add a variable declaration, compile, test and debug as required. Be sure to rename the variable colorlist as well to avoid confusion,

See the related links below for some handy C tutorials. Choose one (or more) which best suit(s) your needs.

Which algorithm has some average worst case and best case time?

All algorithms have a best, worst and average case. Algorithms that always perform in constant time have a best, worst and average of O(1).

Write the algorithm of quick sorting?

1- set pass =1

2- repeat step 3 varying j from 0 to n-1-pass

3- if the element at index j is >than the element at index j+1 swap the two element

4- increment pass by 1

5-if pass is <=n-1 go to step 2

What is the solution of towers of hanoi problem?

This algorithm reads the value of number of discs and prints the move that are to be done for playing towers of hanoi.

Pre: n is a positive integer which carries the value of number of discs.

Post: prints the moves that are to be done for transferring discs in ascending order in another peg

1.initialize value of n(positive integer greater than 1)

2.moves = pow(2,disk)-1

3.if(n=1)

i) moves disk from "from" to "to"

4. end if

5. else

i)hanoi(n-1,from,aux,to)

ii) moves disc from "from" to "to''

iii) hanoi(n-i,from,aux,to)

6.end else

7. prints the moves

end towers_of_hanoi

What is meant by entry controled loop?

An exit-controlled loop is a loop where the controlling conditional expression is evaluated at the end of each iteration, as opposed to an entry-controlled loop where the expression is evaluated at the beginning of each iteration. The upshot is that an exit-controlled loop always executes at least one complete iteration whereas an entry-controlled loop might not iterate at all:

int x = 100;

// entry-controlled loop

while (x<50) { printf ("%d\n", x++); // won't execute at all because x<50 is false }

// exit-controlled loop

do {

printf ("%d\n", x++); // will execute one time only

} while (x<50);

Why do we need type casting in programming?

Becuase, sometimes data is in a form that is unrecognizable to the program. Example:

int a = 2;

int b = 3;

double c = a / b;

We cast c as a double. if it was not, then the data would not have a decimal value.