Showing posts with label c compiler. Show all posts
Showing posts with label c compiler. Show all posts

Thursday, March 6, 2014

What is BUFSIZ?

BUFSIZ is a constant macro representing the size of the input buffer. It is defined in stdio.h and represents the size for your C implementation. ANSI C requires that it be at least 256 bytes. A common size is 512. It gives us the maximum number of characters that can be transferred through our standard input stream, stdin.

Thursday, January 12, 2012

What would be the result of the expression -6/5?

The ANSI C standard says that the result is "implementation defined." This means that ANSI C has no hard and fast rule, therefore, the result depends on the compiler that you use. Check your compiler; it should give either -1 or -2 as a result.

Note that the "correct" answer is -1.2. The compiler therefore has a choice of rounding up or down.

Tuesday, January 10, 2012

What happens if we try to assign a real type value to a variable that has been declared to be int?

C cuts off the fractional part of the real value, converts the remaining part to int, and stores the result in 2's complement binary form in the memory cell for the variable. For instance, the statement

n = 6/4.0

takes the real value (6/4.0, which is 1.5), cuts off the 0.5 to give 1.0, converts this to 1 (without a decimal point meaning it us in 2's complement binary form), and stores this in memory cell for the variable n.

What is the value of 6/4.0?

For this operation, one operand is a real type and the other operand is an integer type. When this occurs, C converts the integer to a real type temporarily (meaning that 6 is converted to 6.0) then performs the operation and the result is a real type. Thus, 6/4.0 gives the result 1.5.

Friday, January 6, 2012

What are the meanings of operators ++, --, and % ?

The operator ++ is an increment operator, which can be placed before or after (but not both) a variable. The operator will increase the value of the variable by 1. For example, assuming a variable i is equal to 1, then after the statement

i++;

or

++i;

is executed, the value of i is 2 (i++ is not exactly the same as ++i).

Note that the C statement

i++;

or

++i;

can be understood as the statement

i = i + 1;

which also causes the value of the variable i to increase by 1. Similarly, the operator -- is a decrement operator, which decreases the value of a variable by 1. Also, the statement

i--;

or

--i;

can be understood as the statement

i = i - 1;

The operator % is a remainder operator, which must be placed between two integer variables or constants. Assuming k and p are two integer variables, the meaning of k%p is the remainder of k divided by p. For example, if k = 11 and p = 3, then k%p is equivalent 11%3, which is equal to 2 (because 3 goes into 11 three times with a remainder of 2). The operator % is pronounced "mod." So this example would be k mod p. ANSI C states that, if either operand is negative, the sign of the result of the % operation is implementation defined; that is, it is free for the C compiler designer to decide. For example, depending on the compiler you use, the results of -50 % 6 and 50 % (-6) may be 2 or -2.

Given the same value and using the same format, will programs created using different compilers display exactly the same output?

No, because various compilers implement the ANSI C standard differently, so, in general, given the same value and using the same format, the output displayed by programs created with different compilers may be slightly different.

What is the complete structure of format specifications for int and float type data?

The complete structure of format specifications is

%[flag][field width][.precision]type

where format string components enclosed by [ ] are optional (the characters [ and ] are not the part of the format string.

The meanings of these components may vary slightly from compiler to compiler. Therefore, you should check the manual of the compiler you use.

Thursday, January 5, 2012

How do I create a constant macro?

We use a preprocessor directive to create a constant macro. The preprocessor is a system program that is part of the C compiler. It automatically performs various operations prior to the translation of source code into object code. In C, preprocessing directives begin with the # symbol (which must begin the line). A semicolon must not be used at the end of the preprocessing directive. Only the preprocessing directive should be on the line. For example, the line

#define DAYS_IN_YEAR 365

is a preprocessor directive called a define directive.

Wednesday, January 4, 2012

How do we declare more than one variable?

Variables of the same type may be declared in the same statement. However, each must be separated from the others by a comma; for example, the statement

float expense, income;

declares the variables expense and income to be of the float (which must be typed in lower case) type. Float type data contain a decimal point with or without a fraction. For example, 1..1.0, and 0.6 are float type data. When data without a decimal point are assigned to a float type variable, the C compiler automatically places a decimal point after the last digit.

What is debugging?

In your source code, looking for and correcting errors or mistakes that cause your programs to behave unexpectedly is called debugging. In general, there are three types of errors in a C source code: syntax errors, run-time errors, and logic errors.

Syntax errors are mistakes cause by violating the "grammar" rules of C. They easily can be caused by typographical mistakes or a lack of knowledge of the forms of statements required by C. These errors often can be diagnosed by the C compiler as it compiles the program. If your computer indicates errors when your try to compile a program, it will not translate your code into machine instructions. You must fix the errors before the compiler will translate your code. Therefore, when you have syntax errors you will not generate any output, even if your syntax errors are very minor and located in the very last lines of code.

Run-time errors, also called semantic errors or smart errors, are caused by violation of the rules during execution of your program. The compiler does not recognize them during compilation. However, the computer displays a message during execution that something has gone wrong and (usually) that execution is terminated. If a run-time error occurs near the end execution, you may get some of your results. The error message given by the computer may help you locate the source of error in your code.

Logic errors are the most difficult errors to recognize and correct, because the computer does not indicate that there are errors in your program as it does with syntax and run-time errors. It is up to you to identify that there is a problem at all. It is up to you to look at your output and decide that it is incorrect. In other words, your program may have appeared to have executed successfully, perhaps giving very reasonable results. However, the answer maybe completely wrong. You must recognize that they are wrong and correct code in the program. (Be careful though. Many hours have been spent looking for bugs in programs only to find out that the program is correct, but the input data is incorrect.)

How do we write valuable comments?

Be wise in your use of comments. Remember that comments are used to enhance the understandability of your programs. Make them pleasing to the eye and clear. With practice, you will develop a style of writing comments that is of benefit to you. I do not use many comments, primarily because I want you to interpret the code yourself. Your programs should use comments far more frequently than I use them.
I recommend that you avoid writing comments on the same line as other C code unless you can clearly distinguish the comments from the code. It often looks much better if your comments are on lines separate from other statements. There is no standard for writing comments, but I like a style that highlights comments and separates them from other C text. The reason for this is that, if not highlighted, comments tend to blend in with the rest of the code and make following the logic of the code confusing. In other words, do not hide your comments. Make them stand out- they are there to help you and others. I like a style in which each comment line begins with two stars, and the comment block begins and ends with a line of stars. For instance, I strongly recommend that you add a banner at the beginning of your programs. A banner is a set of comments that describe such things as the name, parameters used, history, author, purpose, and date of the program. A better look would be as follows:

/*********************************************************************
** Name: Hola.C
** Purpose: Learning how to write comments in C
** Date: Written on 1/4/12
** Author: 3nriched
** Reference: None
*********************************************************************/

#include

int main(void)
{
printf("How do we write comments in C?");
}


Can we write a nested comment?

No, comment statements cannot be nested (meaning that we cannot write a comment within a comment) in C. For example:

/*/* This is an illegal comment because it is */ nested */

Can a comment appear in a C statement?

That depends. The C compiler treats comments like a single white space character. Therefore, comments can appear in a C statement only where a white space character is allowed. This means that comments may be placed between tokens but not within a token. (Note: a string literal is a token.)

Examples of correct comments:

printf /*This comment is legal */("Welcome to C!");
printf ( /*This comment is also legal*/"Welcome to C!");

Examples of incorrect comments:

pr /* This comment is illegal because it splits a C function
name (i.e., a token) */ intf("Welcome to C!");

printf("This is not a comment and /*will be displayed*/ ");

The text contained in the /* and */ is not a comment because it becomes part of the string literal and will be displayed. For this incorrect comment, the compiler will not indicate an error, it will simply print the phrase:

This is not a comment and /*will be displayed*/

on the screen. For the other comments, the compiler will indicate an error when you try to compile the program.

Can we write comments at the very beginning and end of a program?

Yes, a comment line can be written in the very first line of a program. It also can be written on the very last line of a program.

What is the structure of a comment?

The syntax of a C comment is:

/* Any text, number, or character */

where there should be no blanks(s) between the slash and the asterisk. In addition, the /* and */ must form a couple. The /* and */ are called comment delimiters.
The /* and */ must form a couple, but they need not be on the same line. Therefore, a comment line may occupy more than one line. A multi-line comment starts with /* followed by multiple lines of text consisting of numbers, characters, or symbols. The multi-line comment terminates with */.

Examples of incorrect comments:

/* Wrong comment 1, and no end asterisk and slash
/* Wrong comment 2, no end slash
/ *Wrong comment 3, there is a blank between / and * */

Why is the look of a program important?

The look of a program is important because programs continually undergoing change. A program that is neat and organized is easier to understand and therefore easier to modify. As a result, the likelihood for error is reduced in programs that follow a certain visual and organizational style compared to those that do not.

Is it necessary to use different lines in writing code?

A white-space character, such as that created when you press the Enter key, used between tokens is invisible to the C compiler. Therefore, you have the freedom to write C code at any row or column you like. The C compiler, for example, allows you to rewrite and pack into one line:

#include void main(void){printf("This is C!");}

or rewrite it as

#include

void main(void)
{
printf("This is C!");
}

However, these styles will make your program more difficult to understand and should not be used.

There is no required form for spacing within your programs. However, your instructor or employer may want you to adhere to certain standard accepted styles. Our example programs are meant to illustrate acceptable style, however, at times, publishing constraints do not allow us to follow any one accepted style rigorously. Indentation and spacing are considered important for the look of a program, even though they do not affect performance. To make your program readable, do such things as write one statement per line, line up your braces, and add blank lines where there are natural breaks in code instructions.

Where are blank spaces permitted in C code?

C code consists of a number of tokens. A C token is the smallest element that the C compiler does not break down into smaller parts. A token can be a function name, such as main, or a C reserved word. All C words should be written continuously. For example, the expression

void ma in(void)

is not legal because no blank characters are allowed between the characters a and i in the word main.

Between tokens, white-space characters (such as blank, tab, or the carriage return) can be inserted, but this is optional. For example, the line

void main(void)

is equivalent to

void main ( void )

or

void main ( void)

In general, it is acceptable to add blanks between tokens but not acceptable to add blanks within tokens.

Will the follow C program work correctly?

main()
{
printf("This is C!");
}


It may. However, this depends on your compiler. The reason it may work is that;

1) This file stdio.h is used so frequently in C that, for some compilers, it is attached automatically to programs despite no specific direction to do this.

2) Even though you have not written "void main(void)"-that is, the voids are missing-C assigns default types for the voids. The word default is used commonly in computing. A default value is a value that is used when none is specified. Without going into detail, because C assigns default types for missing voids, this program may work. However, I would not recommend that you write your programs in this manner.

Why does it need to be called main?

The C compiler needs to know where execution is to begin. By definition, the primary function, main, is the first function to be executed. Other functions can be named almost anything that you want. A typical program has a large number of functions that you have written. The C compiler searches for the function named main and compiles the program in a manner to ensure that main is the first function executed.