0% found this document useful (0 votes)
34 views2 pages

Functions: No of Digits

The document contains code snippets for functions to count the number of digits in an integer and sum the digits of an integer. It also provides information on Vim commands, how to input a test file into a C program, and an error checklist for common programming mistakes.

Uploaded by

Xue Wei Leoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Functions: No of Digits

The document contains code snippets for functions to count the number of digits in an integer and sum the digits of an integer. It also provides information on Vim commands, how to input a test file into a C program, and an error checklist for common programming mistakes.

Uploaded by

Xue Wei Leoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Functions:

No of Digits

int NumberOfDigits (int a) {


int count = 0;
while (a) {
count++;
a/=10;
}
return count;
}
Sum of Digits

int SumOfDigits (int a) {


int sum = 0;
while (a) {
sum+=a%10;
a/=10;
}
return sum;
}

Others:

char char1 = 65  ‘A’


char char2 = 97  ‘a’

Vim Commands:

0 move to beginning of the current line

$ move to end of line

H move to the top of the current window (high)

M move to the middle of the current window (middle)

L move to the bottom line of the current window (low

dd delete line

yy or Y  yank the current line, including the newline character at the end of the line

p paste line

gg=G Fix indentation

How to input test.txt into c program?


If your program reads standard input, then
./a.out < input.txt
will work.

Similarly, you can save all the output as well with


./a.out < input.txt > output.txt

If your program accepts arguments, and tries to open a file for say argv[1], then use
./a.out input.txt

Error Checklist (Possible Reasons):

1. lvalue required as left operand of assignment


- Eg. a + b = c; //semantic error (LHS shud not be sum of two variables.)
2. Segmentation Fault (Core Dump)
- Out of bounds for Array
- Scanf no & sign
3. Answer is 0
- Put ans = 0; in while loop
- Unassigned
4. Answer too large
- Uninitialized variables
- Scanf/printf type wrong
5. Runtime Error
- Ctrl-c to escape
- While loop inescapable
- For loop inescapable (e.g. for (i = 0; i<n; i--))
6. Others
- Forgot to do break in case
- Did while/if (x = y) instead of while/if (x == y)

You might also like