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

Create a program that will display the sum of all numbers from 1 up to the input number?

to print the sum of first ten numbers:-

void main()

{int i,sum;

sum=0;

while(i<=10)

sum=sum+i;

i=i+1;

}

printf("The sum is : %d",sum);

}

How do you write a C program to calculate the factorial within recursive function?

// Iterative solution

unsigned long iterativeFactorial(const unsigned long n) {

unsigned long i, factorial = 1;

for(i = 1; i <= n; i++) {

factorial *= i;

}

return factorial;

}

// Recursive solution

unsigned long recursiveFactorial(const unsigned long n) {

if(n <= 1) {

return n;

}

return n * recursiveFactorial(n - 1);

}

// Sample calls

int main() {

unsigned long n;

printf("Enter a number to find its factorial: ");

scanf("%u",&n);

printf("Iterative factorial: %u\n", iterativeFactorial(n));

printf("Recursive factorial: %u\n", recursiveFactorial(n));

return 0;

}

Why dont you write ampersand while using strings in scanf?

scanf() is an input function.

It takes an input from the user and store it in a variable.

Since "&" is a reference operator and it represents the memory location of the variable.

String is a type of array and an array consumes more than one memory address.
It is obvious that no need to use "&" operator for an entire array.

For an individual data type like int , char , float etc , "&" is needed but for an array no need to use "&".

What does int A mean in c language?

It is easy to tell with function printf:

int unknown_value;

...

printf ("unknown value is %d\n", unknown_value);

Note: the typical value-range for type integer is -32768..32767 (16 bits), or -2147483648..2147483647 (32 bits).

What is the ASCII value of space key?

Keys haven't got ASCII codes. You might mean the scan code which is returned by functions like getch(TurboC) and ReadKey(TurboPascal) prefixed by a zero value.

Left: 0, 75
Right: 0, 77
Up: 0, 72
Down: 0, 80
PgUp: 0, 73
PgDn: 0, 81
Home: 0, 71
End: 0, 79
Ins: 0, 82
Del: 0, 83

Write a program which will read a string and rewrite it in the alphabetical order?

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main(void)

{

int i,k,n;

char name[50],temp;

printf("\n enter no.of persons");

scanf("%d",&n);

for(i=0;i<n;i++)

gets(name[i]);

for(k=0;k<n-1;k++)

{

for(i=0;i<n-k-1;i++)

{

if(strcmp(name[i],name[i+1])>0)

{

strcpy(temp,name[i]);

strcpy(name[i],name[i+1]);

strcpy(name[i+1],temp);

}

}

}

printf("\n sorted list \n");

for(i=0;i<n;++i)

puts(name[i]);

}

What is the difference between linked list and Array and which is better?

An array is a consecutive chunk of memory with a fixed size that can be accessed directly and randomly.

A linked list has no fixed size (it can grow or shrink) and does not fit in a block of memory the way an array does.

Depending on your usage requirements either might be better than the other. If you require fast, random, direct access to an element and don't have to worry about changing the size of a list than an array may work out better for you than a linked list.

If you don't know the maximum number of elements in your list, or it can grow or shrink, and you don't need to access any elements directly or randomly in a frequent manner, than perhaps the linked list will work out better for you.

What are the limitations of flowchart?

Sometimes the limit is the size of the page.

In today's technological world, however, this limitation has been eliminated or significantly reduced.

Limitations include:

  • size of the diagram depends on the complexity of the logic
  • some programmers can't read data flow diagrams

What does int format mean?

In programming, "int format" typically refers to the representation of integer values as strings or within specific output formats. For example, in Python, the format() method or f-strings can be used to control how integers are displayed, such as specifying the number of digits, padding with zeros, or formatting as hexadecimal. This allows developers to present integer data in a more human-readable or context-appropriate way.

What are the difficulties in writing programs in machine language?

Machine language is error prone simply because it is so difficult to write programs using nothing more than binary notation. Every instruction, every register and every memory address has to be converted to binary. Just one bit out of place would render the code invalid. Trying to find one errant bit amongst many millions is no easy task.

Of course we don't actually use binary (base-2) to write machine code. Any base that is itself a power of 2 will do. Binary is 2^1 while 2^2 is base-4, 2^3 is base-8 (octal) and 2^4 is base-16 (hexadecimal). The advantage of using these higher bases is that instead of entering machine code one bit at a time, we can enter a group of bits using just one symbol. If we divide binary data into groups of 2 bits, then there can be only 4 possible combinations (00, 01, 10 and 11). Base-4 only has 4 possible digits (0, 1, 2 and 3) so it is trivial to map each group of 2 bits to just one base-4 digit. If we divide binary data into groups of 3 bits then there would be 8 possible combinations, each of which can be represented by just one octal digit. And if we divide into groups of 4 bits we get 16 possible combinations each of which can be represented with just one hexadecimal digit. A byte is usually 8 bits long, so if one hexadecimal digit represents half a byte (a nybble) then two hexadecimal digits represent a full byte.

