Talk:C Programming/Q&A Archive

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  function. There will be an unresolved link error, if the  function is omitted from a C program. So the answer to the second question is that you must have a  function defined in a C program.

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.

Learning on a mac
Q. Can I use C on a Mac?

A. Yes, C is the most wide spread portable high level language.

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

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'.

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 compulsory. 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.

Homework assignments
Q) Write a program that count the no. of lines and characters in file.

A) We don't do your homework here.

How to install a C Compiler
Any C compiler that can be downloaded or installed (whether the freeware versions of Turbo C, Dev-CPP, Visual Studio Express or Gcc) should either have an installer or have sufficient instructions to properly install their product.

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

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, "*" ); }

API or function database for C?
Q) 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?

A) The main API used with the C language is known as the Standard C Library. The list of standard libraries can be found at C Programming/Standard libraries, but does not include platform specific functions or third party libraries. You will need to consult the programming documentation for your platforms as necessary.

Regarding memory allocation
Q) 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.

A) For automatic declarations (such as those created on a function call), they will appear on the stack. For declarations made either statically or via malloc, they are located in the heap.

header files
Q: how can we create our own header file?
 * A: Use a text editor. --Jomegat (talk) 17:00, 29 March 2009 (UTC)

about memory management
Q) What is the output 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)

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.

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)

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 Editing guideline for other common practices.) --Panic 19:21, 7 October 2007 (UTC)

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.

reading and printing a matrix
kindly tell me the error in the following programme: 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<
 * 2) include

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)

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)

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 Methods of computing square roots and specifically Methods of computing square roots 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)

bitwiseoperator
Q) Using bitwise right shift operator give and example

A) C Programming/Simple math

interrupts
What 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.

Variables
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.

Header files
What is the use of '#' in '#include '?

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)

Memory Allocation
Q) When we use  compiler make   bytes in the data segment of the program code. When we use function   at run time the program requests the operating system to allocated the block of size bytes.  But, what does the   operator do? Does it also request the operating system?

A) The new operator does nothing in C, since C is not C++. Even if it was implemented in C,  it would allocate it on the heap provided by the operating system.

Swapping values
Q) how can we interchange the value in a program without storing the value

A) See XOR swap algorithm on Wikipedia, and not that it has problems.

Games in C
Q) We can make games using JAVA. Is it possible to do it with C and what is the basic knowledge in C is required.

A) There's plenty of third-party libraries available for C that let you do this.

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)

ctype.h
Why do 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)

Programs without main
Can you write a C program 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)

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!

main
can u give some idea about command line parameters used in main?And what is it's importance?

Answer:

The number of command line arguments (including the program itself) is found in argc. argv contains pointers to the set of command line arguments, where argv[0] is a string to the program, argv[1] to the first pointer, and so on.

Reserved keywords
Q) What is the definition of reserved keyword?

A) A reserved keyword is something that is either in use with by the C standard, or expected to be used in the near future.

about enumerated datatype
Q) What is an enumeration?

A) An enumeration is a set of values. For example:

In this example, numbers is an enumeration, and allows you to use the words "ZERO", "ONE", "TWO", and "THREE" as identifiers to represent the corresponding numbers.

Pointer bounds
Q) How can be check that a pointer may be outside its bound?

A) By keeping track of the data. If you let a pointer carelessly drift out of bounds, then you're already doing something wrong. The easiest ways to protect against this is to either use a sentry value (e.g. null terminations on strings) or to know the number of current records.