Print line by character in C


 
Thread Tools Search this Thread
Top Forums Programming Print line by character in C
# 8  
Old 04-20-2013
That's a GREAT question!. Smilie

Remember that the "c" variable is an int variable. You are using it to store what we call a "character", but ultimately "c" just holds a number.

What number does it store?

Under the "good" logic, it stores ASCII decimal 65 for 'A', etc. for each character it reads. That is fine.

Under the "bad" logic, it stores a 0 or 1 (equal or unequal). What ASCII characters do 0 or 1 represent? Some weird control character. That's why you get the garbage printed.

Does that make sense to you?
This User Gave Thanks to hanson44 For This Post:
# 9  
Old 04-20-2013
Very much! You also answered my question why int is used for getch() and putch() functions in K&R C_book.
# 10  
Old 04-20-2013
How hard was that bug to see?

That's why it's a bad idea to perform assignments inside the conditional clause of thing like an if statement or while loop.

So what if it saves a line or two of code. Writing quality code is not a contest to see who can stuff the most operations in the least number of lines. Especially given that you wind up with bug-prone and hard-to-understand code.
# 11  
Old 04-21-2013
Agreed! I'm trying to pick up C by myself, not for contest or homework. Thank unix.com, I do not need to go back to school, maybe I'd better do it.
# 12  
Old 04-21-2013
I wouldn't worry about it too much. You're doing fine, it just takes a while when you are learning.

I agree with achenlee that there is too much obscure C code, with stuff jammed on one line. I don't write that way. But you see it a lot. Even K&R frequently emphasizes that style. Many expert programmers are used to that kind of terse code, and seem to like it. So you have to also learn to understand it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

4. Shell Programming and Scripting

If first character doesn't equal '#' and same line contains 'temp', print everything else

(2 Replies)
Discussion started by: snoman1
2 Replies

5. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

6. Shell Programming and Scripting

How to print a particular character n number of times in a line??

hi, Is it possible to print a particular character n number of times in a line? for example. i am print the following line using echo command.. echo "files successfully moved" i want to count the number of characters that are been displayed. i am doin it using echo "files... (8 Replies)
Discussion started by: Little
8 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

9. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

10. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies
Login or Register to Ask a Question