Converting from hexadecimal to binary is so trivial that we can easily write a simple machine code program to perform the conversion for us. This reduces the chances of making errors during input by a factor of 4. However, reducing the chances of error doesn't eliminate errors. Programmer's are only human and if a programmer can make mistakes using a high-level language, then the odds of making a mistake in low-level machine code is greatly multiplied. No matter how much time you spend designing, one mistake is all it takes. And if a mistake is made, finding it is easier said than done.

To reduce the chances of mistake even further, it is best to steer clear of machine code altogether and use assembly language. Assembly language is a symbolic language, so just as binary values can be symbolised through hexadecimal notation, operation codes can be symbolised through easy-to-remember mnemonics. That is, instead of having to remember that code 80 moves a memory address into a register and that 81 moves a register value to that memory address, we need only remember that MOV does both and will translate to the appropriate instruction according to the operands we provide. Assembly language also allows us to use any notion that is most convenient, including decimal, octal, hexadecimal or even binary itself. Every register has a unique identifier and we can also give static, global and constant data user-defined names, so we don't need to keep track of every memory address. We can also insert comments!

Although assembly language is not machine code, the translation from assembly language to machine code is virtually 1:1, there is very little in the way of abstraction. We still won't eliminate programming errors, but at least we have a fighting chance of locating them and ultimately fixing them.

Writing machine code programs in assembly is nonetheless a laborious process. So much so that machine code programmers often prefer to use high-level languages like C and C++ to do the bulk of the work to generate the assembly language which they can then tweak further by hand where necessary. However, C++ in particular produces such good machine code by itself that programmers rarely need to "bang the metal". Indeed, many of the optimisations utilised by the C++ compiler came from the tricks of the trade used by machine code programmers themselves. As a result, C++ can often produce machine code that surpasses anything that can be produced by hand -- but even when it can't, tweaking machine code is so much easier when you don't have to write every single bit from scratch.

Who invented c language and where it is used?

C is a general-purpose language which has been closely associated with theUNIX operating system for which it was developed - since the system and most of the programs that run it are written in C. Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-7. BCPL and B are "type less" languages whereas C provides a variety of data types. In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan & Ritchie caused a revolution in the computing world.

What is public in c plus plus?

public is an access-modifier that allows you to access methods or properties of a class from outside of the class. A method or property set to protected can only be accessed from within the creating class or subclasses, while a private method or property can only be accessed from within that class

Can you declare a variable in different scopes with different data types elaborate with examples?

Yes. Because a variable of the more local scope is what is used instead of any other variables with that name, the data types of the other variables that use that name are irrelevant.

It is, however bad practice to do this because it is confusing and often leads to unintentional function behaviour (particularly in weakly typed languages). If the variable in question is meant to be used in different functions and with a different purpose, there is probably no need to give it the same name as another.

Why pointer is callded jewel of c language?

a pointer is a derived data type in c. pointers are undoubtedly one of the most distinct and exciting features of c language.it has added power and flexibility to the language.

*pointers are more efficient in handling arrays and tables.

*pointer can be used to support dynamic memory management.

*pointers reduce length and complexity of programs.

*increase the execution speed and thus reduce the program execution time.

by following character's real power of c lies in proper use of pointers. pointer is called the jewel of c-language.

Write a C plus plus program to interchange two numbers by using function?

#include<iostream>

template<typename _Ty>void swap (_Ty& a, _Ty& b)

{

_Ty temp = a;

a = b;

b = temp;

}

int main()

{

int x = 42;

int y = 0;

std::cout << "Before swap:" << std::endl;

std::cout << "x = " << x << ", y = " << y << std::endl;

swap (x, y);

std::cout << "After swap:" << std::endl;

std::cout << "x = " << x << ", y = " << y << std::endl;

}

Example Output

Before swap:

x = 42, y = 0

After swap:

x = 0, y = 42

What are the latest trends and technologies in printing?

Business cards are an invaluable asset for any professional or businessperson. The humble business card is not just a calling card bearing an individuals name and contact details: it acts as a promotional tool for the company and its products, and serves to inform people of an individuals role within that company. For this reason business cards are every bit as important for the staff and salespeople in a small business as they are for the CEO of a multinational blue-chip company.

