?
Solved

Preprocessor, use for #define is to give symbolic names to string constants

Posted on 2003-03-13
7
Medium Priority
?
318 Views
Last Modified: 2012-05-04
One common use for #define is to give symbolic names to string constants. After encountering

#define  DIGITS    "0123456789"
#define  LOWERS    "abcdefghijklmnopqrstuvwxyz"

the preprocessor replaces each occurrence of DIGITS with string constant "0123456789" and each occurrence of LOWERS with string constant "abcdefghijklmnopqrstuvwxyz". Define a separate header file that contains definitions of some of the more common strings we’re likely to use (ex UPPERS for upper case letters, PUNCTUS for  ,.;:!? ,  QUOTES for  ‘ ” and  BRACKETS  for  ( ){ }[ ]. Write a program that reads a sequence of characters. The program should count the various types of characters and print them. Include the header file in your source code file.
0
Comment
Question by:tom41982
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134275
We're not here to do your homework for you. If you wish, we can help you out with a few hints as to how to proceed with the problem.

Mayank.
0
 
LVL 5

Accepted Solution

by:
Kocil earned 150 total points
ID: 8137616
Usually we ask a student to do his/her best shoot, the we will make the correction. This time, I'll give you the skeletons, you finish the rest by your self.


/* Define a separate header file that contains definitions of some of the more common strings were likely to use (ex UPPERS for upper case letters, PUNCTUS for  ,.;:!? ,  QUOTES for    and  BRACKETS  for  ( ){ }[ ]. */

// First step. create a new file named pre.h
#ifndef PRE_H
#define PRE_H

// define your constants here
#define TYPE1  "value"
#define TYPE2  "value"

#endif


/* Write a program that reads a sequence of characters. The program should count the various types of characters and print them. */
/* Include the header file in your source code file. */


// Second step
// create create a new file, name it pre.c

// Include the header file
#include "pre.h"


main()
{
   char sequences[100];
   
   // read the sequences

   // count the sequence type

}


OK ?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8141102
I'm wondering what the real intent of this homework problem is. The intro correctly points out that using defines like this replaces each occurrence of the SYMBOL with a copy of the string that was defined ... but it leaves open the question of whether you want each occurrence of the symbol to be a separate instance of the string or if you'd prefer that all of the occurrences re-use the same storage space, since, at least in these examples, the strings are constants. Normally, when writing larger programs, such considerations come into play and, since it's undesirable to allocated storage and initialize variables in a header file, you often would need a .c file corresponding to the .h file which can do these things and which is compiled and loaded with the program that's using the header file.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tom41982
ID: 8141557
Thanks for the skeletons that you gives me..
you have a great day^^
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8142310
> I'm wondering what the real intent of this homework problem is.

Have you ever know any assignment that have real purpose except to make grade A, B, C, D, or E ?
 
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8142679
It's been a long time since I was teaching, but when I made up a problem, I tried to make ones that did not teach things that would later have to be unlearned.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8143145
Well, older teacher is wiser.
In many cases, computer assignment is made by senior students.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question