C programming/Q&A
From Wikibooks, the open-content textbooks collection
Welcome to the C Programming questions and answers page.
Feel free to post any questions you have while learning C Programming.
If you have questions about this book, post them on the C Programming discussion page.
If you know the answer to a question, or you can improve any, go ahead and do it.
Ask a question!
[edit] main() function
main() function in C?The operating system calls the main() function, when the program starts. At that point the main() function has already been translated to machine code, which is stored in an executable file along with the rest of the program. The main() function is also being used for command line parameter passing to the program; the compiler adds the operating system specific calls to do that.
Q. Who calls the main() function in C?
A. The operating system calls the main() function, when the program starts. At that point the main() function has already been translated to machine code, which is stored in an executable file along with the rest of the program. The main() function is also being used for command line parameter passing to the program; the compiler adds the operating system specific calls to do that.
[edit] multiple main
Q. Can we have a multiple main in a single program? Can we create a prog without a main function?
A. Each C program has one and only one main() function. There will be an unresolved link error, if the main() function is omitted from a C program. So the answer to the second question is that you must have a main() function defined in a C program.
[edit] Run time environment
Q. What is run time environment in C?
A. During development, the programmer needs to be concerned with two types of errors. One is called the compilation error, the other is called the run time errors. Compilation error occurs when the C source program is being compiled to native code. When the compiler catches a error in the code, it will report to the programmer to fix it. Higher the programming language the more types of errors it would catch. Not all bugs can be caught by the compiler. Run time bugs like divide by zero can not be caught by the compiler.
C language add some primitive run time error checking and exit the program with some information about the problem and its location, by giving a so called a core-dump.
This run time error checking program added to the executable is called the run time environment to the program.
[edit] Learning on a mac
Q. Can I use C on a Mac?
A. Yes, C is the most wide spread portable high level language.
[edit] fscanf
Q. how can i use the file operator "fscanf" to read a string or an array of characters from a test file?
A. You can do it with code as shown below
#include<stdio.h>
main()
{
FILE *fp;
char string[20];
clrscr();
if((fp = fopen("balaram.txt","r")) != NULL) // Opens the file if exists
{
fseek(fp,0,SEEK_SET); // This is optional, to place the seek pointer to begining of the file
fscanf(fp,"%s",string); // Reads the sequence of characters to the array(String) 'string'
printf("The String is : %s\n",string);
}
else
{
printf("FILE CAN NOT BE OPENED\n");
}
fclose(fp);
getch();
}
[edit] Why binary files?
Q1. What is the need to do file manipulations in binary format?
A1. It depends on the application. Your application may store some information in file, that it reads back in later. The easiest way is to define a 'struct', that can have both interger and character values. When you read your data back, just map to your 'struct'. If different application created the file, make sure that you are using the same 'struct' and both C program compiled on the same (32 or 64 bit) platform. Integer length are different on 32 and 64 bit platform.
Q2. If so, what data type is used in C to represent "binary data"?
A2. The binary data can contain both integer, double, and or character values. You need to know the format though, where one data value starts and ends. The best way is to create 'struct' for a record. You read in the record from the file as unsigned character string and then map to the 'struct'.
[edit] Is return type compulsory in c & why it is compulsory in c++?
Both in C and C++ the return type from a function is not comuldory. If you have a function that do not need to return a value, use the 'void' type. In that case the 'return' keyword is optional, but can be used without specifying any value.
[edit] array element
How do you scan an individual element into an array in a loop until it is full.
[edit] solution
Write a program that count the no. of lines and characters in file.
A) We don't do your homework here.
[edit] how to install TURBOC in computer
U can download from any site or you can use latest version released by borland. For windows u can use Dev-cpp or Visual cpp
[edit] Turbo C variable length string data elements that are delimited by "*", need parsed
Hi,
I'm trying to code (in Borland Turbo C). Have many variable length string text data elements that are delimited by "*",
Example David*Smukinhoven*123456 Main St.*HongKong*China*(888)488-6359**0* * Jon*Eva*10 Downing Street*USA*Japan*(555)654-9874*20106*17.02*yen*
I need to break up the string into different fields so I can put the data into different forms.
I have a little experience with C; I do not want to use C++.
ANY HELP will be Apprciated
Use <code> char *strtok(char *s1, const char *s2); </code> function. See example bellow: char *s1 = "David*Smukinhoven*123456 Main St.*HongKong*China*(888)488-6359**0* *Jon*Eva*10 Downing Street*USA*Japan*(555)654-9874*20106*17.02*yen*"; char *parsed = char *[100]; char *elem = strtok( s1, "*" ); int i = 0; while ( elem != null ) { /* Strores the element */ parsed[i++] = elem; /* Get the next element */ elem = strtok( null, "*" ); }
[edit] main()
Q) can we run prog without main()
A) The question was answered above, and was a distinct NO. You must have a main() function, and there must only be one of them. 82.26.142.128 21:58, 18 June 2007 (UTC)
[edit] API or function database for C?
When I was learning Java, the professor showed the class an official API site (if I remember the term correctly), which showed documented all the functions that could be used listed by package, including inputs, outputs, and author(s) notes. Is there a similar site for all the functions in C? I did some Google work and found some sites that mentioned some packages, but I was wondering if there was an official site that had everything, or at least the more important packages?
Thanks. -Steven
[edit] Regarding memory allocation
Where the memory will be allocated for an array declaration? —The preceding unsigned comment was added by Parthisands (talk • contribs) 2007-06-14T04:37:54.
[edit] header files
Q: how can we create our own header file?
Q: what is the coercion
- A: This question does not make sense. Perhaps you could rephrase it? --Jomegat (talk) 17:00, 29 March 2009 (UTC)
Q: how can i open c language in my computer?
- A: This question does not make sense. What are you trying to do? --Jomegat (talk) 17:00, 29 March 2009 (UTC)
[edit] about memory management
what is the out put if we use free(NULL);
A) free() does not ever "output" anything. It is acceptable to call free(NULL); no operation will be performed. --Spoon! (talk) 02:25, 17 May 2008 (UTC)
[edit] memory management
what is the out put of following?
int *ptr; ptr=(int *)malloc(0);//malloc(zero)
A) There's no output, you should learn a bit more man.
if(cond) printf("HELLO"); else printf("WORLD"); } What is the condition to print both hello world? A)Is this a comment to dis stupid question if dis is a question den dont u think that it is something like to say a thing both true and false.Only one condition can be satisfied so either hello is printed or world.Do u know dat cndntn write it plzzzzzzz
[edit] howto do graphics programming
How to show graphics using C ?
A) There is no direct support (since it is platform dependent), there are 3rd party libraries that provide such function, see what you get by using the terms ACE, SDL or OpenGL or DirectX on a WEB search engine. --Panic 19:06, 7 October 2007 (UTC)
[edit] ABOUT C HEADER FILE
HI, Q. WHICTH HEADER FILES ARE NEED TO INCLUDE IN C PROGRAMS AND WHICTH NOT? EXAMPLE:- MATH.H HEADER FILE IS NEED TO INCLUDE BUT STDIO.H HEADER FILE IS NOT NEED TO INCLUDE WHEN WE SAVE PROGRAM WITH .C EXTENTION.
A) The answer to your question is on the book in [here], it explains the use of the include directive you must also understand the functions you are calling to know what to include. (PS: Next time try to write in lowercase it is good etiquette, as is it seems that you are shouting, see Wikibooks:Editing guideline for other common practices.) --Panic 19:21, 7 October 2007 (UTC)
[edit] Multiple return values?
Q: Can a C function have multiple return values?
A: Basicaly no. A C function can return only one value. However, that return value can be a pointer to the first element of an array, or a pointer to a multiple value struct. In that case anything can be returned. In addition, the function can take in additional pointer arguments, where the function "fills in" information in the locations pointed to by the pointers (which are usually variables in the caller function), thus effectively returning additional information.
[edit] reading and printing a matrix
kindly tell me the error in the following programme:
#include<iostream.h>
#include<conio.h>
int main()
{
int m[2][3],i,j;
clrscr();
cout<<"enter values for matrix:"<<"\n";
for(i=0;i<2;i++)
{
for(j=o;j<3;j++)
{
cin>>m[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<m[i][j];
}
cout<<"\n";
}
getch();
return 0;
}
—The preceding unsigned comment was added by Anki agarwal (talk • contribs) 2008-01-31T07:15:00.
Are you referring to the use of the letter o instead of the number 0 in the assignment to j, or perhaps to the way the output numbers are not delimited? Are you getting a compiler error or unexpected output? Rodasmith (talk) 22:08, 31 January 2008 (UTC)
Also, this is C++ not C. --Spoon! (talk) 02:27, 17 May 2008 (UTC)
[edit] Timers in C
How can I implement timers in C? KrisVelkovski (talk) 20:13, 18 February 2008 (UTC)
More information may be needed (for what setup/OS) ANSI C for what remember ( C89 ) can only make use of hardware interrupts (asm, not portable) or needs an external library to handle something more complex. --Panic (talk) 23:21, 18 February 2008 (UTC)
- I'm using windows and mingw. KrisVelkovski (talk) 15:19, 20 February 2008 (UTC)
-
- Check the Windows Programming book, what you need is Win32 API information. --Panic (talk) 16:36, 20 February 2008 (UTC)
-
-
- Have a look at msdn. --212.149.216.233 (talk) 15:20, 21 February 2008 (UTC)
-
[edit] Function to perform square root
I want to calculate the square root of any number without using of sqrt() predefined function in C.
Wikipedia Has The Answers(TM). See w:Methods of computing square roots and specifically w:Methods of computing square roots#Binary (base 2) where the function is probably valid in C too when you leave first few words out. --192.130.48.36 (talk) 19:08, 4 March 2008 (UTC)
[edit] bitwiseoperator
Using bitwise right shift operator give and example
[edit] C program to make a binomial distribution table
C program to make a binomial distribution table Small Text
how to i get free book . with out fee .and i wanna download turbo c++ book. how i uses this procidure
example program to get quadratic function
[edit] pointers
please explain in simple llanguage the real time pointer applications in c programming. the program should also be explanatory
[edit] interrupts
whay are innterrupts?
- Interrupts are external events that cause program execution to suspend and resume at a different place. The "different place" is an interrupt service routine, or ISR. When the ISR completes, the program resumes execution at the point where it was suspended.
they are of how many types?
- There are two major types of interrupts: hardware and software. Hardware interrupts are generated by an event in hardware, such as the arrival of a character from a UART, a key being pressed on a keyboard, or an anomally detected by the processor (such as division by zero, or access to an illegal memory address). A software interrupt is generated by software. Software interrupts can be generated by a program as a method of communication.
how to use interrupts in 'c'?
- You must consult the documentation for the operating system you are programming for. In some cases, you can raise them directly, but in other cases, you must use the functions provided by the platform.
[edit] question
how to write a c_programming alogorithm for finding a determint of a 3*3 matrix
[edit] questions
what does static variable mean?
- A static variable is one that has persistent storage. It is not accessible outside its scope, but it retains its value as the program exits and re-enters its scope. There are two "scopes" to consider. The first is when a static variable is declared in a file, but outside of a function. Such a variable is available to all functions in the file, but not available to functions outside the file. The second scope to consider is a static variable declared inside a function. In this case, the variable is accessible only to that function. When the function exits, the variable does not disappear, but retains its value, so that when the program re-enters the function, it's still there in exactly the same state as when the function was previously exited.
what is a pointer?
- A pointer is the address of a variable. The variable can be a scalar, an array, or a structure. It could also be a function.
what is a structure?
- A structure is an organized blob of data. It consists of programmer-defined elements that have been grouped together.
what are the difference between structures & arrays?
- Arrays are a group of variables all having the same type. The elements of the array can be indexed numerically (as in x[5] - that's the fifth element in the array. Structures are similar, but do not have the restriction of requiring every element within to be of the same type. A structure could contain (for example) an int, an array of 25 chars, and a pointer. Each element in the structure is named, and it is accessed by its element name.
in header files whether functions are declared or defined?
- You could do either, but if the header file is included by more than one c file, the program might not link. Exceptions to this are functions that are declared as static, and functions that are declared as inline. However, it's not generally a good idea to define a function in a header file. It is a good practice, however, to define the function's prototype (i.e, its interface) in a header file. The prototype defines the number of arguments to the function, as well as the type of each argument. It also defines the type of the function's return value.
[edit] can we have a return type to clrscr function
do we have any return types to clrscr function , or can we use . please answer me if we can use any return types to it
[edit] can we have a return type to clrscr function
do we have any return types to clrscr function , or can we use . please answer me if we can use any return types to it
HEADER FILE:
WHAT IS THE USE OF '#' IN '#INCLUDE<HEADER FILE NAME>'? IS THE USE "#" IS SAME IN "#INCLUDE<HEADER FILE NAME>" , "#DEFINE VARIABLE VALUE"(MACRO)?
[edit] HEADER FILE:
WHAT IS THE USE OF '#' IN '#INCLUDE<HEADER FILE NAME>'? IS THE USE "#" IS SAME IN "#INCLUDE<HEADER FILE NAME>" , "#DEFINE VARIABLE VALUE"(MACRO)?
The pound symbol marks the start of a preprocessor directive; see C Programming/Compiling 66.240.31.42 (talk) 17:02, 21 November 2008 (UTC)
[edit] Problem on Linked List
2) Write a C program that reads a list of integers from the keyboard, create a linked list out of them, traverses it and returns the data in the node with the minimum key value, and deletes all the nodes where keys are negative
[Hint use functions for various modules]
[edit] Memory Allocation
When we use int x[SIZE]; compiler make sizeof(int)*SIZE bytes in the data segment of the program code. When we use function void *malloc(unsigned int size); at run time the program requests the operating system to allocated the block of size bytes. But, what does the new() operator do? Does it also request the operating system?
what is prototype? A.Definition B.Initialization C.Function D.Declaration
how can we interchange the value in a program without storing the value
[edit] Memory out of bound!!
Is there any way to ignore the message "Memory out of bound." when running a program from it's '.exe'file? I have to declare very big array size for my program. The file created (.exe) is of 1.8MB.
How to generate a Magic square using C?
[edit] Runtime errors
what are the various runtime errors that occur during the runtime in C programming language?
write a program to check the given number is even or odd
[edit] Games in C
Like we can make games using JAVA is it possible to do it with C and what is the basic knowledge in C is required.
[edit] Format specifiers
what are format specifiers
[edit] How can I differ
How can I differentiate between programing in C, C+ and C++
- C+ isn't a language. C++ uses additional features, such as classes and templates, and enforces a slightly stronger type checking. --Sigma 7 (talk) 14:56, 25 March 2009 (UTC)
[edit] code for addition of two matrix using array of pointers
[edit] headerfile
why we use ctype.h header file?
- It provides access to functions that quickly determine the nature of the character (i.e. isupper, islower, etc.) These functions are locale specific in the event you are programming in a non-English language. --Sigma 7 (talk) 14:56, 25 March 2009 (UTC)
What is grounded header linked list & summit a program for understanding it?
write a program in c that receives an arbitrary sentence of not more than 250 characters including spaces and punctuation marks from the standard input and output. the program should then produce the following output; 1. the input sentence without any spaces
[edit] Programs without main
can u write a c programme without using main function in c language?
- You can write a program without using main() in the C language. Aside from some platforms that require you to use a different program startpoint, C is capable of creating freestanding applications (that don't require an operating system) which require you to define the necessary startpoint yourself. The procedure for doing this depends on the compiler you are using, as well as the target platform. However, you probably should learn the C language before attempting this. --Sigma 7 (talk) 19:19, 5 April 2009 (UTC)
[edit] about C language
I want to learn C language.How I begin C language.
i would like ,advantage\disadvatage c programming
A. go to this site
http://en.wikibooks.org/wiki/C_programming and download the PDF version.this will be the best source for learning C . for amateurs!!
learning C means learning the basics of programming..!! if u want to learn any other languages like JAVA, Basic,C++ etc ...first start with C!
[edit] main()
can u give some idea about command line parameters used in main()?And what is it's importance?
how can i search aauther book named bala uru swamy...........
[edit] loop base
what is the loop base?
[edit] c programing
write a program to input a number and its binary
1.get following fields in a batch of 10 employee number employee name employee's date of joining employee address employee salary 2.using any sorting algorithm of your choice,sort using employee number 3.print employee names who are drawing salaries more than rs.10000 and joined before august 2008. use pointer to structures & pointer to functions to implement the above
[edit] QUATION
WHAT IS FRIEND FUNTION?
How can i install a cprograming software in my laptop? What are the basic that we should keep in mind while making a program?
what is c programing , & its featur also ?
[edit] PDLC
what is program development life cycle?
[edit] Link list
how to insert a value in sorted link list ? please show with a program.