Business cards date back to the 15th Century in China and the 17th Century in Europe, and were originally quite simple: black text on white card stock. However advances in design and print technology meant that they became more elaborate over time, and in addition to the name, job title and contact details of the bearer, todays professional business card will usually include a striking visual design, a corporate font and a unique company logo.

Some business cards, particularly those of salespeople and real estate agents, may also include a photograph of the employee, but the one thing all companies want is a uniform business card style to ensure that the company looks professional to its customers. This is known as the Brand Identity or the Corporate ID, and is usually extended to any other printed stationery the company may use such as letterheads and compliment slips.

Special Materials

The vast majority of corporate business cards are printed on 320~350gsm white card. However, business cards can be printed on a wide variety of materials, and designers have taken advantage of that in recent years to produce durable plastic cards (frosted translucent plastic being the most popular), metallic business cards, wooden business cards, and even magnetic business cards: magnetic cards are particularly popular with tradesmen such as plumbers and electricians because customers can stick them on the door of their refrigerator to save them the trouble of looking in the Yellow Pages.

Digital vs Litho

There are two main methods of printing business stationery: digital and litho. The decision about which one to use usually comes down to speed and cost. Digital printing is very fast and very cost-effective for short-run print jobs such as individual sets of business cards, whereas litho printing is more cost-effective for longer runs.

Litho printing uses individual Pantone colour inks and the press needs to be cleaned down between the application of each colour. This means that the setup costs are more expensive than for digital printing, and the process also takes longer. In addition, litho print requires extra time for the ink to dry before the it can be guillotined. If the ink is not dry, there is a risk of set off which is when the ink from the front of one page rubs off onto the reverse of another.

Digital presses, on the other hand, use a combination of four colours (Cyan, Magenta, Yellow, and Black, or CMYK) to create all the colours in the rainbow. There are no costs involved in setting up the press, no cleaning in between colours, and no drying time. This makes it the ideal method for printing business cards, and ensures that urgent orders can be printed and despatched within a few hours, something which would be impossible if they were printed litho.

Online Ordering

Recent innovations in computer technology and graphic applications have seen the rise of some print companies offering an online ordering service for business cards and other printed stationery. In some cases this may simply be an web form which the user fills in to request an order, but some of the more progressive print companies have invested in sophisticated online procurement software which enables customers to enter their details online and view instant on-screen proofs of their printed stationery before the order goes to print. There are a number of very real advantages to using a system like this, particularly for larger companies which need to order business cards regularly.

First of all, a sophisticated online print system will preserve the companys Corporate ID across the entire range of stationery and this will guarantee that each print run conforms to the companys print guidelines: it removes the need for a graphic designer to create the artwork from scratch for each order, and that means there are no mistakes.

In addition, an online print system will also enable users to place orders whenever they need to, whether it be at the weekend or at 2am: no need to speak to the printer, no need to wait for the proofs, no need to correct the typo errors which the graphic designer has made orders can be placed quickly and efficiently online any time of the day or night, and the proofs can be checked instantly and even emailed to a colleague for approval. These efficiencies of time also translate into cost-savings, both for the customer (who doesnt need to waste valuable time chasing the printer for proofs and corrections) and for the printer (who can employ their graphic designers time more productively).

Dual-Language Business Cards

Finally, one very important aspect of our technology-driven, ever-shrinking world is the increase in international trade. Reduced travel costs and open trade agreements mean that import/export is no longer the preserve of multinational corporations, but is now also open to smaller companies: it has never been easier to trade abroad. This has led to a dramatic rise in the requirement for dual-language business cards, particularly for companies trading overseas in South East Asian countries such as Japan, China and Korea, but also increasingly in Arab countries too.

The problem here is that whilst printers may be very good at putting ink on paper, and some of the more innovative among them may even have ventured into online ordering, there are no more than a very small handful of printers in Western countries who know anything about translating or printing foreign languages, particularly complex scripts (ie, anything beyond the Western alphabet). However, it is vital to the success of any overseas trade negotiations that the company looks as professional as possible to its foreign customers, so it comes as little surprise that expert online print companies such as Biznis Cards which provide specialist translation and foreign-language typesetting services are becoming more and more popular.

What is the significance of declaring a constant unsigned integer?

What is the significance of declaring a constant unsigned integer?

Write a C program to print the pattern 1 01 101 0101 10101?

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,k=0,l;

clrscr();

for(i=1;i<=5;i++)

{

for(j=1;j<=i;j++)

{

k++;

printf("%d ",k%2);

}

for(l=i;l<=4;l++)

{

k++;

}

printf("\n");

}

getch();

}

Algorithm for converting prefix to postfix using stack?

/*Infix to Prefix And Postfix*/

