Can a while statement end in a semicolon?
Yes. A while statement ends in a statement...
while (expression) statement
...and that statement can be a null statement, a single statement, or a block of statements. In the case of the block of statements, there is also a set of braces surrounding them...
while (expression);
while (expression) statement;
while (expression) {
statement1;
statement2;
...
statementN;
}
In the case where the body of the statement is null, there is no body. This is often done while taking advantage of side effects. For instance, to copy a string you could use...
char *strcpy (char *pszDestination, char *pszSource) {
char *pszTemp = pszDestination;
while ((*pszDestination++ = *pszSource++) != '\0');
return pszTemp;
}
...this works because the post-increment (++) operator has higher precedence than the dereference (*) operator, and because the assignment (=) operator has the value of the assignment, which is compared using the not equal (!=) operator against the string terminator null.
Note, carefully, the inner parentheses. They are needed because != has higher precedence than =, and you want it the other way around. Also, some compilers will let you eliminate the != '\0' terms and the inner parentheses, but that is not portable, and most compilers will warn you about assignment in a conditional expression.
In the case of a single statement you could use...
i= -1;
while (++i < argc) printf ("%d %s\n", i, argv[i]);
...here the while statement also ends in a semicolon.
The case of the block of statements is not shown, because it seems to be understood from the context of the question.
How do arrays differ from single memory position variables?
A single memory position variable can store only one value of its type.
An array can store n number of values, where n is the size of the array.
How do you detect a loop in a linked list?
A possibility is tagging. Example:
typedef struct ListElem {
struct ListElem *next;
int tag;
int value;
} ListElem;
int Check (ListElem *l)
{
static int tagn= 0;
++tagn;
for (; l; l= l->next) {
if (l->tag==tagn) return -1; /* loop */
l->tag= tagn;
}
return 0;
}
What is CD in header function?
In the context of HTTP headers, "CD" typically refers to "Content-Disposition." This header field is used to indicate how the content should be displayed or handled by the browser, such as whether it should be displayed inline or treated as an attachment to be downloaded. The Content-Disposition header can specify a filename for the downloaded file and control how the content is presented to the user.
What is the difference between Strings and Arrays in c?
There is no difference. A string is just an array of type char. The only real difference is that we do not need to keep track of the length of a string because strings are null-terminated in C. If a string does not have a null-terminator, then it is just an ordinary array of character values.
The heap refers to the free store, which basically means the total unused memory available to you. The physical amount of memory will vary from system to system and the amount of memory available will depend upon how much is currently in use. However memory fragmentation means that while there may be sufficient physical memory available to meet an allocation request it does not follow that there is a large enough block of contiguous memory available, resulting in an out of memory error.
Modern systems use virtual memory addresses rather than physical ones. This allows the memory manager to physically move objects around in memory without affecting the virtual addresses allocated to those objects and thus makes it possible to consolidate memory fragments. Objects that are not in use can also be paged out to a disk file, thus making it seem like there's far more memory available than physically exists.
Solutions for the Exercises in the book of let us c fifth edition?
/*simple stringout put*/
#include<stdio.h>
int main ()
{charc[2]="A";
printf("%c\n",c[0]);
printf("%s\n",c);
return0;
}
Write a recursive procedure to compute the factorial of a number?
#include <iostream>
using namespace std;
int main()
{
int i, number=0, factorial=1;
// User input must be an integer number between 1 and 10
while(number<1 number>10)
{
cout << "Enter integer number (1-10) = ";
cin >> number;
}
// Calculate the factorial with a FOR loop
for(i=1; i<=number; i++)
{
factorial = factorial*i;
}
// Output result
cout << "Factorial = " << factorial << endl;
What is the program to find the number of each word in the string array?
#include<iostream>
#include<string>
using namespace std;
int main()
{
int count=0;
string b;
string a[6]={"technical","school","technical","hawler","school","technical"};
for(int i=0;i<6;i++)
{
b=a[i];
for(int j=i;j<6;j++)
{
if(a[j]==b)
count++;
}
cout<<a[i]<<" "<<count<<endl;
count=0;
}
return 0;
}
The Big C is the other term for cancer. If your referring to the inspiring TV Series "The Big C", starring Laura Linney and Oliver Platt, its a dark comedy about a woman living with cancer hoping to live life up to the fullest contempt! It's an amazing series that everyone can enjoy. Don't miss the season 4 premiere on April 29th 2013! GO CATHY! "C"
Why does a character constant require two bytes of memory space in c?
It doesn't.
Try this
#include <stdio.h>
#include <wchar.h>
int main (void) {
printf ("sizeof (char)=%d, sizeof (wchar_t)=%d\n",
(int)sizeof (char), (int)sizeof (wchar_t));
return 0;
}
What is drawback of writing the definitions of all the functions before main function?
If you write out the functions before you write main, you will probably have to return to the functions after outlining main and correct them so that they interact with main as they should. In the long run, it is far better to write main first, so that you understand exactly what you want each function to do before you write the function, so that you don't have to go back and correct them.
Weite a your own progrem to find a length of string without using library function?
int mystrlen (char* str) {
int len = 0;
while (*str++ != '\0') len++;
return len;
}
What is program of table in GW basic?
Program code for Creating Two Table for Two number Given By User:
Codes
10 Input " Enter 1st Number for the Table: ", A
20 Input Enter 2nd Number: ", B
30 For I =1 To 10
40 Print A " * " I " = " A*I TAB(20) B " * " I " = " B*I
50 Next I
60 END
Output(assume that given numbers are 3 and 4 for output)
3*1=3 4*1=4
3*2=6 4*2=8
3*3=9 4*3=12
3*4=12 4*4=16
3*5=15 4*5=20
3*6=18 4*6=24
3*7=21 4*7=28
3*8=24 4*8=32
3*9=27 4*9=36
3*10=30 4*10=40
What will be the signature of the function that returns three-dimensional array char arr234?
You cannot return an array by value as all arrays (including multi-dimensional arrays) will implicitly degrade to pointers, so they must be returned by reference instead. However, you cannot return the address of a local variable as that variable will fall from scope when the function returns, so the memory is no longer valid. Therefore the array must be allocated dynamically, on the heap.
You cannot create multi-dimensional arrays on the heap as easily as you can on the stack or in static memory. Essentially you need three separate arrays. The first holds all the actual elements and is allocated by multiplying all the dimensions together with the size of each element. Thus to dynamically allocate the equivalent of a char[2][3][4] array, you would actually allocate a char[24] array (2*3*4=24).
char* array1 = malloc (2*3*4*sizeof(char));
The second array holds pointers into the first array. This array is the equivalent of a char*[2][3] array, but is actually allocated as a char*[6] array (2*3=6):
char** array2 = malloc (6*sizeof(char*));
Once you have this array you need to initialise the pointers so they each point into the first array, where each pointer refers to the start address of a group of 4 elements:
for (int i=0; i<6; ++i) {
array2[i]=&array1[i*4];
}
Finally, you can allocate the third array which holds pointers into the second array and is equivalent to a char**[2] array:
char*** array3 = malloc (2*sizeof(char**));
Again, the pointers must be initialised to point into the second array, where each pointer points to the start address of a group of 3 pointers:
for (int i=0; i<2; ++i) {
array3[i]=&array2[i*3];
}
Putting this all together, we can create a function that dynamically allocates a three-dimensional char array of any size:
char*** create_array3d (unsigned d1, unsigned d2, unsigned d3) {
int i;
char* a1 = malloc (d1*d2*d3*sizeof(char));
char** a2 = malloc (d1*d2*sizeof(char*));
char*** a3 = malloc (d1*sizeof(char**));
for (i=0; i<(d1*d2); ++i) {
a2[i]=&a1[i*d3];
}
for (i=0; i<d1; ++i) {
a3[i]=&a3[i*d2];
}
return a3;
}
Thus the signature for this function will be:
char*** create_array3d (unsigned, unsigned, unsigned);
The problem with this function is how we go about releasing the memory given we only have one pointer. The easiest way is to create another function:
void release_array3d (char*** ptr) {
free (ptr[0][0];)
free (ptr[0];
free (ptr);
}
To demonstrate how we might use these functions, consider the following:
int main (void) {
const int d1=2;
const int d2=3;
const int d3=4;
int x, y, z;
/* instantiate array */
char*** array = create_array3d (d1, d2, d3);
/* initialise it with some values */
for (x=0; x<d1; ++x)
for (y=0; y<d2; ++y)
for (z=0; z<d3; ++z)
array[x][y][z] = (x+1)*(y+1)*(z+1);
/* perform other operations on array */
/* ... */
/* release array */
release_array3d (array);
array = 0;
return 0;
}
Note how we can access the array elements just as if the array were allocated on the stack or in static memory and that the functions hide all the (low-level) implementation details from the user.
How would you declare a constant of 5 called MYCONST?
A constant of 5 called MYCONST would be declared as #define MYCONST 5. This is because the statement used is a define statement.
What Preselected value that stops the execution of a program?
A preselected value that stops the execution of a program is often referred to as an "exit code" or "exit status." This value is typically set to indicate an error or a specific condition under which the program should terminate, with common values being 0 for success and non-zero values for various errors. When the program encounters this exit code, it halts execution and returns control to the operating system or calling process, signaling the outcome of the execution.
Why stack pointer is always incremented?
The stack pointer is typically incremented to manage the stack's memory allocation during function calls and local variable storage. When a function is called, the stack pointer moves to allocate space for local variables and return addresses, effectively growing the stack downwards in memory. Incrementing the stack pointer helps maintain the correct position for accessing these variables and managing function calls efficiently. This process is crucial for maintaining the integrity of the stack structure and ensuring proper memory management.
Strong number program using c?
Nikhil Aggarwal
strong_number: The sum of factorials of digits of a number is equal to the original number.
void strong_number()
{
int num,i,p,r,sum=0,save_num;
printf("\nEnter a number");
scanf("%d",&num);
save_num=num;
while(num)
{
i=1,p=1;
r=num%10;
while(i<=r)
{
p=p*i;
i++;
} //while
sum=sum+p;
num=num/10;
} //while
if(sum==save_num)
printf("%d is a Strong number", save_num);
else
printf("%d is not a Strong number", save_num);
}
C programming language bsc it notes?
different between defining value definition section and defining value declaration section