Posts

Showing posts with the label C language

Whats the o/p of following

#define BUFF_SIZE 64*1024 UWord64 fileOffset; fileOffset = readFromFile(BUFF_SIZE); /* above fn returned BUFF_SIZE */ fileOffset -= BUFF_SIZE - 71444;/* 17444=0x11714 */ Options: 1. 18446744069414655764 or 0xffff ffff 0001 1714 2. 71444 or 0x1 1714 Soln: BUFF_SIZE - 71444 i.e 65536 - 71444 is converted to 2's compliment form in Word32 format which is 0xffff e8ec. That value is subtracted from 0x0000 0000 0001 0000. So option 1 is the ans.

C quiz with answers

Q1. What is the form of variable name? A1. 1. Should not begin with digit. 2. Sequence of letters and digits. Q2. What are identifiers? A2. Identifiers = symbols. They can refer to 1. variables 2. type 3. functions 4. labels Q3. What are elements of C language? A3. Following 1. Operator 2. Keywords 3. Identifiers 4. Constants 5. String Literals 6. Punctuation and special characters Q4. What are tokens? A4. The basic element recognized by the compiler is the "token". A token is source-program text that the compiler does not break down into component elements. Note: Elements of C language are discussed above.

L-values and R-values

Expressions in C++ can evaluate to l-values or r-values. L-values are expressions that evaluate to a type other than void and that designate a variable. L-values appear on the left side of an assignment statement (hence the "l" in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values. The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values. // lValues_rValues.cppint main() { int i, j, *p; i = 7; // OK variable name is an l-value. 7 = i; // C2106 constant is an r-value. j * 4 = 7; // C2106 expression j * 4 yields an r-value. *p = i; // OK a dereferenced pointer is an l-value. const int ci = 7; ci = 9; // C3892 ci is a nonmodifiable l-value ((i

Tips to develop C code from scratch.....

I have been blessed to get a chance to really write a from scratch code (before that deciding directory structure and creating blank file also ;)) but simultaneously punished for proceeding directionlessly with the team (even when there was a team leader leader :P). Out of hard things I have learnt following lessons while going for a from scratch development. 1. Decide return codes first. I think all the robust codes are formed due to proper handling of return codes of functions calls. If an enumeration comprising of all the required return codes is finalized before hand then the module writers can effectively use them so that alongwith robustness there is a consistency among the modules. Also to finalize on error codes one has to fully know what he wants to do - the big big key for success. 2. Decide upon coding style Yes, this has to be taken next. If you don't take it now you will never take it later when your code is functional. Even if you take it with lot of resolve (an of co...

Caveat in character constant \'

Escape sequences are used to represent invisible or hard-to-represent characters. Some of them are used for avoiding ambiguities as listed below. \" : Is only necessary in strings i.e. char const '"' (double quote in two single quotes) is valid. \\ : Always reqd to distinguish \ from esc seq. \? : To avoid trigraph sequence ??'. Char const '?' is valid. All above escape sequences are justifiable coz possible ambiguities are obvios. But for escape sequence \' what is ambiguous if we use char const ''' (single quote in two single quotes) is not obvious. Following is the list characters tried to print through a C program and associated compiler errors. char c = x;// x as below printf("%c\n", c); '' : Two single quotes without space in between. error: empty character constant Comment: This means empty character const is not permitted. ''' : Single quote char in two single quotes error: missing terminating ' cha...

Character constant and String constant

A character constant is a character written between single quotes. Although called character constant it represents an interger value. Its value equals to numeric value of the character in the machine's character set. In the ASCII character set: '\n' = 10 '0' (zero) = 48 'A' = 65 'a' = 97 A sequence of characters in double quotes, like "hello, world\n", is called a character string or string constant .

Shell text color control from C program

How to control output text color of a shell? Text color output is not defined in ANSI C/C++. Instead the creators of the language left that to be operating system dependent. In Linux, to change text color you must issue what are known as terminal commands. To print "Hello, world.\!" in red, change the printf statement to read:printf("\033[22;31mHello, world!"); \033[22;31m is the terminal command. Note the numneric escape sequence at the start and on an ASCII based machine, octal 33 is the ESC (escape) code. For further details on numneric escape sequence read http://casual-pavan.blogspot.com/2008/10/intersting-things-about-escape.html To try different colors, just change the numbers after the left bracket. \033[22;30m is black \033[22;31m is red \033[22;32m is green Some more colours: \033[22;33m - brown \033[22;34m - blue \033[22;35m - magenta \033[22;36m - cyan \033[22;37m - gray \033[01;30m - dark gray \033[01;31m - light red \033[01;32m - light green \033[01;33...

Intersting things about escape sequences

Escape sequence provides an general and extensible mechanism for representing hard-to-type or invisible characters. In C language escape sequences always begin with a backslash (\). Charater constants represented by escape sequences like '\n' (newline) look like two characters, but represent only one. There are fundamentally two distinct purposes behind the escape sequences. 1. The pricipal purpose of escape sequences is to represent invisible characters which control the motions of a printing device when they are sent to it. e.g. i. \f - Form feed: Go to the first position on the ‘next page’, whatever that may mean for the output device. The Standard carefully avoids mentioning the physical directions of movement of the output device which are not necessarily the top to bottom, left to right movements commonly followed. ii. Escape sequences also ease the way of representing some special characters that would otherwise be hard to get into a character constant (or hard to read; ...

Diff in strcmp and stricmp

strcmp (s, t) compares the strings and returns a negative, zero or positive if s is lexicographically less than, equal to, or greater than t. The value is obtained by subtracting the characters at the first position where s and t disagree. stricmp performs the same function as strcmp, but alphabetic characters are compared without regard to case. That is, "A" and "a" are considered equal by stricmp, but different by strcmp. Examples: a.out>strcmp("apple", "peach"); -15 a.out> strcmp("peach", "apple"); 15 a.out> strcmp("Apple","Apple"); 0 a.out> strcmp("Apple","Apple pie"); -32 a.out> strcmp("Apple","apple"); -32 a.out> stricmp("Apple","apple"); 0

Whats the significance of static global vars

Q:What is the use of declaring a global variable static? Ans: It causes the object to have internal linkage i.e. the object can be accessed from that file only. For understanding what is internal/external linkage, refer to http://publications.gbdirect.co.uk/c_book/chapter4/linkage.html which also serves as an excellent C language reference book.