/*Assignment:5*/

/*Roll No:2102*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 15

#define true 1

#define false 0

/*Structure Decvlaration*/

typedef struct

{

char data[MAX];

char top;

}STK;

/*Function Declarations*/

void input(char str[]);

void intopre(char str1[],char pre[]);

void intopost(char str1[],char post[]);

int isoperand(char sym);

int prcd(char sym);

void push(STK *s1,char elem);

int pop(STK *s1);

int empty(STK *s2);

int full(STK *s2);

void dis(char str[]);

void main()

{

STK s;

int cs,ans;

char str[MAX],pre[MAX],post[MAX];

clrscr();

do /*Using Do-while Loop*/

{

clrscr();

printf(" -----Program for Expressions-----");

printf(" Input The String:");

printf(" MENU: ");

printf("1.Infix to Prefix ");

printf("2.Infix to Postfix");

printf(" 3.Exit");

cs=getche();

switch(cs) /*Using Switch Case*/

{

case 1:

intopre(str,pre);

break;

case 2:

intopost(str,post);

break;

case 3:

break;

default:

printf(" Enter a Valid Choise!"); /*Default Case*/

break;

}

printf(" Do you wish to Continue?(y/n)");

ans=getche();

}while(ans=='y'ans=='Y'); /*Condition for Do-while loop*/

getch();

}

/**************************************************/

/*To Input String*/

/**************************************************/

void input(char str)

{

printf("Enter the Infix String:");

scanf("%s",str);

}

/**************************************************/

/*To Covert Infix To Prefix*/

/**************************************************/

void intopre(STK s1,char str1[],char pre[])

{

int len,flag;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

char elem;

while(cnt>=0) /*while condition*/

{

flag=0;

if(isoperand(str1[cnt])) /*Checking for Operand*/

{

printf("%c",str1[cnt]);

cnt--;

pos++;

}

else

{

check=prcd(str1[cnt]);

while(check==false)

{

pre[pos]=str1[cnt];

flag=1;

pos++;

cnt--;

}

if(flag==0)

{

elem=pop(&s1);

printf("%c",elem);

}

}

}

}

/**************************************************/

/*To Convert Infix To Postfix*/

/**************************************************/

void intopost(STK s1,char str1[],char post[])

{

int len;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

}

/**************************************************/

/*To Check For Operand*/

/**************************************************/

int isoperand(char sym)

{

if('A'<sym<'Z''a'<sym<'z')

return(true);

return(false);

}

/**************************************************/

/*To Check The Precedence*/

/**************************************************/

int prcd(char sym)

{

}

/**************************************************/

/*To Display String*/

/**************************************************/

void dis(char str[])

{

}

/******************************************/

/*Push Function Definition*/

/******************************************/

void push(STK *s1,char elem)

{

if(!full(s1))

{

s1->top++; /*Incrementing top*/

s1->data[s1->top]=elem; /*Storing element*/

}

else

printf("

Stack is Full!");

}

/******************************************/

/*Full Function Definition*/

/******************************************/

int full(STK *s2)

{

if(s2->top==MAX) /*Condition for Full*/

return(true);

return(false);

}

/******************************************/

/*Pop Function Definition*/

/******************************************/

int pop(STK *s1)

{

char elem;

if(!empty(s1))

{

elem=s1->data[s1->top]; /*Storing top stack element in elem*/

s1->top--; /*Decrementing top*/

return(elem);

}

return(false);

}

/******************************************/

/*Empty Function Definition*/

/******************************************/

int empty(STK *s2)

{

if(s2->top==-1) /*Condition For Empty*/

return(true);

return(false);

}

Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this:

MyClass var = new MyClass();

Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler.

public class MyClass{

// The java compiler will insert a real constructor here

public Integer MyClass(){ //This isn't a constructor, only a simple method

return new Integer(1);

}

}

What the Advantages of while loop over the loop?

Here you can initialize, check for condition and incriment the variable at the same time inside in the for loop

like

for(int i=0;i<10;i++)

and for other looping structures you need to have these three things at different places.

What are the advantages and disadvantages of data structure stack?

Advantages (Pros)

* Easy to get started

* Low Hardware Requirement

* Cross- Platform

* Anyone with access can edit the program

Disadvantages (Cons)

* Inflexible

* Lack of scalability

* Unable to Copy & Paste

What is worst case and average case complexity of linear search algorithm with explanation?

For a list with n elements, the expected cost is the same as the worst-case cost, which is O(n). The average cost will be O(n/2). However, if the list is ordered by probability and geometrically distributed, the complexity becomes constant, O(1). Compare with a binary search which has a cost of O(log n).